-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package slogutil | ||
|
||
import ( | ||
"fmt" | ||
"log/slog" | ||
|
||
"github.com/AdguardTeam/golibs/errors" | ||
) | ||
|
||
// Verbosity represents the acceptable log verbosity levels. | ||
type Verbosity uint8 | ||
|
||
// Valid verbosity levels. | ||
const ( | ||
VerbosityInfo Verbosity = 0 | ||
VerbosityDebug Verbosity = 1 | ||
VerbosityTrace Verbosity = 2 | ||
) | ||
|
||
// 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 | ||
default: | ||
return lvl, fmt.Errorf("verbosity: %w: %d", errors.ErrBadEnumValue, l) | ||
} | ||
} |