Move logrus setup code to log package

Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
Maksym Pavlenko
2023-04-24 10:14:13 -07:00
parent fdd1be6734
commit 370be0c18f
4 changed files with 64 additions and 41 deletions

View File

@@ -36,7 +36,6 @@ import (
srvconfig "github.com/containerd/containerd/services/server/config"
"github.com/containerd/containerd/sys"
"github.com/containerd/containerd/version"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"google.golang.org/grpc/grpclog"
)
@@ -150,7 +149,7 @@ can be used and modified as necessary as a custom configuration.`
// Stop if we are registering or unregistering against Windows SCM.
stop, err := registerUnregisterService(config.Root)
if err != nil {
logrus.Fatal(err)
log.L.Fatal(err)
}
if stop {
return nil
@@ -203,7 +202,7 @@ can be used and modified as necessary as a custom configuration.`
// Launch as a Windows Service if necessary
if err := launchService(server, done); err != nil {
logrus.Fatal(err)
log.L.Fatal(err)
}
select {
case <-ctx.Done():
@@ -343,11 +342,7 @@ func setLogLevel(context *cli.Context, config *srvconfig.Config) error {
l = config.Debug.Level
}
if l != "" {
lvl, err := logrus.ParseLevel(l)
if err != nil {
return err
}
logrus.SetLevel(lvl)
return log.SetLevel(l)
}
return nil
}
@@ -358,21 +353,7 @@ func setLogFormat(config *srvconfig.Config) error {
f = log.TextFormat
}
switch f {
case log.TextFormat:
logrus.SetFormatter(&logrus.TextFormatter{
TimestampFormat: log.RFC3339NanoFixed,
FullTimestamp: true,
})
case log.JSONFormat:
logrus.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: log.RFC3339NanoFixed,
})
default:
return fmt.Errorf("unknown log format: %s", f)
}
return nil
return log.SetFormat(f)
}
func dumpStacks(writeToFile bool) {
@@ -387,7 +368,7 @@ func dumpStacks(writeToFile bool) {
bufferLen *= 2
}
buf = buf[:stackSize]
logrus.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
log.L.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
if writeToFile {
// Also write to file to aid gathering diagnostics
@@ -398,6 +379,6 @@ func dumpStacks(writeToFile bool) {
}
defer f.Close()
f.WriteString(string(buf))
logrus.Infof("goroutine stack dump written to %s", name)
log.L.Infof("goroutine stack dump written to %s", name)
}
}