From e31cbdf241b1ff161e3bc5eb4af1c9601fbb7639 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Mon, 10 Jul 2017 03:41:20 -0500 Subject: [PATCH] feat(logger): show query string in logger. (#999) close #988 Signed-off-by: Bo-Yi Wu --- logger.go | 5 +++++ logger_test.go | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/logger.go b/logger.go index a03cde6..470e4bb 100644 --- a/logger.go +++ b/logger.go @@ -77,6 +77,7 @@ func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc { // Start timer start := time.Now() path := c.Request.URL.Path + raw := c.Request.URL.RawQuery // Process request c.Next() @@ -97,6 +98,10 @@ func LoggerWithWriter(out io.Writer, notlogged ...string) HandlerFunc { } comment := c.Errors.ByType(ErrorTypePrivate).String() + if raw != "" { + path = path + "?" + raw + } + fmt.Fprintf(out, "[GIN] %v |%s %3d %s| %13v | %15s |%s %s %-7s %s\n%s", end.Format("2006/01/02 - 15:04:05"), statusColor, statusCode, reset, diff --git a/logger_test.go b/logger_test.go index 15d6ee9..62c1366 100644 --- a/logger_test.go +++ b/logger_test.go @@ -28,10 +28,11 @@ func TestLogger(t *testing.T) { router.HEAD("/example", func(c *Context) {}) router.OPTIONS("/example", func(c *Context) {}) - performRequest(router, "GET", "/example") + performRequest(router, "GET", "/example?a=100") assert.Contains(t, buffer.String(), "200") assert.Contains(t, buffer.String(), "GET") assert.Contains(t, buffer.String(), "/example") + assert.Contains(t, buffer.String(), "a=100") // I wrote these first (extending the above) but then realized they are more // like integration tests because they test the whole logging process rather