From 2406f6856d0e1eb9e1b2a70f344fd14fc76bd5e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pablo=20Mat=C3=ADas=20Gomez?= Date: Fri, 22 Nov 2024 13:30:45 -0300 Subject: [PATCH] fix(leak): Avoid mutating logger (#97) * Avoid mutating logger * Change test to cover this scenario * better comment * lint --- logger.go | 6 ++++-- logger_test.go | 5 +++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/logger.go b/logger.go index 0aa5a22..87ff4b9 100644 --- a/logger.go +++ b/logger.go @@ -137,14 +137,16 @@ func SetLogger(opts ...Option) gin.HandlerFunc { } } + // Use a separate logger to save to the context, as we want to avoid mutating the original logger. + contextLogger := rl if track { - l = l.With(). + contextLogger = rl.With(). Str("method", c.Request.Method). Str("path", path). Str("ip", c.ClientIP()). Str("user_agent", c.Request.UserAgent()).Logger() } - c.Set(loggerKey, l) + c.Set(loggerKey, contextLogger) c.Next() diff --git a/logger_test.go b/logger_test.go index f5a1581..ff59044 100644 --- a/logger_test.go +++ b/logger_test.go @@ -180,9 +180,10 @@ func TestCustomLoggerIssue68(t *testing.T) { buffer := new(concurrentBuffer) gin.SetMode(gin.ReleaseMode) r := gin.New() - l := zerolog.New(buffer) + // Use JSON logger as it will explicitly print keys multiple times if they are added multiple times, + // which may happen if there are mutations to the logger. r.Use(SetLogger( - WithLogger(func(*gin.Context, zerolog.Logger) zerolog.Logger { return l }), + WithLogger(func(_ *gin.Context, l zerolog.Logger) zerolog.Logger { return l.Output(buffer).With().Logger() }), WithDefaultLevel(zerolog.DebugLevel), WithClientErrorLevel(zerolog.ErrorLevel), WithServerErrorLevel(zerolog.FatalLevel),