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

Table of Contents

Signatures

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

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
)
Copied!

Initialization / Default Config

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

Logging Request ID

1
app.Use(logger.New(logger.Config{
2
Format: "[${ip}]:${port} ${status} - ${method} ${path}\n",
3
}))
Copied!

Logging Request ID

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

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
}))
Copied!

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
​
7
app.Use(logger.New(logger.Config{
8
Output: file,
9
}))
Copied!

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
​
8
// Format defines the logging tags
9
//
10
// Optional. Default: [${time}] ${status} - ${latency} ${method} ${path}\n
11
Format string
12
​
13
// TimeFormat https://programming.guide/go/format-parse-string-time-date-example.html
14
//
15
// Optional. Default: 15:04:05
16
TimeFormat string
17
​
18
// TimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc
19
//
20
// Optional. Default: "Local"
21
TimeZone string
22
​
23
// TimeInterval is the delay before the timestamp is updated
24
//
25
// Optional. Default: 500 * time.Millisecond
26
TimeInterval time.Duration
27
​
28
// Output is a writter where logs are written
29
//
30
// Default: os.Stderr
31
Output io.Writer
32
}
Copied!

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
}
Copied!

Constants

1
// Logger variables
2
const (
3
TagPid = "pid"
4
TagTime = "time"
5
TagReferer = "referer"
6
TagProtocol = "protocol"
7
TagPort = "port"
8
TagIP = "ip"
9
TagIPs = "ips"
10
TagHost = "host"
11
TagMethod = "method"
12
TagPath = "path"
13
TagURL = "url"
14
TagUA = "ua"
15
TagLatency = "latency"
16
TagStatus = "status" // response status
17
TagResBody = "resBody" // response body
18
TagQueryStringParams = "queryParams" // request query parameters
19
TagBody = "body" // request body
20
TagBytesSent = "bytesSent"
21
TagBytesReceived = "bytesReceived"
22
TagRoute = "route"
23
TagError = "error"
24
TagHeader = "header:" // DEPRECATED: Use TagReqHeader instead
25
TagReqHeader = "reqHeader:" // request header
26
TagRespHeader = "respHeader:" // response header
27
TagQuery = "query:" // request query
28
TagForm = "form:" // request form
29
TagCookie = "cookie:" // request cookie
30
TagLocals = "locals:"
31
​
32
// colors
33
TagBlack = "black"
34
TagRed = "red"
35
TagGreen = "green"
36
TagYellow = "yellow"
37
TagBlue = "blue"
38
TagMagenta = "magenta"
39
TagCyan = "cyan"
40
TagWhite = "white"
41
TagReset = "reset"
42
)
Copied!
Last modified 16d ago