From d9404e581f8f2b824dd949b65ea592bf31f1e88e Mon Sep 17 00:00:00 2001 From: Pablo Matias Gomez Date: Thu, 21 Nov 2024 15:24:05 -0300 Subject: [PATCH 1/4] Avoid mutating logger --- logger.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 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() From 77ac804d826e1e7fd1f917e4c77ade659e34ab02 Mon Sep 17 00:00:00 2001 From: Pablo Matias Gomez Date: Thu, 21 Nov 2024 16:08:05 -0300 Subject: [PATCH 2/4] Change test to cover this scenario --- logger_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/logger_test.go b/logger_test.go index f5a1581..a4c6b66 100644 --- a/logger_test.go +++ b/logger_test.go @@ -180,9 +180,9 @@ func TestCustomLoggerIssue68(t *testing.T) { buffer := new(concurrentBuffer) gin.SetMode(gin.ReleaseMode) r := gin.New() - l := zerolog.New(buffer) + // Use JSON logger JSON will explicitly print keys multiple times if they are added multiple times (because of 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), From def5116b91c5c13c45383b299dbea7f65ac83337 Mon Sep 17 00:00:00 2001 From: Pablo Matias Gomez Date: Thu, 21 Nov 2024 16:10:51 -0300 Subject: [PATCH 3/4] better comment --- logger_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/logger_test.go b/logger_test.go index a4c6b66..403b901 100644 --- a/logger_test.go +++ b/logger_test.go @@ -180,7 +180,7 @@ func TestCustomLoggerIssue68(t *testing.T) { buffer := new(concurrentBuffer) gin.SetMode(gin.ReleaseMode) r := gin.New() - // Use JSON logger JSON will explicitly print keys multiple times if they are added multiple times (because of mutations to the logger) + // Use JSON logger as it will explicitly print keys multiple times if they are added multiple times (if there were mutations to the logger) r.Use(SetLogger( WithLogger(func(_ *gin.Context, l zerolog.Logger) zerolog.Logger { return l.Output(buffer).With().Logger() }), WithDefaultLevel(zerolog.DebugLevel), From 667f96124444a284c1a63ddf7ab631e2e46729fc Mon Sep 17 00:00:00 2001 From: Pablo Matias Gomez Date: Fri, 22 Nov 2024 13:23:38 -0300 Subject: [PATCH 4/4] lint --- logger_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/logger_test.go b/logger_test.go index 403b901..ff59044 100644 --- a/logger_test.go +++ b/logger_test.go @@ -180,7 +180,8 @@ func TestCustomLoggerIssue68(t *testing.T) { buffer := new(concurrentBuffer) gin.SetMode(gin.ReleaseMode) r := gin.New() - // Use JSON logger as it will explicitly print keys multiple times if they are added multiple times (if there were mutations to the logger) + // 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, l zerolog.Logger) zerolog.Logger { return l.Output(buffer).With().Logger() }), WithDefaultLevel(zerolog.DebugLevel),