Logger

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

Table of Contents

Signatures

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

Examples

First ensure the appropriate packages are imported

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

Initialization / Default Config

  1. // Default middleware config
  2. app.Use(logger.New())

Logging Request ID

  1. app.Use(requestid.New())
  2. app​.​Use​(​logger​.​New​(logger.​Config​{
  3. // For more options, see the Config section
  4. Format​: "${pid} ${locals:requestid} ${status} - ${method} ${path}​\n​"​,
  5. }))

Changing TimeZone & TimeFormat

  1. app.Use(logger.New(logger.Config{
  2. Format: "${pid} ${status} - ${method} ${path}\n",
  3. TimeFormat: "02-Jan-2006",
  4. TimeZone: "America/New_York",
  5. }))

Custom File Writer

  1. file, err := os.OpenFile("./123.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
  2. if err != nil {
  3. log.Fatalf("error opening file: %v", err)
  4. }
  5. defer file.Close()
  6. app.Use(logger.New(logger.Config{
  7. Output: file,
  8. }))

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. // TimeInterval is the delay before the timestamp is updated
  20. //
  21. // Optional. Default: 500 * time.Millisecond
  22. TimeInterval time.Duration
  23. // Output is a writter where logs are written
  24. //
  25. // Default: os.Stderr
  26. Output io.Writer
  27. }

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. TimeInterval: 500 * time.Millisecond,
  7. Output: os.Stderr,
  8. }

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" // response status
  16. TagResBody = "resBody" // response body
  17. TagQueryStringParams = "queryParams" // request query parameters
  18. TagBody = "body" // request body
  19. TagBytesSent = "bytesSent"
  20. TagBytesReceived = "bytesReceived"
  21. TagRoute = "route"
  22. TagError = "error"
  23. TagHeader = "header:" // request header
  24. TagQuery = "query:" // request query
  25. TagForm = "form:" // request form
  26. TagCookie = "cookie:" // request cookie
  27. TagLocals = "locals:"
  28. // colors
  29. TagBlack = "black"
  30. TagRed = "red"
  31. TagGreen = "green"
  32. TagYellow = "yellow"
  33. TagBlue = "blue"
  34. TagMagenta = "magenta"
  35. TagCyan = "cyan"
  36. TagWhite = "white"
  37. TagReset = "reset"
  38. )