From 59500f7c36fbc1dd11f66b805b7bb476b7be9c46 Mon Sep 17 00:00:00 2001 From: Adam Zielinski Date: Sat, 23 Feb 2019 18:12:12 -0500 Subject: [PATCH 1/3] make context available as LogFormatterParams --- logger.go | 3 +++ logger_test.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/logger.go b/logger.go index dc639975a0..e8237bf415 100644 --- a/logger.go +++ b/logger.go @@ -66,6 +66,8 @@ type LogFormatterParams struct { IsTerm bool // BodySize is the size of the Response Body BodySize int + // Context is the Context associated with the request. + Context *Context } // StatusCodeColor is the ANSI color for appropriately logging http status code to a terminal. @@ -227,6 +229,7 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc { param := LogFormatterParams{ Request: c.Request, IsTerm: isTerm, + Context: c, } // Stop timer diff --git a/logger_test.go b/logger_test.go index c551677a6f..dae72f76c2 100644 --- a/logger_test.go +++ b/logger_test.go @@ -181,6 +181,7 @@ func TestLoggerWithFormatter(t *testing.T) { func TestLoggerWithConfigFormatting(t *testing.T) { var gotParam LogFormatterParams + var gotCtx *Context buffer := new(bytes.Buffer) router := New() @@ -204,6 +205,7 @@ func TestLoggerWithConfigFormatting(t *testing.T) { router.GET("/example", func(c *Context) { // set dummy ClientIP c.Request.Header.Set("X-Forwarded-For", "20.20.20.20") + gotCtx = c }) performRequest(router, "GET", "/example?a=100") @@ -223,6 +225,8 @@ func TestLoggerWithConfigFormatting(t *testing.T) { assert.Equal(t, "GET", gotParam.Method) assert.Equal(t, "/example?a=100", gotParam.Path) assert.Empty(t, gotParam.ErrorMessage) + assert.Empty(t, gotParam.ErrorMessage) + assert.Equal(t, gotCtx, gotParam.Context) } From 1be9ccae8e5d1a7e82b90fe401c495d620495b7e Mon Sep 17 00:00:00 2001 From: Adam Zielinski Date: Tue, 5 Mar 2019 22:55:50 -0500 Subject: [PATCH 2/3] pass context Keys to LogFormatterParams --- logger.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/logger.go b/logger.go index e8237bf415..2ecaed7d04 100644 --- a/logger.go +++ b/logger.go @@ -66,8 +66,8 @@ type LogFormatterParams struct { IsTerm bool // BodySize is the size of the Response Body BodySize int - // Context is the Context associated with the request. - Context *Context + // Keys are the keys set on the request's context. + Keys map[string]interface{} } // StatusCodeColor is the ANSI color for appropriately logging http status code to a terminal. @@ -229,7 +229,7 @@ func LoggerWithConfig(conf LoggerConfig) HandlerFunc { param := LogFormatterParams{ Request: c.Request, IsTerm: isTerm, - Context: c, + Keys: c.Keys, } // Stop timer From 8327f065b9e96a89fe6a6e93df05254bcb404aba Mon Sep 17 00:00:00 2001 From: Adam Zielinski Date: Tue, 5 Mar 2019 23:00:40 -0500 Subject: [PATCH 3/3] update logger test to check for Key param --- logger_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/logger_test.go b/logger_test.go index dae72f76c2..a2041773ba 100644 --- a/logger_test.go +++ b/logger_test.go @@ -181,7 +181,7 @@ func TestLoggerWithFormatter(t *testing.T) { func TestLoggerWithConfigFormatting(t *testing.T) { var gotParam LogFormatterParams - var gotCtx *Context + var gotKeys map[string]interface{} buffer := new(bytes.Buffer) router := New() @@ -205,7 +205,7 @@ func TestLoggerWithConfigFormatting(t *testing.T) { router.GET("/example", func(c *Context) { // set dummy ClientIP c.Request.Header.Set("X-Forwarded-For", "20.20.20.20") - gotCtx = c + gotKeys = c.Keys }) performRequest(router, "GET", "/example?a=100") @@ -226,7 +226,7 @@ func TestLoggerWithConfigFormatting(t *testing.T) { assert.Equal(t, "/example?a=100", gotParam.Path) assert.Empty(t, gotParam.ErrorMessage) assert.Empty(t, gotParam.ErrorMessage) - assert.Equal(t, gotCtx, gotParam.Context) + assert.Equal(t, gotKeys, gotParam.Keys) }