Move logrus setup code to log package
Signed-off-by: Maksym Pavlenko <pavlenko.maksym@gmail.com>
This commit is contained in:
parent
fdd1be6734
commit
370be0c18f
@ -36,7 +36,6 @@ import (
|
|||||||
srvconfig "github.com/containerd/containerd/services/server/config"
|
srvconfig "github.com/containerd/containerd/services/server/config"
|
||||||
"github.com/containerd/containerd/sys"
|
"github.com/containerd/containerd/sys"
|
||||||
"github.com/containerd/containerd/version"
|
"github.com/containerd/containerd/version"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"github.com/urfave/cli"
|
"github.com/urfave/cli"
|
||||||
"google.golang.org/grpc/grpclog"
|
"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 if we are registering or unregistering against Windows SCM.
|
||||||
stop, err := registerUnregisterService(config.Root)
|
stop, err := registerUnregisterService(config.Root)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.Fatal(err)
|
log.L.Fatal(err)
|
||||||
}
|
}
|
||||||
if stop {
|
if stop {
|
||||||
return nil
|
return nil
|
||||||
@ -203,7 +202,7 @@ can be used and modified as necessary as a custom configuration.`
|
|||||||
|
|
||||||
// Launch as a Windows Service if necessary
|
// Launch as a Windows Service if necessary
|
||||||
if err := launchService(server, done); err != nil {
|
if err := launchService(server, done); err != nil {
|
||||||
logrus.Fatal(err)
|
log.L.Fatal(err)
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@ -343,11 +342,7 @@ func setLogLevel(context *cli.Context, config *srvconfig.Config) error {
|
|||||||
l = config.Debug.Level
|
l = config.Debug.Level
|
||||||
}
|
}
|
||||||
if l != "" {
|
if l != "" {
|
||||||
lvl, err := logrus.ParseLevel(l)
|
return log.SetLevel(l)
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
logrus.SetLevel(lvl)
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -358,21 +353,7 @@ func setLogFormat(config *srvconfig.Config) error {
|
|||||||
f = log.TextFormat
|
f = log.TextFormat
|
||||||
}
|
}
|
||||||
|
|
||||||
switch f {
|
return log.SetFormat(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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func dumpStacks(writeToFile bool) {
|
func dumpStacks(writeToFile bool) {
|
||||||
@ -387,7 +368,7 @@ func dumpStacks(writeToFile bool) {
|
|||||||
bufferLen *= 2
|
bufferLen *= 2
|
||||||
}
|
}
|
||||||
buf = buf[:stackSize]
|
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 {
|
if writeToFile {
|
||||||
// Also write to file to aid gathering diagnostics
|
// Also write to file to aid gathering diagnostics
|
||||||
@ -398,6 +379,6 @@ func dumpStacks(writeToFile bool) {
|
|||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
f.WriteString(string(buf))
|
f.WriteString(string(buf))
|
||||||
logrus.Infof("goroutine stack dump written to %s", name)
|
log.L.Infof("goroutine stack dump written to %s", name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ package log
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@ -38,6 +39,9 @@ type (
|
|||||||
|
|
||||||
// Fields type to pass to `WithFields`, alias from `logrus`.
|
// Fields type to pass to `WithFields`, alias from `logrus`.
|
||||||
Fields = logrus.Fields
|
Fields = logrus.Fields
|
||||||
|
|
||||||
|
// Level is a logging level
|
||||||
|
Level = logrus.Level
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -50,8 +54,52 @@ const (
|
|||||||
|
|
||||||
// JSONFormat represents the JSON logging format
|
// JSONFormat represents the JSON logging format
|
||||||
JSONFormat = "json"
|
JSONFormat = "json"
|
||||||
|
|
||||||
|
// TraceLevel level.
|
||||||
|
TraceLevel = logrus.TraceLevel
|
||||||
|
|
||||||
|
// DebugLevel level.
|
||||||
|
DebugLevel = logrus.DebugLevel
|
||||||
|
|
||||||
|
// InfoLevel level.
|
||||||
|
InfoLevel = logrus.InfoLevel
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// SetLevel sets log level globally.
|
||||||
|
func SetLevel(level string) error {
|
||||||
|
lvl, err := logrus.ParseLevel(level)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
logrus.SetLevel(lvl)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetLevel returns the current log level.
|
||||||
|
func GetLevel() Level {
|
||||||
|
return logrus.GetLevel()
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetFormat sets log output format
|
||||||
|
func SetFormat(format string) error {
|
||||||
|
switch format {
|
||||||
|
case TextFormat:
|
||||||
|
logrus.SetFormatter(&logrus.TextFormatter{
|
||||||
|
TimestampFormat: RFC3339NanoFixed,
|
||||||
|
FullTimestamp: true,
|
||||||
|
})
|
||||||
|
case JSONFormat:
|
||||||
|
logrus.SetFormatter(&logrus.JSONFormatter{
|
||||||
|
TimestampFormat: RFC3339NanoFixed,
|
||||||
|
})
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unknown log format: %s", format)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// WithLogger returns a new context with the provided logger. Use in
|
// WithLogger returns a new context with the provided logger. Use in
|
||||||
// combination with logger.WithField(s) for great effect.
|
// combination with logger.WithField(s) for great effect.
|
||||||
func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context {
|
func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context {
|
||||||
|
@ -30,7 +30,6 @@ import (
|
|||||||
"github.com/containerd/containerd/platforms"
|
"github.com/containerd/containerd/platforms"
|
||||||
"github.com/containerd/containerd/plugin"
|
"github.com/containerd/containerd/plugin"
|
||||||
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"k8s.io/klog/v2"
|
"k8s.io/klog/v2"
|
||||||
|
|
||||||
criconfig "github.com/containerd/containerd/pkg/cri/config"
|
criconfig "github.com/containerd/containerd/pkg/cri/config"
|
||||||
@ -111,24 +110,21 @@ func initCRIService(ic *plugin.InitContext) (interface{}, error) {
|
|||||||
|
|
||||||
// Set glog level.
|
// Set glog level.
|
||||||
func setGLogLevel() error {
|
func setGLogLevel() error {
|
||||||
l := logrus.GetLevel()
|
l := log.GetLevel()
|
||||||
fs := flag.NewFlagSet("klog", flag.PanicOnError)
|
fs := flag.NewFlagSet("klog", flag.PanicOnError)
|
||||||
klog.InitFlags(fs)
|
klog.InitFlags(fs)
|
||||||
if err := fs.Set("logtostderr", "true"); err != nil {
|
if err := fs.Set("logtostderr", "true"); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
switch l {
|
switch l {
|
||||||
case logrus.TraceLevel:
|
case log.TraceLevel:
|
||||||
return fs.Set("v", "5")
|
return fs.Set("v", "5")
|
||||||
case logrus.DebugLevel:
|
case log.DebugLevel:
|
||||||
return fs.Set("v", "4")
|
return fs.Set("v", "4")
|
||||||
case logrus.InfoLevel:
|
case log.InfoLevel:
|
||||||
return fs.Set("v", "2")
|
return fs.Set("v", "2")
|
||||||
// glog doesn't support following filters. Defaults to v=0.
|
default:
|
||||||
case logrus.WarnLevel:
|
// glog doesn't support other filters. Defaults to v=0.
|
||||||
case logrus.ErrorLevel:
|
|
||||||
case logrus.FatalLevel:
|
|
||||||
case logrus.PanicLevel:
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -25,8 +25,6 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
gruntime "runtime"
|
gruntime "runtime"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
"github.com/containerd/containerd/api/runtime/task/v2"
|
"github.com/containerd/containerd/api/runtime/task/v2"
|
||||||
"github.com/containerd/containerd/log"
|
"github.com/containerd/containerd/log"
|
||||||
"github.com/containerd/containerd/namespaces"
|
"github.com/containerd/containerd/namespaces"
|
||||||
@ -64,8 +62,8 @@ type binary struct {
|
|||||||
|
|
||||||
func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_ *shim, err error) {
|
func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_ *shim, err error) {
|
||||||
args := []string{"-id", b.bundle.ID}
|
args := []string{"-id", b.bundle.ID}
|
||||||
switch logrus.GetLevel() {
|
switch log.GetLevel() {
|
||||||
case logrus.DebugLevel, logrus.TraceLevel:
|
case log.DebugLevel, log.TraceLevel:
|
||||||
args = append(args, "-debug")
|
args = append(args, "-debug")
|
||||||
}
|
}
|
||||||
args = append(args, "start")
|
args = append(args, "start")
|
||||||
@ -163,8 +161,8 @@ func (b *binary) Delete(ctx context.Context) (*runtime.Exit, error) {
|
|||||||
"-id", b.bundle.ID,
|
"-id", b.bundle.ID,
|
||||||
"-bundle", b.bundle.Path,
|
"-bundle", b.bundle.Path,
|
||||||
}
|
}
|
||||||
switch logrus.GetLevel() {
|
switch log.GetLevel() {
|
||||||
case logrus.DebugLevel, logrus.TraceLevel:
|
case log.DebugLevel, log.TraceLevel:
|
||||||
args = append(args, "-debug")
|
args = append(args, "-debug")
|
||||||
}
|
}
|
||||||
args = append(args, "delete")
|
args = append(args, "delete")
|
||||||
|
Loading…
Reference in New Issue
Block a user