1
0
mirror of https://github.com/labstack/echo.git synced 2025-01-01 22:09:21 +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 struct {
// 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"`
URI string `json:"uri,omitempty"`
Host string `json:"host,omitempty"`
@ -17,8 +17,8 @@ type (
Status int `json:"status,omitempty"`
Latency time.Duration `json:"latency,omitempty"`
LatencyHuman string `json:"latency_human,omitempty"`
BytesIn int64 `json:"bytes_in"`
BytesOut int64 `json:"bytes_out"`
BytesIn int64 `json:"bytes_in"` // Allow 0 value
BytesOut int64 `json:"bytes_out"` // As aboves
Header map[string]string `json:"header,omitempty"`
Form map[string]string `json:"form,omitempty"`
Query map[string]string `json:"query,omitempty"`

View File

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

View File

@ -16,7 +16,7 @@ Logger middleware logs the information about each HTTP request.
*Sample Output*
```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
@ -26,7 +26,7 @@ Logger middleware logs the information about each HTTP request.
```go
e := echo.New()
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*
```sh
method=GET, uri=/hello, status=200
{"uri":"/","method":"GET","status":200,"bytes_in":0,"bytes_out":0}
```
## Configuration
@ -45,9 +45,9 @@ LoggerConfig struct {
// Skipper defines a function to skip middleware.
Skipper Skipper
// Log format which can be constructed using the following tags:
// Availabe logger fields:
//
// - time_rfc3339
// - time
// - id (Request ID - Not implemented)
// - remote_ip
// - uri
@ -57,22 +57,20 @@ LoggerConfig struct {
// - referer
// - user_agent
// - status
// - latency (In microseconds)
// - latency (In nanosecond)
// - latency_human (Human readable)
// - bytes_in (Bytes received)
// - bytes_out (Bytes sent)
// - header:<name>
// - query:<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 os.Stdout.
Output io.Writer
// Optional. Default value DefaultLoggerConfig.Fields.
Fields []string `json:"fields"`
// Output is where logs are written.
// Optional. Default value &Stream{os.Stdout}.
Output db.Logger
}
```
@ -81,10 +79,18 @@ LoggerConfig struct {
```go
DefaultLoggerConfig = LoggerConfig{
Skipper: defaultSkipper,
Format: `{"time":"${time_rfc3339}","remote_ip":"${remote_ip}",` +
`"method":"${method}","uri":"${uri}","status":${status}, "latency":${latency},` +
`"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
`"bytes_out":${bytes_out}}` + "\n",
Output: os.Stdout,
Fields: []string{
"time",
"remote_ip",
"host",
"method",
"uri",
"status",
"latency",
"latency_human",
"bytes_in",
"bytes_out",
},
Output: &Stream{os.Stdout},
}
```