Logger

Logger middleware for Fiber that logs HTTP request/response details.

Signatures

  1. func New(config ...Config) fiber.Handler

Examples

Import the middleware package that is part of the Fiber web framework

  1. import (
  2. "github.com/gofiber/fiber/v2"
  3. "github.com/gofiber/fiber/v2/middleware/logger"
  4. )

After you initiate your Fiber app, you can use the following possibilities:

  1. // Default middleware config
  2. app.Use(logger.New())
  3. // Or extend your config for customization
  4. app.Use(logger.New(logger.Config{
  5. Format: "${pid} ${status} - ${method} ${path}\n",
  6. TimeFormat: "02-Jan-2006",
  7. TimeZone: "America/New_York",
  8. Output: os.Stdout,
  9. }))

Config

  1. // Config defines the config for middleware.
  2. type Config struct {
  3. // Next defines a function to skip this middleware when returned true.
  4. //
  5. // Optional. Default: nil
  6. Next func(c *fiber.Ctx) bool
  7. // Format defines the logging tags
  8. //
  9. // Optional. Default: [${time}] ${status} - ${latency} ${method} ${path}\n
  10. Format string
  11. // TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html
  12. //
  13. // Optional. Default: 15:04:05
  14. TimeFormat string
  15. // TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc
  16. //
  17. // Optional. Default: "Local"
  18. TimeZone string
  19. // Output is a writter where logs are written
  20. //
  21. // Default: os.Stderr
  22. Output io.Writer
  23. }

Default Config

  1. var ConfigDefault = Config{
  2. Next: nil,
  3. Format: "[${time}] ${status} - ${latency} ${method} ${path}\n",
  4. TimeFormat: "15:04:05",
  5. TimeZone: "Local",
  6. Output: os.Stderr,
  7. }

Constants

  1. // Logger variables
  2. const (
  3. TagPid = "pid"
  4. TagTime = "time"
  5. TagReferer = "referer"
  6. TagProtocol = "protocol"
  7. TagIP = "ip"
  8. TagIPs = "ips"
  9. TagHost = "host"
  10. TagMethod = "method"
  11. TagPath = "path"
  12. TagURL = "url"
  13. TagUA = "ua"
  14. TagLatency = "latency"
  15. TagStatus = "status"
  16. TagBody = "body"
  17. TagBytesSent = "bytesSent"
  18. TagBytesReceived = "bytesReceived"
  19. TagRoute = "route"
  20. TagError = "error"
  21. TagHeader = "header:"
  22. TagQuery = "query:"
  23. TagForm = "form:"
  24. TagCookie = "cookie:"
  25. TagBlack = "black"
  26. TagRed = "red"
  27. TagGreen = "green"
  28. TagYellow = "yellow"
  29. TagBlue = "blue"
  30. TagMagenta = "magenta"
  31. TagCyan = "cyan"
  32. TagWhite = "white"
  33. TagReset = "reset"
  34. )