Skip to main content
Version: Next

Logger

Logger middleware for Fiber that logs HTTP requests and responses.

Signatures

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

Examples

Import the package:

import (
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/logger"
)
tip

Registration order matters: only routes added after the logger are logged, so register it early.

Once your Fiber app is initialized, use the middleware like this:

// Initialize default config
app.Use(logger.New())

// Or extend your config for customization
// Log remote IP and port
app.Use(logger.New(logger.Config{
Format: "[${ip}]:${port} ${status} - ${method} ${path}\n",
}))

// Logging Request ID
app.Use(requestid.New()) // Ensure requestid middleware is used before the logger
app.Use(logger.New(logger.Config{
CustomTags: map[string]logger.LogFunc{
"requestid": func(output logger.Buffer, c fiber.Ctx, data *logger.Data, extraParam string) (int, error) {
return output.WriteString(requestid.FromContext(c))
},
},
// For more options, see the Config section
// Use the custom tag ${requestid} as defined above.
Format: "${pid} ${requestid} ${status} - ${method} ${path}\n",
}))

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

// Custom File Writer
accessLog, err := os.OpenFile("./access.log", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening access.log file: %v", err)
}
defer accessLog.Close()
app.Use(logger.New(logger.Config{
Stream: accessLog,
}))

// Add Custom Tags
app.Use(logger.New(logger.Config{
CustomTags: map[string]logger.LogFunc{
"custom_tag": func(output logger.Buffer, c fiber.Ctx, data *logger.Data, extraParam string) (int, error) {
return output.WriteString("it is a custom tag")
},
},
}))

// Callback after log is written
app.Use(logger.New(logger.Config{
TimeFormat: time.RFC3339Nano,
TimeZone: "Asia/Shanghai",
Done: func(c fiber.Ctx, logString []byte) {
if c.Response().StatusCode() != fiber.StatusOK {
reporter.SendToSlack(logString)
}
},
}))

// Disable colors when outputting to default format
app.Use(logger.New(logger.Config{
DisableColors: true,
}))

// Force the use of colors
app.Use(logger.New(logger.Config{
ForceColors: true,
}))

// Use predefined formats
app.Use(logger.New(logger.Config{
Format: logger.FormatCommon,
}))

app.Use(logger.New(logger.Config{
Format: logger.FormatCombined,
}))

app.Use(logger.New(logger.Config{
Format: logger.FormatJSON,
}))

app.Use(logger.New(logger.Config{
Format: logger.FormatECS,
}))

Use Logger Middleware with Other Loggers

To combine the logger middleware with loggers like Zerolog, Zap, or Logrus, use the LoggerToWriter helper to adapt them to an io.Writer.

package main

import (
"github.com/gofiber/contrib/fiberzap/v2"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
"github.com/gofiber/fiber/v3/middleware/logger"
)

func main() {
// Create a new Fiber instance
app := fiber.New()

// Create a new zap logger which is compatible with Fiber AllLogger interface
zap := fiberzap.NewLogger(fiberzap.LoggerConfig{
ExtraKeys: []string{"request_id"},
})

// Use the logger middleware with the zap logger
app.Use(logger.New(logger.Config{
Stream: logger.LoggerToWriter(zap, log.LevelDebug),
}))

// Define a route
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})

// Start server on http://localhost:3000
app.Listen(":3000")
}
tip

Writing to os.File is goroutine-safe, but custom streams may require locking to serialize writes.

Config

PropertyTypeDescriptionDefault
Nextfunc(fiber.Ctx) boolNext defines a function to skip this middleware when it returns true.nil
Skipfunc(fiber.Ctx) boolSkip is a function to determine if logging is skipped or written to Stream.nil
Donefunc(fiber.Ctx, []byte)Done is a function that is called after the log string for a request is written to Stream, and pass the log string as parameter.nil
CustomTagsmap[string]LogFunctagFunctions defines the custom tag action.map[string]LogFunc
FormatstringDefines the logging tags. See more in Predefined Formats, or create your own using Tags.[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n (same as DefaultFormat)
TimeFormatstringTimeFormat defines the time format for log timestamps.15:04:05
TimeZonestringTimeZone can be specified, such as "UTC" and "America/New_York" and "Asia/Chongqing", etc"Local"
TimeIntervaltime.DurationTimeInterval is the delay before the timestamp is updated.500 * time.Millisecond
Streamio.WriterStream is a writer where logs are written.os.Stdout
LoggerFuncfunc(c fiber.Ctx, data *Data, cfg Config) errorCustom logger function for integration with logging libraries (Zerolog, Zap, Logrus, etc). Defaults to Fiber's default logger if not defined.see default_logger.go defaultLoggerInstance
DisableColorsboolDisableColors defines if the logs output should be colorized.false
ForceColorsboolForceColors defines if the logs output should be colorized even when the output is not a terminal.false

Default Config

var ConfigDefault = Config{
Next: nil,
Skip: nil,
Done: nil,
Format: DefaultFormat,
TimeFormat: "15:04:05",
TimeZone: "Local",
TimeInterval: 500 * time.Millisecond,
Stream: os.Stdout,
BeforeHandlerFunc: beforeHandlerFunc,
LoggerFunc: defaultLoggerInstance,
enableColors: true,
}

Predefined Formats

Logger provides predefined formats that you can use by name or directly by specifying the format string.

Format ConstantFormat StringDescription
DefaultFormat"[${time}] ${ip} ${status} - ${latency} ${method} ${path} ${error}\n"Fiber's default logger format.
CommonFormat"${ip} - - [${time}] "${method} ${url} ${protocol}" ${status} ${bytesSent}\n"Common Log Format (CLF) used in web server logs.
CombinedFormat"${ip} - - [${time}] "${method} ${url} ${protocol}" ${status} ${bytesSent} "${referer}" "${ua}"\n"CLF format plus the referer and user agent fields.
JSONFormat"{time: ${time}, ip: ${ip}, method: ${method}, url: ${url}, status: ${status}, bytesSent: ${bytesSent}}\n"JSON format for structured logging.
ECSFormat"{\"@timestamp\":\"${time}\",\"ecs\":{\"version\":\"1.6.0\"},\"client\":{\"ip\":\"${ip}\"},\"http\":{\"request\":{\"method\":\"${method}\",\"url\":\"${url}\",\"protocol\":\"${protocol}\"},\"response\":{\"status_code\":${status},\"body\":{\"bytes\":${bytesSent}}}},\"log\":{\"level\":\"INFO\",\"logger\":\"fiber\"},\"message\":\"${method} ${url} responded with ${status}\"}\n"Elastic Common Schema (ECS) format for structured logging.

Constants

// Logger variables
const (
TagPid = "pid"
TagTime = "time"
TagReferer = "referer"
TagProtocol = "protocol"
TagPort = "port"
TagIP = "ip"
TagIPs = "ips"
TagHost = "host"
TagMethod = "method"
TagPath = "path"
TagURL = "url"
TagUA = "ua"
TagLatency = "latency"
TagStatus = "status" // response status
TagResBody = "resBody" // response body
TagReqHeaders = "reqHeaders"
TagQueryStringParams = "queryParams" // request query parameters
TagBody = "body" // request body
TagBytesSent = "bytesSent"
TagBytesReceived = "bytesReceived"
TagRoute = "route"
TagError = "error"
TagReqHeader = "reqHeader:" // request header
TagRespHeader = "respHeader:" // response header
TagQuery = "query:" // request query
TagForm = "form:" // request form
TagCookie = "cookie:" // request cookie
TagLocals = "locals:"
// colors
TagBlack = "black"
TagRed = "red"
TagGreen = "green"
TagYellow = "yellow"
TagBlue = "blue"
TagMagenta = "magenta"
TagCyan = "cyan"
TagWhite = "white"
TagReset = "reset"
)