Skip to content

Commit

Permalink
Pull request: AGDNS-2441-trace-log
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 444cf5e
Author: Dimitry Kolyshev <[email protected]>
Date:   Tue Oct 1 15:49:52 2024 +0300

    slogutil: imp code

commit e1e6a7c
Author: Dimitry Kolyshev <[email protected]>
Date:   Tue Oct 1 15:45:49 2024 +0300

    slogutil: imp code

commit e9e5ef0
Author: Dimitry Kolyshev <[email protected]>
Date:   Tue Oct 1 15:21:42 2024 +0300

    slogutil: imp code

commit f3def15
Author: Dimitry Kolyshev <[email protected]>
Date:   Tue Oct 1 15:15:20 2024 +0300

    slogutil: default

commit a79518e
Author: Dimitry Kolyshev <[email protected]>
Date:   Tue Oct 1 15:09:52 2024 +0300

    all: legacy trace log

commit 86e7acf
Author: Dimitry Kolyshev <[email protected]>
Date:   Mon Sep 30 10:58:08 2024 +0300

    all: legacy trace log

commit 954d7d2
Author: Dimitry Kolyshev <[email protected]>
Date:   Mon Sep 30 10:09:29 2024 +0300

    slogutil: imp code

commit e9b9a24
Author: Dimitry Kolyshev <[email protected]>
Date:   Fri Sep 27 11:32:24 2024 +0700

    slogutil: replace level name

commit 1aa7a3a
Author: Dimitry Kolyshev <[email protected]>
Date:   Fri Sep 27 09:54:03 2024 +0700

    slogutil: imp code

commit 8e07d8a
Author: Dimitry Kolyshev <[email protected]>
Date:   Fri Sep 27 08:16:27 2024 +0700

    slogutil: imp code

commit c9ee827
Author: Dimitry Kolyshev <[email protected]>
Date:   Thu Sep 26 15:09:44 2024 +0700

    slogutil: trace
  • Loading branch information
Mizzick committed Oct 1, 2024
1 parent fcc1b72 commit 72234b6
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 18 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
2 changes: 2 additions & 0 deletions logutil/slogutil/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ type logFunction func(format string, args ...any)
// returned.
func logFuncForLevel(lvl slog.Level) (logFunc logFunction, warnStr string, err error) {
switch lvl {
case LevelTrace:
return aglog.Debug, "trace: ", nil
case slog.LevelDebug:
return aglog.Debug, "", nil
case slog.LevelInfo:
Expand Down
51 changes: 39 additions & 12 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 is 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,22 +49,15 @@ 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 {
// Fast path for the default handler.
return newDefault(output, lvl, c.AddTimestamp)
}

var replaceAttr func(groups []string, a slog.Attr) (res slog.Attr)
if !c.AddTimestamp {
replaceAttr = RemoveTime
}
replaceAttr := newReplaceAttr(!c.AddTimestamp)

var h slog.Handler
switch format {
Expand Down Expand Up @@ -95,6 +88,8 @@ func New(c *Config) (l *slog.Logger) {
}

// newDefault returns a new default slog logger set up with the given options.
//
// TODO(d.kolyshev): Replace log level name for [LevelTrace].
func newDefault(output io.Writer, lvl slog.Level, addTimestamp bool) (l *slog.Logger) {
h := NewLevelHandler(lvl, slog.Default().Handler())
log.SetOutput(output)
Expand All @@ -107,6 +102,38 @@ func newDefault(output io.Writer, lvl slog.Level, addTimestamp bool) (l *slog.Lo
return slog.New(h)
}

// newReplaceAttr is a function that returns [slog.HandlerOptions.ReplaceAttr]
// function for provided parameters.
func newReplaceAttr(removeTime bool) func(groups []string, a slog.Attr) (res slog.Attr) {
if !removeTime {
return ReplaceLevel
}

return func(groups []string, a slog.Attr) (res slog.Attr) {
return ReplaceLevel(groups, RemoveTime(groups, a))
}
}

// traceAttrValue is a [LevelTrace] value under the [slog.LevelKey] key.
var traceAttrValue = slog.StringValue("TRACE")

// ReplaceLevel is a function for [slog.HandlerOptions.ReplaceAttr] that adds
// [LevelTrace] custom name for level attribute.
func ReplaceLevel(groups []string, a slog.Attr) (res slog.Attr) {
if len(groups) > 0 {
return a
}

if a.Key == slog.LevelKey {
lvl := a.Value.Any().(slog.Level)
if lvl == LevelTrace {
a.Value = traceAttrValue
}
}

return a
}

// RemoveTime is a function for [slog.HandlerOptions.ReplaceAttr] that removes
// the "time" attribute.
func RemoveTime(groups []string, a slog.Attr) (res slog.Attr) {
Expand Down
26 changes: 21 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,19 @@ 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{
Format: slogutil.FormatText,
Level: slogutil.LevelTrace,
})

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

// Output:
// level=TRACE msg="test trace"
// level=INFO msg="test info"
// level=DEBUG msg="test debug"
}
31 changes: 31 additions & 0 deletions logutil/slogutil/verbosity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package slogutil

import (
"fmt"
"log/slog"

"github.com/AdguardTeam/golibs/errors"
)

// Acceptable [slog.Level] levels.
const (
LevelTrace = slog.Level(-8)
LevelDebug = slog.LevelDebug
LevelInfo = slog.LevelInfo
LevelWarn = slog.LevelWarn
LevelError = slog.LevelError
)

// VerbosityToLevel returns log level for given verbosity.
func VerbosityToLevel(l uint8) (lvl slog.Level, err error) {
switch l {
case 0:
return LevelInfo, nil
case 1:
return LevelDebug, nil
case 2:
return LevelTrace, nil
default:
return lvl, fmt.Errorf("%w: %d", errors.ErrBadEnumValue, l)
}
}

0 comments on commit 72234b6

Please sign in to comment.