From 8cfaf50b8fcb8f5df55176358ecab3656a703a0f Mon Sep 17 00:00:00 2001 From: nattawitc Date: Thu, 18 Jul 2019 11:34:31 +0700 Subject: [PATCH] fix #1260 change middleware.Logger's default output (#1336) * fix TestLoggerIPAddress reverse assertion * change middleware.Logger default output * remove nil field declaration --- middleware/logger.go | 6 ++++-- middleware/logger_test.go | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/middleware/logger.go b/middleware/logger.go index 6fd59efbc..9baac4769 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,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: os.Stdout, colorer: color.New(), } ) @@ -214,6 +212,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 } 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) {