Logger

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, "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. // - latency (In nanoseconds)
  20. // - latency_human (Human readable)
  21. // - bytes_in (Bytes received)
  22. // - bytes_out (Bytes sent)
  23. // - header:<NAME>
  24. // - query:<NAME>
  25. // - form:<NAME>
  26. // - cookie:<NAME>
  27. //
  28. // Example "${remote_ip} ${status}"
  29. //
  30. // Optional. Default value DefaultLoggerConfig.Format.
  31. Format string `json:"format"`
  32. // Output is a writer where logs are written.
  33. // Optional. Default value os.Stdout.
  34. Output io.Writer
  35. }

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}, "latency":${latency},` +
  5. `"latency_human":"${latency_human}","bytes_in":${bytes_in},` +
  6. `"bytes_out":${bytes_out}}` + "\n",
  7. Output: os.Stdout
  8. }