2023-09-07 15:48:49 +03:00
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2023-11-29 10:33:50 +03:00
|
|
|
|
|
|
|
"golang.org/x/exp/slog"
|
2023-09-07 15:48:49 +03:00
|
|
|
)
|
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
// TestLoggingWithVmodule checks that vmodule works.
|
|
|
|
func TestLoggingWithVmodule(t *testing.T) {
|
2023-09-07 15:48:49 +03:00
|
|
|
out := new(bytes.Buffer)
|
2023-11-29 10:33:50 +03:00
|
|
|
glog := NewGlogHandler(NewTerminalHandlerWithLevel(out, LevelTrace, false))
|
|
|
|
glog.Verbosity(LevelCrit)
|
|
|
|
logger := NewLogger(glog)
|
|
|
|
logger.Warn("This should not be seen", "ignored", "true")
|
|
|
|
glog.Vmodule("logger_test.go=5")
|
|
|
|
logger.Trace("a message", "foo", "bar")
|
2023-09-07 15:48:49 +03:00
|
|
|
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]
|
2023-11-29 10:33:50 +03:00
|
|
|
want := " a message foo=bar\n"
|
|
|
|
if have != want {
|
|
|
|
t.Errorf("\nhave: %q\nwant: %q\n", have, want)
|
2023-09-07 15:48:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-29 10:33:50 +03:00
|
|
|
func TestTerminalHandlerWithAttrs(t *testing.T) {
|
2023-09-07 15:48:49 +03:00
|
|
|
out := new(bytes.Buffer)
|
2023-11-29 10:33:50 +03:00
|
|
|
glog := NewGlogHandler(NewTerminalHandlerWithLevel(out, LevelTrace, false).WithAttrs([]slog.Attr{slog.String("baz", "bat")}))
|
|
|
|
glog.Verbosity(LevelTrace)
|
|
|
|
logger := NewLogger(glog)
|
2023-09-07 15:48:49 +03:00
|
|
|
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]
|
2023-11-29 10:33:50 +03:00
|
|
|
want := " a message baz=bat foo=bar\n"
|
2023-09-07 15:48:49 +03:00
|
|
|
if have != want {
|
|
|
|
t.Errorf("\nhave: %q\nwant: %q\n", have, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkTraceLogging(b *testing.B) {
|
2023-11-29 10:33:50 +03:00
|
|
|
SetDefault(NewLogger(NewTerminalHandler(os.Stderr, true)))
|
2023-09-07 15:48:49 +03:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
Trace("a message", "v", i)
|
|
|
|
}
|
|
|
|
}
|