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