mirror of
https://github.com/labstack/echo.git
synced 2025-01-03 22:52:19 +02:00
add id (Request ID) in logger format.
This commit is contained in:
parent
4dc9cd0bb6
commit
2995614e54
@ -26,7 +26,7 @@ type (
|
|||||||
// - time_unix_nano
|
// - time_unix_nano
|
||||||
// - time_rfc3339
|
// - time_rfc3339
|
||||||
// - time_rfc3339_nano
|
// - time_rfc3339_nano
|
||||||
// - id (Request ID - Not implemented)
|
// - id (Request ID)
|
||||||
// - remote_ip
|
// - remote_ip
|
||||||
// - uri
|
// - uri
|
||||||
// - host
|
// - host
|
||||||
@ -62,7 +62,7 @@ var (
|
|||||||
// DefaultLoggerConfig is the default Logger middleware config.
|
// DefaultLoggerConfig is the default Logger middleware config.
|
||||||
DefaultLoggerConfig = LoggerConfig{
|
DefaultLoggerConfig = LoggerConfig{
|
||||||
Skipper: DefaultSkipper,
|
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},` +
|
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
|
||||||
`"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
|
`"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
|
||||||
`"bytes_out":${bytes_out}}` + "\n",
|
`"bytes_out":${bytes_out}}` + "\n",
|
||||||
@ -169,6 +169,12 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
|
|||||||
return buf.WriteString(cl)
|
return buf.WriteString(cl)
|
||||||
case "bytes_out":
|
case "bytes_out":
|
||||||
return buf.WriteString(strconv.FormatInt(res.Size, 10))
|
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:
|
default:
|
||||||
switch {
|
switch {
|
||||||
case strings.HasPrefix(tag, "header:"):
|
case strings.HasPrefix(tag, "header:"):
|
||||||
|
@ -86,7 +86,7 @@ func TestLoggerTemplate(t *testing.T) {
|
|||||||
|
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
e.Use(LoggerWithConfig(LoggerConfig{
|
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},` +
|
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
|
||||||
`"latency_human":"${latency_human}","bytes_in":${bytes_in}, "path":"${path}", "referer":"${referer}",` +
|
`"latency_human":"${latency_human}","bytes_in":${bytes_in}, "path":"${path}", "referer":"${referer}",` +
|
||||||
`"bytes_out":${bytes_out},"ch":"${header:X-Custom-Header}",` +
|
`"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("Referer", "google.com")
|
||||||
req.Header.Add("User-Agent", "echo-tests-agent")
|
req.Header.Add("User-Agent", "echo-tests-agent")
|
||||||
req.Header.Add("X-Custom-Header", "AAA-CUSTOM-VALUE")
|
req.Header.Add("X-Custom-Header", "AAA-CUSTOM-VALUE")
|
||||||
|
req.Header.Add("X-Request-ID", "6ba7b810-9dad-11d1-80b4-00c04fd430c8")
|
||||||
req.Form = url.Values{
|
req.Form = url.Values{
|
||||||
"username": []string{"apagano-form"},
|
"username": []string{"apagano-form"},
|
||||||
"password": []string{"secret-form"},
|
"password": []string{"secret-form"},
|
||||||
@ -113,20 +114,21 @@ func TestLoggerTemplate(t *testing.T) {
|
|||||||
e.ServeHTTP(rec, req)
|
e.ServeHTTP(rec, req)
|
||||||
|
|
||||||
cases := map[string]bool{
|
cases := map[string]bool{
|
||||||
"apagano-param": true,
|
"apagano-param": true,
|
||||||
"apagano-form": true,
|
"apagano-form": true,
|
||||||
"AAA-CUSTOM-VALUE": true,
|
"AAA-CUSTOM-VALUE": true,
|
||||||
"BBB-CUSTOM-VALUE": false,
|
"BBB-CUSTOM-VALUE": false,
|
||||||
"secret-form": false,
|
"secret-form": false,
|
||||||
"hexvalue": false,
|
"hexvalue": false,
|
||||||
"GET": true,
|
"GET": true,
|
||||||
"127.0.0.1": true,
|
"127.0.0.1": true,
|
||||||
"\"path\":\"/\"": true,
|
"\"path\":\"/\"": true,
|
||||||
"\"uri\":\"/\"": true,
|
"\"uri\":\"/\"": true,
|
||||||
"\"status\":200": true,
|
"\"status\":200": true,
|
||||||
"\"bytes_in\":0": true,
|
"\"bytes_in\":0": true,
|
||||||
"google.com": true,
|
"google.com": true,
|
||||||
"echo-tests-agent": true,
|
"echo-tests-agent": true,
|
||||||
|
"6ba7b810-9dad-11d1-80b4-00c04fd430c8": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
for token, present := range cases {
|
for token, present := range cases {
|
||||||
|
Loading…
Reference in New Issue
Block a user