Skip to content

Commit 7fbd5dc

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 14b5c66 commit 7fbd5dc

4 files changed

Lines changed: 62 additions & 37 deletions

File tree

cmd/containerd/command/main.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ can be used and modified as necessary as a custom configuration.`
141141
// Stop if we are registering or unregistering against Windows SCM.
142142
stop, err := registerUnregisterService(config.Root)
143143
if err != nil {
144-
logrus.Fatal(err)
144+
log.L.Fatal(err)
145145
}
146146
if stop {
147147
return nil
@@ -203,7 +203,7 @@ can be used and modified as necessary as a custom configuration.`
203203

204204
// Launch as a Windows Service if necessary
205205
if err := launchService(server, done); err != nil {
206-
logrus.Fatal(err)
206+
log.L.Fatal(err)
207207
}
208208
select {
209209
case <-ctx.Done():
@@ -346,11 +346,7 @@ func setLogLevel(context *cli.Context, config *srvconfig.Config) error {
346346
l = config.Debug.Level
347347
}
348348
if l != "" {
349-
lvl, err := logrus.ParseLevel(l)
350-
if err != nil {
351-
return err
352-
}
353-
logrus.SetLevel(lvl)
349+
return log.SetLevel(l)
354350
}
355351
return nil
356352
}
@@ -361,21 +357,7 @@ func setLogFormat(config *srvconfig.Config) error {
361357
f = log.TextFormat
362358
}
363359

364-
switch f {
365-
case log.TextFormat:
366-
logrus.SetFormatter(&logrus.TextFormatter{
367-
TimestampFormat: log.RFC3339NanoFixed,
368-
FullTimestamp: true,
369-
})
370-
case log.JSONFormat:
371-
logrus.SetFormatter(&logrus.JSONFormatter{
372-
TimestampFormat: log.RFC3339NanoFixed,
373-
})
374-
default:
375-
return fmt.Errorf("unknown log format: %s", f)
376-
}
377-
378-
return nil
360+
return log.SetFormat(f)
379361
}
380362

381363
func setLogHooks() {
@@ -394,7 +376,7 @@ func dumpStacks(writeToFile bool) {
394376
bufferLen *= 2
395377
}
396378
buf = buf[:stackSize]
397-
logrus.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
379+
log.L.Infof("=== BEGIN goroutine stack dump ===\n%s\n=== END goroutine stack dump ===", buf)
398380

399381
if writeToFile {
400382
// Also write to file to aid gathering diagnostics
@@ -405,6 +387,6 @@ func dumpStacks(writeToFile bool) {
405387
}
406388
defer f.Close()
407389
f.WriteString(string(buf))
408-
logrus.Infof("goroutine stack dump written to %s", name)
390+
log.L.Infof("goroutine stack dump written to %s", name)
409391
}
410392
}

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
@@ -36,7 +36,6 @@ import (
3636
"github.com/containerd/containerd/services"
3737
"github.com/containerd/containerd/snapshots"
3838
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
39-
"github.com/sirupsen/logrus"
4039
"k8s.io/klog/v2"
4140

4241
criconfig "github.com/containerd/containerd/pkg/cri/config"
@@ -174,24 +173,21 @@ func getServicesOpts(ic *plugin.InitContext) ([]containerd.ServicesOpt, error) {
174173

175174
// Set glog level.
176175
func setGLogLevel() error {
177-
l := logrus.GetLevel()
176+
l := log.GetLevel()
178177
fs := flag.NewFlagSet("klog", flag.PanicOnError)
179178
klog.InitFlags(fs)
180179
if err := fs.Set("logtostderr", "true"); err != nil {
181180
return err
182181
}
183182
switch l {
184-
case logrus.TraceLevel:
183+
case log.TraceLevel:
185184
return fs.Set("v", "5")
186-
case logrus.DebugLevel:
185+
case log.DebugLevel:
187186
return fs.Set("v", "4")
188-
case logrus.InfoLevel:
187+
case log.InfoLevel:
189188
return fs.Set("v", "2")
190-
// glog doesn't support following filters. Defaults to v=0.
191-
case logrus.WarnLevel:
192-
case logrus.ErrorLevel:
193-
case logrus.FatalLevel:
194-
case logrus.PanicLevel:
189+
default:
190+
// glog doesn't support other filters. Defaults to v=0.
195191
}
196192
return nil
197193
}

runtime/v2/binary.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import (
3333
"github.com/containerd/containerd/runtime/v2/task"
3434
"github.com/containerd/ttrpc"
3535
"github.com/gogo/protobuf/types"
36-
"github.com/sirupsen/logrus"
3736
)
3837

3938
type shimBinaryConfig struct {
@@ -63,8 +62,8 @@ type binary struct {
6362

6463
func (b *binary) Start(ctx context.Context, opts *types.Any, onClose func()) (_ *shim, err error) {
6564
args := []string{"-id", b.bundle.ID}
66-
switch logrus.GetLevel() {
67-
case logrus.DebugLevel, logrus.TraceLevel:
65+
switch log.GetLevel() {
66+
case log.DebugLevel, log.TraceLevel:
6867
args = append(args, "-debug")
6968
}
7069
args = append(args, "start")

0 commit comments

Comments
 (0)