Skip to content

Commit

Permalink
slogutil: trace
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Sep 26, 2024
1 parent fcc1b72 commit c9ee827
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
2 changes: 1 addition & 1 deletion logutil/slogutil/defer_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func ExampleCloseAndLog() {
ctx := context.Background()
l := slogutil.New(&slogutil.Config{
Verbose: true,
Level: slog.LevelDebug,
})

func() {
Expand Down
12 changes: 4 additions & 8 deletions logutil/slogutil/slogutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ type Config struct {
// If set, it must be valid.
Format Format

// Level reports the minimum record level that will be logged.
Level slog.Level

// AddTimestamp, if true, adds a timestamp to every record.
AddTimestamp bool

// Verbose, if true, enables verbose logging.
Verbose bool
}

// New creates a slog logger with the given parameters. If c is nil, the
Expand All @@ -49,11 +49,7 @@ func New(c *Config) (l *slog.Logger) {
}
}

lvl := slog.LevelInfo
if c.Verbose {
lvl = slog.LevelDebug
}

lvl := c.Level
format := cmp.Or(c.Format, FormatDefault)
output := cmp.Or[io.Writer](c.Output, os.Stdout)
if format == FormatDefault {
Expand Down
25 changes: 20 additions & 5 deletions logutil/slogutil/slogutil_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func ExampleNew_default() {
l := slogutil.New(&slogutil.Config{
Verbose: true,
Level: slog.LevelDebug,
})

l.Info("test info")
Expand All @@ -22,8 +22,8 @@ func ExampleNew_default() {

func ExampleNew_json() {
l := slogutil.New(&slogutil.Config{
Format: slogutil.FormatJSON,
Verbose: true,
Format: slogutil.FormatJSON,
Level: slog.LevelDebug,
})

l.Info("test info")
Expand All @@ -41,8 +41,8 @@ func ExampleNew_json() {

func ExampleNew_text() {
l := slogutil.New(&slogutil.Config{
Format: slogutil.FormatText,
Verbose: true,
Format: slogutil.FormatText,
Level: slog.LevelDebug,
})

l.Info("test info")
Expand Down Expand Up @@ -72,3 +72,18 @@ This is a very long text with many lines.`
// INFO my text line_num=2 line=""
// INFO my text line_num=3 line="This is a very long text with many lines."
}

func ExampleNew_trace() {
l := slogutil.New(&slogutil.Config{
Level: slogutil.LevelTrace,
})

l.Log(context.Background(), slogutil.LevelTrace, "test trace")
l.Info("test info")
l.Debug("test debug")

// Output:
// DEBUG-4 test trace
// INFO test info
// DEBUG test debug
}
35 changes: 35 additions & 0 deletions logutil/slogutil/verbosity.go
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)
}
}

0 comments on commit c9ee827

Please sign in to comment.