From 1827916e761e05691bc87b625064b28a39ed0558 Mon Sep 17 00:00:00 2001 From: aruhi Date: Tue, 9 May 2017 00:10:11 +0900 Subject: [PATCH] Add cookie tag for logger (#921) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Thanks for your contribution 🎉 --- middleware/logger.go | 5 +++++ middleware/logger_test.go | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/middleware/logger.go b/middleware/logger.go index 071d0f67..b9c54468 100644 --- a/middleware/logger.go +++ b/middleware/logger.go @@ -183,6 +183,11 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc { return buf.Write([]byte(c.QueryParam(tag[6:]))) case strings.HasPrefix(tag, "form:"): return buf.Write([]byte(c.FormValue(tag[5:]))) + case strings.HasPrefix(tag, "cookie:"): + cookie, err := c.Cookie(tag[7:]) + if err == nil { + return buf.Write([]byte(cookie.Value)) + } } } return 0, nil diff --git a/middleware/logger_test.go b/middleware/logger_test.go index f8b09cfd..e90baaef 100644 --- a/middleware/logger_test.go +++ b/middleware/logger_test.go @@ -90,7 +90,7 @@ func TestLoggerTemplate(t *testing.T) { `"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` + `"latency_human":"${latency_human}","bytes_in":${bytes_in}, "path":"${path}", "referer":"${referer}",` + `"bytes_out":${bytes_out},"ch":"${header:X-Custom-Header}",` + - `"us":"${query:username}", "cf":"${form:username}"}` + "\n", + `"us":"${query:username}", "cf":"${form:username}", "session":"${cookie:session}"}` + "\n", Output: buf, })) @@ -105,6 +105,7 @@ func TestLoggerTemplate(t *testing.T) { req.Header.Add("User-Agent", "echo-tests-agent") req.Header.Add("X-Custom-Header", "AAA-CUSTOM-VALUE") req.Header.Add("X-Request-ID", "6ba7b810-9dad-11d1-80b4-00c04fd430c8") + req.Header.Add("Cookie", "_ga=GA1.2.000000000.0000000000; session=ac08034cd216a647fc2eb62f2bcf7b810") req.Form = url.Values{ "username": []string{"apagano-form"}, "password": []string{"secret-form"}, @@ -129,6 +130,7 @@ func TestLoggerTemplate(t *testing.T) { "google.com": true, "echo-tests-agent": true, "6ba7b810-9dad-11d1-80b4-00c04fd430c8": true, + "ac08034cd216a647fc2eb62f2bcf7b810": true, } for token, present := range cases {