Skip to content

Commit f7a20e1

Browse files
mxpvthaJeztah
authored andcommitted
Move logrus setup code to log package
Signed-off-by: Maksym Pavlenko <[email protected]> (cherry picked from commit 370be0c) Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 6bf8f2c commit f7a20e1

4 files changed

Lines changed: 64 additions & 41 deletions

File tree

cmd/containerd/command/main.go

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
srvconfig "github.com/containerd/containerd/services/server/config"
3737
"github.com/containerd/containerd/sys"
3838
"github.com/containerd/containerd/version"
39-
"github.com/sirupsen/logrus"
4039
"github.com/urfave/cli"
4140
"google.golang.org/grpc/grpclog"
4241
)
@@ -150,7 +149,7 @@ can be used and modified as necessary as a custom configuration.`
150149
// Stop if we are registering or unregistering against Windows SCM.
151150
stop, err := registerUnregisterService(config.Root)
152151
if err != nil {
153-
logrus.Fatal(err)
152+
log.L.Fatal(err)
154153
}
155154
if stop {
156155
return nil
@@ -203,7 +202,7 @@ can be used and modified as necessary as a custom configuration.`
203202

204203
// Launch as a Windows Service if necessary
205204
if err := launchService(server, done); err != nil {
206-
logrus.Fatal(err)
205+
log.L.Fatal(err)
207206
}
208207
select {
209208
case <-ctx.Done():
@@ -352,11 +351,7 @@ func setLogLevel(context *cli.Context, config *srvconfig.Config) error {
352351
l = config.Debug.Level
353352
}
354353
if l != "" {
355-
lvl, err := logrus.ParseLevel(l)
356-
if err != nil {
357-
return err
358-
}
359-
logrus.SetLevel(lvl)
354+
return log.SetLevel(l)
360355
}
361356
return nil
362357
}
@@ -367,21 +362,7 @@ func setLogFormat(config *srvconfig.Config) error {
367362
f = log.TextFormat
368363
}
369364

370-
switch f {
371-
case log.TextFormat:
372-
logrus.SetFormatter(&logrus.TextFormatter{
373-
TimestampFormat: log.RFC3339NanoFixed,
374-
FullTimestamp: true,
375-
})
376-
case log.JSONFormat:
377-
logrus.SetFormatter(&logrus.JSONFormatter{
378-
TimestampFormat: log.RFC3339NanoFixed,
379-
})
380-
default:
381-
return fmt.Errorf("unknown log format: %s", f)
382-
}
383-
384-
return nil
365+
return log.SetFormat(f)
385366
}
386367

387368
func dumpStacks(writeToFile bool) {
@@ -396,7 +377,7 @@ func dumpStacks(writeToFile bool) {
396377
bufferLen *= 2
397378
}
398379
buf = buf[:stackSize]
399-
logrus.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
380+
log.L.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
400381

401382
if writeToFile {
402383
// Also write to file to aid gathering diagnostics
@@ -407,6 +388,6 @@ func dumpStacks(writeToFile bool) {
407388
}
408389
defer f.Close()
409390
f.WriteString(string(buf))
410-
logrus.Infof("goroutine stack dump written to %s", name)
391+
log.L.Infof("goroutine stack dump written to %s", name)
411392
}
412393
}

log/context.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package log
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"github.com/sirupsen/logrus"
2324
)
@@ -38,6 +39,9 @@ type (
3839

3940
// Fields type to pass to `WithFields`, alias from `logrus`.
4041
Fields = logrus.Fields
42+
43+
// Level is a logging level
44+
Level = logrus.Level
4145
)
4246

4347
const (
@@ -50,8 +54,52 @@ const (
5054

5155
// JSONFormat represents the JSON logging format
5256
JSONFormat = "json"
57+
58+
// TraceLevel level.
59+
TraceLevel = logrus.TraceLevel
60+
61+
// DebugLevel level.
62+
DebugLevel = logrus.DebugLevel
63+
64+
// InfoLevel level.
65+
InfoLevel = logrus.InfoLevel
5366
)
5467

68+
// SetLevel sets log level globally.
69+
func SetLevel(level string) error {
70+
lvl, err := logrus.ParseLevel(level)
71+
if err != nil {
72+
return err
73+
}
74+
75+
logrus.SetLevel(lvl)
76+
return nil
77+
}
78+
79+
// GetLevel returns the current log level.
80+
func GetLevel() Level {
81+
return logrus.GetLevel()
82+
}
83+
84+
// SetFormat sets log output format
85+
func SetFormat(format string) error {
86+
switch format {
87+
case TextFormat:
88+
logrus.SetFormatter(&logrus.TextFormatter{
89+
TimestampFormat: RFC3339NanoFixed,
90+
FullTimestamp: true,
91+
})
92+
case JSONFormat:
93+
logrus.SetFormatter(&logrus.JSONFormatter{
94+
TimestampFormat: RFC3339NanoFixed,
95+
})
96+
default:
97+
return fmt.Errorf("unknown log format: %s", format)
98+
}
99+
100+
return nil
101+
}
102+
55103
// WithLogger returns a new context with the provided logger. Use in
56104
// combination with logger.WithField(s) for great effect.
57105
func WithLogger(ctx context.Context, logger *logrus.Entry) context.Context {

pkg/cri/cri.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/containerd/containerd/platforms"
3131
"github.com/containerd/containerd/plugin"
3232
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
33-
"github.com/sirupsen/logrus"
3433
"k8s.io/klog/v2"
3534

3635
criconfig "github.com/containerd/containerd/pkg/cri/config"
@@ -112,24 +111,21 @@ func initCRIService(ic *plugin.InitContext) (interface{}, error) {
112111

113112
// Set glog level.
114113
func setGLogLevel() error {
115-
l := logrus.GetLevel()
114+
l := log.GetLevel()
116115
fs := flag.NewFlagSet("klog", flag.PanicOnError)
117116
klog.InitFlags(fs)
118117
if err := fs.Set("logtostderr", "true"); err != nil {
119118
return err
120119
}
121120
switch l {
122-
case logrus.TraceLevel:
121+
case log.TraceLevel:
123122
return fs.Set("v", "5")
124-
case logrus.DebugLevel:
123+
case log.DebugLevel:
125124
return fs.Set("v", "4")
126-
case logrus.InfoLevel:
125+
case log.InfoLevel:
127126
return fs.Set("v", "2")
128-
// glog doesn't support following filters. Defaults to v=0.
129-
case logrus.WarnLevel:
130-
case logrus.ErrorLevel:
131-
case logrus.FatalLevel:
132-
case logrus.PanicLevel:
127+
default:
128+
// glog doesn't support other filters. Defaults to v=0.
133129
}
134130
return nil
135131
}

runtime/v2/binary.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ import (
2525
"path/filepath"
2626
gruntime "runtime"
2727

28-
"github.com/sirupsen/logrus"
29-
3028
"github.com/containerd/containerd/api/runtime/task/v2"
3129
"github.com/containerd/containerd/log"
3230
"github.com/containerd/containerd/namespaces"
@@ -64,8 +62,8 @@ type binary struct {
6462

6563
func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_ *shim, err error) {
6664
args := []string{"-id", b.bundle.ID}
67-
switch logrus.GetLevel() {
68-
case logrus.DebugLevel, logrus.TraceLevel:
65+
switch log.GetLevel() {
66+
case log.DebugLevel, log.TraceLevel:
6967
args = append(args, "-debug")
7068
}
7169
args = append(args, "start")
@@ -163,8 +161,8 @@ func (b *binary) Delete(ctx context.Context) (*runtime.Exit, error) {
163161
"-id", b.bundle.ID,
164162
"-bundle", b.bundle.Path,
165163
}
166-
switch logrus.GetLevel() {
167-
case logrus.DebugLevel, logrus.TraceLevel:
164+
switch log.GetLevel() {
165+
case log.DebugLevel, log.TraceLevel:
168166
args = append(args, "-debug")
169167
}
170168
args = append(args, "delete")

0 commit comments

Comments
 (0)