1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00

More tags in logger middleware

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-05-10 07:57:35 -07:00
parent 6b55156cb6
commit c4d6f16fc4

View File

@ -22,15 +22,21 @@ type (
// Format is the log format which can be constructed using the following tags:
//
// - time_rfc3339
// - id (Request ID - Not implemented)
// - remote_ip
// - uri
// - host
// - method
// - path
// - referer
// - user_agent
// - status
// - response_time
// - response_size
// - latency (In microseconds)
// - latency_human (Human readable)
// - rx_bytes (Bytes received)
// - tx_bytes (Bytes sent)
//
// Example "${remote_id} ${status}"
// Example "${remote_ip} ${status}"
//
// Optional, with default value as `DefaultLoggerConfig.Format`.
Format string
@ -48,9 +54,10 @@ type (
var (
// DefaultLoggerConfig is the default logger middleware config.
DefaultLoggerConfig = LoggerConfig{
Format: `{"time": "${time_rfc3339}", "remote_ip": "${remote_ip}", ` +
`"method": "${method}", "uri": "${uri}", "status": ${status}, ` +
`"latency": "${response_time}", "served": "${response_size}B"}` + "\n",
Format: `{"time":"${time_rfc3339}","remote_ip":"${remote_ip}",` +
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
`"latency_human":"${latency_human}","rx_bytes":${rx_bytes},` +
`"tx_bytes":${tx_bytes}}` + "\n",
color: color.New(),
Output: os.Stdout,
}
@ -110,6 +117,8 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
ra, _, _ = net.SplitHostPort(ra)
}
return w.Write([]byte(ra))
case "host":
return w.Write([]byte(req.Host()))
case "uri":
return w.Write([]byte(req.URI()))
case "method":
@ -120,6 +129,10 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
p = "/"
}
return w.Write([]byte(p))
case "referer":
return w.Write([]byte(req.Referer()))
case "user_agent":
return w.Write([]byte(req.UserAgent()))
case "status":
n := res.Status()
s := config.color.Green(n)
@ -132,9 +145,18 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
s = config.color.Cyan(n)
}
return w.Write([]byte(s))
case "response_time":
case "latency":
l := stop.Sub(start).Nanoseconds() / 1000
return w.Write([]byte(strconv.FormatInt(l, 10)))
case "latency_human":
return w.Write([]byte(stop.Sub(start).String()))
case "response_size":
case "rx_bytes":
b := req.Header().Get(echo.HeaderContentLength)
if b == "" {
b = "0"
}
return w.Write([]byte(b))
case "tx_bytes":
return w.Write([]byte(strconv.FormatInt(res.Size(), 10)))
default:
return w.Write([]byte(fmt.Sprintf("[unknown tag %s]", tag)))