init.go, main.go: don't use logs.ConfigureLogging

This function is somewhat strange and I always wanted to remove it,
as it tries to satisfy both init.go and main.go, which have somewhat
different needs.

It is more straightforward and readable to configure logrus directly.

While at it, simplify errors on panic (errors from logrus.ParseLevel
and strconv.Atoi already contain value which they fail to parse, and
panic already contains enough context to figure out what's wrong).

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin
2021-07-25 20:43:17 -07:00
parent 3023e6c625
commit f77fb7a3ee
2 changed files with 33 additions and 33 deletions
+8 -16
View File
@@ -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("")
+25 -17
View File
@@ -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
}