From 2995614e54686976042ef1a25c7c83c80cec00d7 Mon Sep 17 00:00:00 2001 From: Yusuke Komatsu Date: Fri, 10 Feb 2017 16:40:54 +0900 Subject: [PATCH] add id (Request ID) in logger format. --- middleware/logger.go | 10 ++++++++-- middleware/logger_test.go | 32 +++++++++++++++++--------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/middleware/logger.go b/middleware/logger.go index e26b5496..2c3cb452 100644 --- a/middleware/logger.go +++ b/middleware/logger.go @@ -26,7 +26,7 @@ type ( // - time_unix_nano // - time_rfc3339 // - time_rfc3339_nano - // - id (Request ID - Not implemented) + // - id (Request ID) // - remote_ip // - uri // - host @@ -62,7 +62,7 @@ var ( // DefaultLoggerConfig is the default Logger middleware config. DefaultLoggerConfig = LoggerConfig{ Skipper: DefaultSkipper, - Format: `{"time":"${time_rfc3339_nano}","remote_ip":"${remote_ip}","host":"${host}",` + + Format: `{"time":"${time_rfc3339_nano}","id":"${request_id}","remote_ip":"${remote_ip}","host":"${host}",` + `"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` + `"latency_human":"${latency_human}","bytes_in":${bytes_in},` + `"bytes_out":${bytes_out}}` + "\n", @@ -169,6 +169,12 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc { return buf.WriteString(cl) case "bytes_out": return buf.WriteString(strconv.FormatInt(res.Size, 10)) + case "request_id": + ri := req.Header.Get("X-Request-ID") + if ri == "" { + ri = res.Header().Get("X-Request-ID") + } + return w.Write([]byte(ri)) default: switch { case strings.HasPrefix(tag, "header:"): diff --git a/middleware/logger_test.go b/middleware/logger_test.go index 5eacb51f..58e48d7b 100644 --- a/middleware/logger_test.go +++ b/middleware/logger_test.go @@ -86,7 +86,7 @@ func TestLoggerTemplate(t *testing.T) { e := echo.New() e.Use(LoggerWithConfig(LoggerConfig{ - Format: `{"time":"${time_rfc3339_nano}","remote_ip":"${remote_ip}","host":"${host}","user_agent":"${user_agent}",` + + Format: `{"time":"${time_rfc3339_nano}","id":"${request_id}","remote_ip":"${remote_ip}","host":"${host}","user_agent":"${user_agent}",` + `"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}",` + @@ -104,6 +104,7 @@ func TestLoggerTemplate(t *testing.T) { req.Header.Add("Referer", "google.com") 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.Form = url.Values{ "username": []string{"apagano-form"}, "password": []string{"secret-form"}, @@ -113,20 +114,21 @@ func TestLoggerTemplate(t *testing.T) { e.ServeHTTP(rec, req) cases := map[string]bool{ - "apagano-param": true, - "apagano-form": true, - "AAA-CUSTOM-VALUE": true, - "BBB-CUSTOM-VALUE": false, - "secret-form": false, - "hexvalue": false, - "GET": true, - "127.0.0.1": true, - "\"path\":\"/\"": true, - "\"uri\":\"/\"": true, - "\"status\":200": true, - "\"bytes_in\":0": true, - "google.com": true, - "echo-tests-agent": true, + "apagano-param": true, + "apagano-form": true, + "AAA-CUSTOM-VALUE": true, + "BBB-CUSTOM-VALUE": false, + "secret-form": false, + "hexvalue": false, + "GET": true, + "127.0.0.1": true, + "\"path\":\"/\"": true, + "\"uri\":\"/\"": true, + "\"status\":200": true, + "\"bytes_in\":0": true, + "google.com": true, + "echo-tests-agent": true, + "6ba7b810-9dad-11d1-80b4-00c04fd430c8": true, } for token, present := range cases {