Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 8 additions & 16 deletions init.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package main

import (
"fmt"
"os"
"runtime"
"strconv"

"github.com/opencontainers/runc/libcontainer"
"github.com/opencontainers/runc/libcontainer/logs"
_ "github.com/opencontainers/runc/libcontainer/nsenter"
"github.com/sirupsen/logrus"
)
Expand All @@ -19,25 +17,19 @@ func init() {
runtime.GOMAXPROCS(1)
runtime.LockOSThread()

level := os.Getenv("_LIBCONTAINER_LOGLEVEL")
logLevel, err := logrus.ParseLevel(level)
logLevel, err := logrus.ParseLevel(os.Getenv("_LIBCONTAINER_LOGLEVEL"))
if err != nil {
panic(fmt.Sprintf("libcontainer: failed to parse log level: %q: %v", level, err))
panic(err)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error message from logrus.ParseLevel is descriptive enough, no need to wrap it.

}

logPipeFdStr := os.Getenv("_LIBCONTAINER_LOGPIPE")
logPipeFd, err := strconv.Atoi(logPipeFdStr)
logPipeFd, err := strconv.Atoi(os.Getenv("_LIBCONTAINER_LOGPIPE"))
if err != nil {
panic(fmt.Sprintf("libcontainer: failed to convert environment variable _LIBCONTAINER_LOGPIPE=%s to int: %s", logPipeFdStr, err))
}
err = logs.ConfigureLogging(logs.Config{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps the original intent was to try to abstract away logrus, to allow swapping with something else, but given that we already import logrus here, I agree that it doesn't add anything really useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It actually makes things worse because

  • its two users have different needs;
  • its existence makes the intention of libcontainer/logs package blurry (since the removal it's now clear that this it's about forwarding logs from runc init to the parent process).

LogPipeFd: logPipeFd,
LogFormat: "json",
LogLevel: logLevel,
})
if err != nil {
panic(fmt.Sprintf("libcontainer: failed to configure logging: %v", err))
panic(err)
}

logrus.SetLevel(logLevel)
logrus.SetOutput(os.NewFile(uintptr(logPipeFd), "logpipe"))
logrus.SetFormatter(new(logrus.JSONFormatter))
logrus.Debug("child process in init()")

factory, _ := libcontainer.New("")
Expand Down
40 changes: 24 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package main

import (
"errors"
"fmt"
"io"
"os"
"runtime"
"strings"

"github.com/opencontainers/runc/libcontainer/logs"
"github.com/opencontainers/runc/libcontainer/seccomp"
"github.com/opencontainers/runtime-spec/specs-go"

Expand Down Expand Up @@ -149,7 +149,7 @@ func main() {
return err
}

return logs.ConfigureLogging(createLogConfig(context))
return configLogrus(context)
}

// If the command returns an error, cli takes upon itself to print
Expand All @@ -173,22 +173,30 @@ func (f *FatalWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}

func createLogConfig(context *cli.Context) logs.Config {
logFilePath := context.GlobalString("log")
logPipeFd := 0
if logFilePath == "" {
logPipeFd = 2
func configLogrus(context *cli.Context) error {
if context.GlobalBool("debug") {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetReportCaller(true)
}
config := logs.Config{
LogPipeFd: logPipeFd,
LogLevel: logrus.InfoLevel,
LogFilePath: logFilePath,
LogFormat: context.GlobalString("log-format"),
LogCaller: context.GlobalBool("debug"),

switch f := context.GlobalString("log-format"); f {
case "":
// do nothing
case "text":
// do nothing
case "json":
logrus.SetFormatter(new(logrus.JSONFormatter))
default:
return errors.New("invalid log-format: " + f)
}
if context.GlobalBool("debug") {
config.LogLevel = logrus.DebugLevel

if file := context.GlobalString("log"); file != "" {
f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_APPEND|os.O_SYNC, 0o644)
if err != nil {
return err
}
logrus.SetOutput(f)
}

return config
return nil
}