From 81265a9f4fe8ee113de0b1244ce04ed553c5d543 Mon Sep 17 00:00:00 2001 From: Nattawit Chaiworawitsakul Date: Sat, 11 May 2019 15:45:52 +0700 Subject: [PATCH 1/3] fix TestLoggerIPAddress reverse assertion --- middleware/logger_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/middleware/logger_test.go b/middleware/logger_test.go index 76b205aa8..b196bc6c8 100644 --- a/middleware/logger_test.go +++ b/middleware/logger_test.go @@ -70,18 +70,18 @@ func TestLoggerIPAddress(t *testing.T) { // With X-Real-IP req.Header.Add(echo.HeaderXRealIP, ip) h(c) - assert.Contains(t, ip, buf.String()) + assert.Contains(t, buf.String(), ip) // With X-Forwarded-For buf.Reset() req.Header.Del(echo.HeaderXRealIP) req.Header.Add(echo.HeaderXForwardedFor, ip) h(c) - assert.Contains(t, ip, buf.String()) + assert.Contains(t, buf.String(), ip) buf.Reset() h(c) - assert.Contains(t, ip, buf.String()) + assert.Contains(t, buf.String(), ip) } func TestLoggerTemplate(t *testing.T) { From 55c4d0abb102f2ed2ca3409bac7f76041ce604ba Mon Sep 17 00:00:00 2001 From: Nattawit Chaiworawitsakul Date: Sat, 11 May 2019 15:47:00 +0700 Subject: [PATCH 2/3] change middleware.Logger default output --- middleware/logger.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/middleware/logger.go b/middleware/logger.go index 6fd59efbc..a0dfbf0df 100644 --- a/middleware/logger.go +++ b/middleware/logger.go @@ -4,7 +4,6 @@ import ( "bytes" "encoding/json" "io" - "os" "strconv" "strings" "sync" @@ -74,7 +73,7 @@ var ( `"status":${status},"error":"${error}","latency":${latency},"latency_human":"${latency_human}"` + `,"bytes_in":${bytes_in},"bytes_out":${bytes_out}}` + "\n", CustomTimeFormat: "2006-01-02 15:04:05.00000", - Output: os.Stdout, + Output: nil, colorer: color.New(), } ) @@ -214,6 +213,10 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc { return } + if config.Output == nil { + _, err = c.Logger().Output().Write(buf.Bytes()) + return + } _, err = config.Output.Write(buf.Bytes()) return } From af34c36bf532dd8a923d9ec44fa4c703c75c6582 Mon Sep 17 00:00:00 2001 From: Nattawit Chaiworawitsakul Date: Thu, 18 Jul 2019 10:43:37 +0700 Subject: [PATCH 3/3] remove nil field declaration --- middleware/logger.go | 1 - 1 file changed, 1 deletion(-) diff --git a/middleware/logger.go b/middleware/logger.go index a0dfbf0df..9baac4769 100644 --- a/middleware/logger.go +++ b/middleware/logger.go @@ -73,7 +73,6 @@ var ( `"status":${status},"error":"${error}","latency":${latency},"latency_human":"${latency_human}"` + `,"bytes_in":${bytes_in},"bytes_out":${bytes_out}}` + "\n", CustomTimeFormat: "2006-01-02 15:04:05.00000", - Output: nil, colorer: color.New(), } )