Skip to content

Commit

Permalink
fix: assign sugar logger name. (#4144)
Browse files Browse the repository at this point in the history
Signed-off-by: qicz <[email protected]>
Co-authored-by: zirain <[email protected]>
  • Loading branch information
qicz and zirain authored Sep 4, 2024
1 parent 07a6ca4 commit b50f5fa
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion internal/logging/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (l Logger) WithName(name string) Logger {
return Logger{
Logger: zapr.NewLogger(logger).WithName(name),
logging: l.logging,
sugaredLogger: logger.Sugar(),
sugaredLogger: logger.Sugar().Named(name),
}
}

Expand Down
29 changes: 29 additions & 0 deletions internal/logging/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,32 @@ func TestLoggerWithName(t *testing.T) {
assert.Contains(t, capturedOutput, "info message")
assert.Contains(t, capturedOutput, "debug message")
}

func TestLoggerSugarName(t *testing.T) {
originalStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

defer func() {
// Restore the original stdout and close the pipe
os.Stdout = originalStdout
err := w.Close()
require.NoError(t, err)
}()

const logName = "loggerName"

config := egv1a1.DefaultEnvoyGatewayLogging()
config.Level[logName] = egv1a1.LogLevelDebug

logger := NewLogger(config).WithName(logName)

logger.Sugar().Debugf("debugging message")

// Read from the pipe (captured stdout)
outputBytes := make([]byte, 200)
_, err := r.Read(outputBytes)
require.NoError(t, err)
capturedOutput := string(outputBytes)
assert.Contains(t, capturedOutput, "debugging message", logName)
}

0 comments on commit b50f5fa

Please sign in to comment.