@@ -12,6 +12,7 @@ package log
1212// ... existing code ...
1313
1414import (
15+ "strings"
1516 "testing"
1617
1718 "go.uber.org/zap/zapcore"
@@ -42,26 +43,47 @@ func TestSetLevel(t *testing.T) {
4243 }
4344}
4445
45- // TestTracef makes sure Tracef forwards the call to the underlying
46- // logger at the debug level. The test installs a stub logger that
47- // records the last format string received and asserts that Tracef
48- // prefixes the message with the required tag.
49- func TestTracef (t * testing.T ) {
50- var recorded string
51- stub := & stubLogger {debugf : func (format string , _ ... any ) {
52- recorded = format
53- }}
46+ // TestTraceDisabledByDefault ensures trace logging starts disabled and Tracef is a no-op.
47+ func TestTraceDisabledByDefault (t * testing.T ) {
48+ stub := & stubLogger {}
49+ oldDefault := Default
50+ oldTrace := traceEnabled
51+ Default = stub
52+ t .Cleanup (func () {
53+ Default = oldDefault
54+ traceEnabled = oldTrace
55+ })
56+
57+ if traceEnabled {
58+ t .Fatalf ("traceEnabled should be false by default" )
59+ }
60+
61+ Tracef ("hello %s" , "world" )
5462
55- // Replace Default logger with stub and restore afterwards.
56- old := Default
63+ if stub .debugfCalls != 0 {
64+ t .Fatalf ("Tracef should not log when trace is disabled; got %d calls" , stub .debugfCalls )
65+ }
66+ }
67+
68+ // TestTracefEnabled makes sure Tracef forwards the call when trace is enabled.
69+ func TestTracefEnabled (t * testing.T ) {
70+ stub := & stubLogger {}
71+ oldDefault := Default
72+ oldTrace := traceEnabled
5773 Default = stub
58- defer func () { Default = old }()
74+ SetTraceEnabled (true )
75+ t .Cleanup (func () {
76+ Default = oldDefault
77+ traceEnabled = oldTrace
78+ })
5979
6080 Tracef ("hello %s" , "world" )
6181
62- wantPrefix := "[TRACE] "
63- if recorded == "" || recorded [:len (wantPrefix )] != wantPrefix {
64- t .Fatalf ("Tracef did not prefix message with %q: got %q" , wantPrefix , recorded )
82+ if stub .debugfCalls != 1 {
83+ t .Fatalf ("Tracef should log once when trace is enabled; got %d calls" , stub .debugfCalls )
84+ }
85+ if ! strings .HasPrefix (stub .lastFormat , "[TRACE] " ) {
86+ t .Fatalf ("Tracef did not prefix message with \" [TRACE] \" : got %q" , stub .lastFormat )
6587 }
6688}
6789
@@ -70,11 +92,15 @@ func TestTracef(t *testing.T) {
7092// Only the methods required by the tests are implemented; the rest
7193// are no-ops to satisfy the interface.
7294type stubLogger struct {
73- debugf func (format string , args ... any )
95+ lastFormat string
96+ debugfCalls int
7497}
7598
76- func (s * stubLogger ) Debug (args ... any ) {}
77- func (s * stubLogger ) Debugf (format string , args ... any ) { s .debugf (format , args ... ) }
99+ func (s * stubLogger ) Debug (args ... any ) {}
100+ func (s * stubLogger ) Debugf (format string , args ... any ) {
101+ s .debugfCalls ++
102+ s .lastFormat = format
103+ }
78104func (s * stubLogger ) Info (args ... any ) {}
79105func (s * stubLogger ) Infof (format string , args ... any ) {}
80106func (s * stubLogger ) Warn (args ... any ) {}
0 commit comments