Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 24 additions & 7 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,16 @@ const (
PanicLevel Level = logrus.PanicLevel
)

// SetLevel sets log level globally. It returns an error if the given
// level is not supported.
// levelValue is a log level accepted by [SetLevel].
type levelValue interface {
string | Level
}

// SetLevel sets the global log level.
//
// The level may be specified as a string or a [Level].
//
// level can be one of:
// String levels are parsed using [logrus.ParseLevel] and may be one of:
//
// - "trace" ([TraceLevel])
// - "debug" ([DebugLevel])
Expand All @@ -121,10 +127,21 @@ const (
// - "error" ([ErrorLevel])
// - "fatal" ([FatalLevel])
// - "panic" ([PanicLevel])
func SetLevel(level string) error {
lvl, err := logrus.ParseLevel(level)
if err != nil {
return err
//
// SetLevel returns an error if a string level is not supported.
func SetLevel[T levelValue](level T) error {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My only concern is that now it's impossible to assign this to a function-value without instantiation.

So if someone depends on passing log.SetLevel anywhere - it will fail now

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, can probably still pass it as log.SetLevel[string].

I doubt it's used like that, but I can check if there's any cases where it'd break (but also, ultimately, this whole module was meant to be for transitioning, so it should only be relevant to existing uses in containerd, moby 🤔)

var lvl Level

switch l := any(level).(type) {
case string:
var err error
lvl, err = logrus.ParseLevel(l)
if err != nil {
return err
}

case Level:
lvl = l
}

L.Logger.SetLevel(lvl)
Expand Down
66 changes: 66 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,69 @@ func TestCompat(t *testing.T) {
t.Errorf("expected: (%[1]T) %+[1]v, got: (%[2]T) %+[2]v", expected, l2.Data)
}
}

func TestSetLevel(t *testing.T) {
oldLevel := L.Logger.GetLevel()
t.Cleanup(func() {
L.Logger.SetLevel(oldLevel)
})

tests := []struct {
str string
level Level
}{
{
str: "trace",
level: TraceLevel,
},
{
str: "debug",
level: DebugLevel,
},
{
str: "info",
level: InfoLevel,
},
{
str: "warn",
level: WarnLevel,
},
{
str: "error",
level: ErrorLevel,
},
{
str: "fatal",
level: FatalLevel,
},
{
str: "panic",
level: PanicLevel,
},
}

for _, tc := range tests {
t.Run(tc.str, func(t *testing.T) {
inputs := []struct {
doc string
set func() error
}{
{doc: "string", set: func() error { return SetLevel(tc.str) }},
{doc: "Level", set: func() error { return SetLevel(tc.level) }},
}

for _, input := range inputs {
t.Run(input.doc, func(t *testing.T) {
L.Logger.SetLevel(InfoLevel)

if err := input.set(); err != nil {
t.Fatal(err)
}
if got := L.Logger.GetLevel(); got != tc.level {
t.Fatalf("got %v, want %v", got, tc.level)
}
})
}
})
}
}
Loading