Logger Middleware

Logger middleware logs the information about each HTTP request.

Usage

e.Use(middleware.Logger())

Sample Output

  1. {"time":"2017-01-12T08:58:07.372015644-08:00","remote_ip":"::1","host":"localhost:1323","method":"GET","uri":"/","status":200,"error":"","latency":14743,"latency_human":"14.743µs","bytes_in":0,"bytes_out":2}

Custom Configuration

Usage

  1. e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
  2. Format: "method=${method}, uri=${uri}, status=${status}\n",
  3. }))

Example above uses a Format which logs request method and request URI.

Sample Output

  1. method=GET, uri=/, status=200

Configuration

  1. LoggerConfig struct {
  2. // Skipper defines a function to skip middleware.
  3. Skipper Skipper
  4. // Tags to constructed the logger format.
  5. //
  6. // - time_unix
  7. // - time_unix_nano
  8. // - time_rfc3339
  9. // - time_rfc3339_nano
  10. // - id (Request ID)
  11. // - remote_ip
  12. // - uri
  13. // - host
  14. // - method
  15. // - path
  16. // - referer
  17. // - user_agent
  18. // - status
  19. // - error
  20. // - latency (In nanoseconds)
  21. // - latency_human (Human readable)
  22. // - bytes_in (Bytes received)
  23. // - bytes_out (Bytes sent)
  24. // - header:<NAME>
  25. // - query:<NAME>
  26. // - form:<NAME>
  27. // - cookie:<NAME>
  28. //
  29. // Example "${remote_ip} ${status}"
  30. //
  31. // Optional. Default value DefaultLoggerConfig.Format.
  32. Format string `json:"format"`
  33. // Output is a writer where logs are written.
  34. // Optional. Default value os.Stdout.
  35. Output io.Writer
  36. }

Default Configuration

  1. DefaultLoggerConfig = LoggerConfig{
  2. Skipper: DefaultSkipper,
  3. Format: `{"time":"${time_rfc3339_nano}","id":"${id}","remote_ip":"${remote_ip}","host":"${host}",` +
  4. `"method":"${method}","uri":"${uri}","status":${status},"error":"${error}","latency":${latency},` +
  5. `"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
  6. `"bytes_out":${bytes_out}}` + "\n",
  7. Output: os.Stdout
  8. }