Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(zerolog): support parse level #47

Merged
merged 1 commit into from
Feb 13, 2023
Merged
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
6 changes: 6 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,9 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
}
}
}

// ParseLevel converts a level string into a zerolog Level value.
// returns an error if the input string does not match known values.
func ParseLevel(levelStr string) (zerolog.Level, error) {
return zerolog.ParseLevel(levelStr)
}
37 changes: 37 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,40 @@ func TestLoggerWithLevels(t *testing.T) {
performRequest(r, "PUT", "/example?a=100")
assert.Contains(t, buffer.String(), "FTL")
}

func TestLoggerParseLevel(t *testing.T) {
type args struct {
levelStr string
}
tests := []struct {
name string
args args
want zerolog.Level
wantErr bool
}{
{"trace", args{"trace"}, zerolog.TraceLevel, false},
{"debug", args{"debug"}, zerolog.DebugLevel, false},
{"info", args{"info"}, zerolog.InfoLevel, false},
{"warn", args{"warn"}, zerolog.WarnLevel, false},
{"error", args{"error"}, zerolog.ErrorLevel, false},
{"fatal", args{"fatal"}, zerolog.FatalLevel, false},
{"panic", args{"panic"}, zerolog.PanicLevel, false},
{"disabled", args{"disabled"}, zerolog.Disabled, false},
{"nolevel", args{""}, zerolog.NoLevel, false},
{"-1", args{"-1"}, zerolog.TraceLevel, false},
{"-2", args{"-2"}, zerolog.Level(-2), false},
{"-3", args{"-3"}, zerolog.Level(-3), false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := ParseLevel(tt.args.levelStr)
if (err != nil) != tt.wantErr {
t.Errorf("ParseLevel() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("ParseLevel() got = %v, want %v", got, tt.want)
}
})
}
}