1
0
mirror of https://github.com/labstack/echo.git synced 2025-06-06 23:46:16 +02:00

Updated logger docs

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2017-01-11 21:02:34 -08:00
parent ce6b1e20db
commit 0696d55586
3 changed files with 31 additions and 25 deletions

View File

@ -6,7 +6,7 @@ type (
// Request defines the data to be logged by logger middleware. // Request defines the data to be logged by logger middleware.
Request struct { Request struct {
// ID string `json:"id,omitempty"` (Request ID - Not implemented) // ID string `json:"id,omitempty"` (Request ID - Not implemented)
Time time.Time `json:"time,omitempty"` Time *time.Time `json:"time,omitempty"` // http://stackoverflow.com/questions/32643815/golang-json-omitempty-with-time-time-field
RemoteIP string `json:"remote_ip,omitempty"` RemoteIP string `json:"remote_ip,omitempty"`
URI string `json:"uri,omitempty"` URI string `json:"uri,omitempty"`
Host string `json:"host,omitempty"` Host string `json:"host,omitempty"`
@ -17,8 +17,8 @@ type (
Status int `json:"status,omitempty"` Status int `json:"status,omitempty"`
Latency time.Duration `json:"latency,omitempty"` Latency time.Duration `json:"latency,omitempty"`
LatencyHuman string `json:"latency_human,omitempty"` LatencyHuman string `json:"latency_human,omitempty"`
BytesIn int64 `json:"bytes_in"` BytesIn int64 `json:"bytes_in"` // Allow 0 value
BytesOut int64 `json:"bytes_out"` BytesOut int64 `json:"bytes_out"` // As aboves
Header map[string]string `json:"header,omitempty"` Header map[string]string `json:"header,omitempty"`
Form map[string]string `json:"form,omitempty"` Form map[string]string `json:"form,omitempty"`
Query map[string]string `json:"query,omitempty"` Query map[string]string `json:"query,omitempty"`

View File

@ -21,8 +21,7 @@ type (
// Availabe logger fields: // Availabe logger fields:
// //
// - time_unix // - time
// - time_rfc3339
// - id (Request ID - Not implemented) // - id (Request ID - Not implemented)
// - remote_ip // - remote_ip
// - uri // - uri
@ -121,7 +120,8 @@ func LoggerWithConfig(config LoggerConfig) echo.MiddlewareFunc {
for _, f := range config.Fields { for _, f := range config.Fields {
switch f { switch f {
case "time": case "time":
request.Time = time.Now() t := time.Now()
request.Time = &t
case "remote_ip": case "remote_ip":
request.RemoteIP = c.RealIP() request.RemoteIP = c.RealIP()
case "host": case "host":

View File

@ -16,7 +16,7 @@ Logger middleware logs the information about each HTTP request.
*Sample Output* *Sample Output*
```js ```js
{"time":"2016-05-10T07:02:25-07:00","remote_ip":"::1","method":"GET","uri":"/","status":200, "latency":55653,"latency_human":"55.653µs","rx_bytes":0,"tx_bytes":13} {"time":"2017-01-11T19:58:51.322299983-08:00","remote_ip":"::1","uri":"/","host":"localhost:1323","method":"GET","status":200,"latency":10667,"latency_human":"10.667µs","bytes_in":0,"bytes_out":2}
``` ```
## Custom Configuration ## Custom Configuration
@ -26,7 +26,7 @@ Logger middleware logs the information about each HTTP request.
```go ```go
e := echo.New() e := echo.New()
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: "method=${method}, uri=${uri}, status=${status}\n", Fields: []string{"method", "uri", "status"},
})) }))
``` ```
@ -35,7 +35,7 @@ Example above uses a `Format` which logs request method and request URI.
*Sample Output* *Sample Output*
```sh ```sh
method=GET, uri=/hello, status=200 {"uri":"/","method":"GET","status":200,"bytes_in":0,"bytes_out":0}
``` ```
## Configuration ## Configuration
@ -45,9 +45,9 @@ LoggerConfig struct {
// Skipper defines a function to skip middleware. // Skipper defines a function to skip middleware.
Skipper Skipper Skipper Skipper
// Log format which can be constructed using the following tags: // Availabe logger fields:
// //
// - time_rfc3339 // - time
// - id (Request ID - Not implemented) // - id (Request ID - Not implemented)
// - remote_ip // - remote_ip
// - uri // - uri
@ -57,22 +57,20 @@ LoggerConfig struct {
// - referer // - referer
// - user_agent // - user_agent
// - status // - status
// - latency (In microseconds) // - latency (In nanosecond)
// - latency_human (Human readable) // - latency_human (Human readable)
// - bytes_in (Bytes received) // - bytes_in (Bytes received)
// - bytes_out (Bytes sent) // - bytes_out (Bytes sent)
// - header:<name> // - header:<name>
// - query:<name> // - query:<name>
// - form:<name> // - form:<name>
//
// Example "${remote_ip} ${status}"
//
// Optional. Default value DefaultLoggerConfig.Format.
Format string `json:"format"`
// Output is a writer where logs are written. // Optional. Default value DefaultLoggerConfig.Fields.
// Optional. Default value os.Stdout. Fields []string `json:"fields"`
Output io.Writer
// Output is where logs are written.
// Optional. Default value &Stream{os.Stdout}.
Output db.Logger
} }
``` ```
@ -81,10 +79,18 @@ LoggerConfig struct {
```go ```go
DefaultLoggerConfig = LoggerConfig{ DefaultLoggerConfig = LoggerConfig{
Skipper: defaultSkipper, Skipper: defaultSkipper,
Format: `{"time":"${time_rfc3339}","remote_ip":"${remote_ip}",` + Fields: []string{
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` + "time",
`"latency_human":"${latency_human}","bytes_in":${bytes_in},` + "remote_ip",
`"bytes_out":${bytes_out}}` + "\n", "host",
Output: os.Stdout, "method",
"uri",
"status",
"latency",
"latency_human",
"bytes_in",
"bytes_out",
},
Output: &Stream{os.Stdout},
} }
``` ```