Skip to content

Commit 3fa538d

Browse files
test(log): add TestNamedLoggerContext to verify logger= field in logfmt output
Adds two unit tests in pkg/log/zap_test.go that prove the named logger context is correctly emitted as a logger= field in logfmt output: TestNamedLoggerContext — mirrors create.go line 194 Logger().Named("retina-capture-create") TestNamedLoggerContextDeleteCmd — mirrors delete.go line 36 Logger().Named("retina-capture-delete") Each test builds a buffer-backed logfmt core (matching the production encoder config, which sets NameKey = "logger"), stores it as the global, calls Named(), logs a message, then asserts: logger=retina-capture-create (or retina-capture-delete) is present in the captured output. This directly addresses the review request in #2144 to demonstrate the named logger context. Signed-off-by: mail2sudheerobbu-oss <mail2sudheerobbu@gmail.com>
1 parent c90b61d commit 3fa538d

1 file changed

Lines changed: 57 additions & 0 deletions

File tree

pkg/log/zap_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
package log
44

55
import (
6+
"bytes"
67
"log/slog"
78
"os"
89
"path/filepath"
910
"strings"
1011
"testing"
1112

13+
logfmt "github.com/jsternberg/zap-logfmt"
1214
"github.com/stretchr/testify/assert"
1315
"github.com/stretchr/testify/require"
1416
"go.uber.org/zap"
17+
"go.uber.org/zap/zapcore"
1518
)
1619

1720
func TestLogFileRotation(t *testing.T) {
@@ -117,3 +120,57 @@ func TestLogrLoggerFallback(t *testing.T) {
117120
// Should not panic even without zap being setup
118121
logrLogger.Info("fallback logr test message", "key", "value")
119122
}
123+
124+
func TestNamedLoggerContext(t *testing.T) {
125+
resetGlobalForTest()
126+
defer resetGlobalForTest()
127+
128+
// Build a logfmt core that writes to a buffer so we can inspect the output.
129+
// This mirrors how SetupZapLogger configures the stdout core, but directed
130+
// to a bytes.Buffer instead of os.Stdout.
131+
var buf bytes.Buffer
132+
encoderCfg := EncoderConfig() // same encoder config as production (NameKey = "logger")
133+
core := zapcore.NewCore(
134+
logfmt.NewEncoder(encoderCfg),
135+
zapcore.AddSync(&buf),
136+
zapcore.InfoLevel,
137+
)
138+
global.Store(&ZapLogger{Logger: zap.New(core)})
139+
140+
// This is the exact call made in cli/cmd/capture/create.go RunE (line 194).
141+
namedLogger := Logger().Named("retina-capture-create")
142+
namedLogger.Info("packet capture started", zap.String("namespace", "kube-system"))
143+
144+
output := buf.String()
145+
t.Logf("logfmt output: %s", output)
146+
147+
// The zap ProductionEncoderConfig sets NameKey = "logger", so Named() injects
148+
// a logger= field into every logfmt line emitted by the child logger.
149+
assert.Contains(t, output, "logger=retina-capture-create",
150+
"Named logger context must appear as logger= field in logfmt output")
151+
assert.Contains(t, output, `msg="packet capture started"`)
152+
assert.Contains(t, output, "namespace=kube-system")
153+
}
154+
155+
func TestNamedLoggerContextDeleteCmd(t *testing.T) {
156+
resetGlobalForTest()
157+
defer resetGlobalForTest()
158+
159+
var buf bytes.Buffer
160+
encoderCfg := EncoderConfig()
161+
core := zapcore.NewCore(
162+
logfmt.NewEncoder(encoderCfg),
163+
zapcore.AddSync(&buf),
164+
zapcore.InfoLevel,
165+
)
166+
global.Store(&ZapLogger{Logger: zap.New(core)})
167+
168+
// Mirrors cli/cmd/capture/delete.go RunE.
169+
namedLogger := Logger().Named("retina-capture-delete")
170+
namedLogger.Info("deleting capture", zap.String("namespace", "default"), zap.String("name", "my-capture"))
171+
172+
output := buf.String()
173+
t.Logf("logfmt output: %s", output)
174+
assert.Contains(t, output, "logger=retina-capture-delete")
175+
assert.Contains(t, output, `msg="deleting capture"`)
176+
}

0 commit comments

Comments
 (0)