diff --git a/init.go b/init.go index d39492952..61026e220 100644 --- a/init.go +++ b/init.go @@ -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" ) @@ -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) } - 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{ - 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("") diff --git a/main.go b/main.go index f141e79b4..e610e4b65 100644 --- a/main.go +++ b/main.go @@ -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" @@ -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 @@ -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 - } - config := logs.Config{ - LogPipeFd: logPipeFd, - LogLevel: logrus.InfoLevel, - LogFilePath: logFilePath, - LogFormat: context.GlobalString("log-format"), - LogCaller: context.GlobalBool("debug"), - } +func configLogrus(context *cli.Context) error { if context.GlobalBool("debug") { - config.LogLevel = logrus.DebugLevel + logrus.SetLevel(logrus.DebugLevel) + logrus.SetReportCaller(true) } - return config + 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 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 nil }