Skip to content

Commit

Permalink
Add request route with "route" tag to logger middleware (#2162)
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yomo committed Dec 4, 2022
1 parent 8d4ac4c commit 135c511
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
3 changes: 3 additions & 0 deletions middleware/logger.go
Expand Up @@ -35,6 +35,7 @@ type (
// - host
// - method
// - path
// - route
// - protocol
// - referer
// - user_agent
Expand Down Expand Up @@ -173,6 +174,8 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
p = "/"
}
return buf.WriteString(p)
case "route":
return buf.WriteString(c.Path())
case "protocol":
return buf.WriteString(req.Proto)
case "referer":
Expand Down
9 changes: 5 additions & 4 deletions middleware/logger_test.go
Expand Up @@ -92,17 +92,17 @@ func TestLoggerTemplate(t *testing.T) {
e.Use(LoggerWithConfig(LoggerConfig{
Format: `{"time":"${time_rfc3339_nano}","id":"${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}",` +
`"latency_human":"${latency_human}","bytes_in":${bytes_in}, "path":"${path}", "route":"${route}", "referer":"${referer}",` +
`"bytes_out":${bytes_out},"ch":"${header:X-Custom-Header}", "protocol":"${protocol}"` +
`"us":"${query:username}", "cf":"${form:username}", "session":"${cookie:session}"}` + "\n",
Output: buf,
}))

e.GET("/", func(c echo.Context) error {
e.GET("/users/:id", func(c echo.Context) error {
return c.String(http.StatusOK, "Header Logged")
})

req := httptest.NewRequest(http.MethodGet, "/?username=apagano-param&password=secret", nil)
req := httptest.NewRequest(http.MethodGet, "/users/1?username=apagano-param&password=secret", nil)
req.RequestURI = "/"
req.Header.Add(echo.HeaderXRealIP, "127.0.0.1")
req.Header.Add("Referer", "google.com")
Expand All @@ -127,7 +127,8 @@ func TestLoggerTemplate(t *testing.T) {
"hexvalue": false,
"GET": true,
"127.0.0.1": true,
"\"path\":\"/\"": true,
"\"path\":\"/users/1\"": true,
"\"route\":\"/users/:id\"": true,
"\"uri\":\"/\"": true,
"\"status\":200": true,
"\"bytes_in\":0": true,
Expand Down

0 comments on commit 135c511

Please sign in to comment.