From 14bc27f1789a3f29b47dc71b1fbed7d529ec0c82 Mon Sep 17 00:00:00 2001 From: Jared Wasinger Date: Thu, 9 Nov 2023 19:38:13 +0800 Subject: [PATCH] pass handler attrs to TerminalFormat as a parameter instead of adding them to record. add unit test for this --- log/logger_test.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/log/logger_test.go b/log/logger_test.go index ffed73ba48..bdf4a51eb4 100644 --- a/log/logger_test.go +++ b/log/logger_test.go @@ -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()