Skip to content

Commit

Permalink
slogutil: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Sep 27, 2024
1 parent c9ee827 commit 8e07d8a
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions logutil/slogutil/verbosity.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,24 @@ import (
"github.com/AdguardTeam/golibs/errors"
)

// Verbosity represents the acceptable log verbosity levels.
type Verbosity uint8

// Valid verbosity levels.
// Acceptable [slog.Level] levels.
const (
VerbosityInfo Verbosity = 0
VerbosityDebug Verbosity = 1
VerbosityTrace Verbosity = 2
LevelTrace slog.Level = -8
LevelDebug slog.Level = -4
LevelInfo slog.Level = 0
LevelWarn slog.Level = 4
LevelError slog.Level = 8
)

// LevelTrace is the [slog.Level] used for trace messages.
const LevelTrace = -8

// VerbosityToLevel returns [slog.Level] for given verbosity.
func VerbosityToLevel(l uint8) (lvl slog.Level, err error) {
switch v := Verbosity(l); v {
case VerbosityInfo:
return slog.LevelInfo, nil
case VerbosityDebug:
return slog.LevelDebug, nil
case VerbosityTrace:
return slog.Level(LevelTrace), nil
switch l {
case 0:
return LevelInfo, nil
case 1:
return LevelDebug, nil
case 2:
return LevelTrace, nil
default:
return lvl, fmt.Errorf("verbosity: %w: %d", errors.ErrBadEnumValue, l)
}
Expand Down

0 comments on commit 8e07d8a

Please sign in to comment.