pass handler attrs to TerminalFormat as a parameter instead of adding them to record. add unit test for this

This commit is contained in:
Jared Wasinger 2023-11-09 19:38:13 +08:00
parent dad6ef06fe
commit 14bc27f178

View file

@ -5,6 +5,8 @@ import (
"os"
"strings"
"testing"
"golang.org/x/exp/slog"
)
// TestLoggingWithVmodule checks that vmodule works.
@ -26,6 +28,22 @@ func TestLoggingWithVmodule(t *testing.T) {
}
}
func TestTerminalHandlerWithAttrs(t *testing.T) {
out := new(bytes.Buffer)
glog := NewGlogHandler(TerminalHandlerWithLevel(out, LevelTrace, false).WithAttrs([]slog.Attr{slog.String("baz", "bat")}))
glog.Verbosity(LevelTrace)
logger := NewLogger(glog)
logger.Trace("a message", "foo", "bar")
have := out.String()
// The timestamp is locale-dependent, so we want to trim that off
// "INFO [01-01|00:00:00.000] a messag ..." -> "a messag..."
have = strings.Split(have, "]")[1]
want := " a message baz=bat foo=bar\n"
if have != want {
t.Errorf("\nhave: %q\nwant: %q\n", have, want)
}
}
func BenchmarkTraceLogging(b *testing.B) {
SetDefault(NewLogger(TerminalHandler(os.Stderr, true)))
b.ResetTimer()