Zerolog
Zerolog logging support for Fiber.
Compatible with Fiber v3.
Go version support
We only support the latest two versions of Go. Visit https://go.dev/doc/devel/release for more information.
Install
go get -u github.com/gofiber/fiber/v3
go get -u github.com/gofiber/contrib/v3/zerolog
go get -u github.com/rs/zerolog/log
Signature
zerolog.New(config ...zerolog.Config) fiber.Handler
Config
| Property | Type | Description | Default |
|---|---|---|---|
| Next | func(fiber.Ctx) bool | Define a function to skip this middleware when it returns true. | nil |
| Skip | func(fiber.Ctx) bool | Define a function that is called after the request has been processed but before the log entry is assembled. If it returns true, logging is skipped entirely with zero overhead. This gives access to the full request context, including data set by downstream middlewares. | nil |
| Logger | *zerolog.Logger | Add a custom zerolog logger. | zerolog.New(os.Stderr).With().Timestamp().Logger() |
| GetLogger | func(fiber.Ctx) zerolog.Logger | Get a custom zerolog logger. If set, the returned logger replaces Logger. | nil |
| Fields | []string | Add the fields you want to log. | []string{"latency", "status", "method", "url", "error"} |
| SkipField | func(string, fiber.Ctx) bool | Skip logging a field when it returns true. | nil |
| SkipHeader | func(string, fiber.Ctx) bool | Skip logging a header when it returns true. | nil |
| RedactHeader | func(string, fiber.Ctx) bool | Replace every value of a header with [REDACTED] when it returns true. Common credential-bearing headers such as Authorization, Cookie, Set-Cookie, Location, X-Api-Key, X-Auth-Token, and CSRF-token headers are redacted by default. | built-in sensitive-header filter |
| WrapHeaders | bool | Wrap headers into a dictionary. If false: {"method":"POST", "header-key":"header value"}If true: {"method":"POST", "reqHeaders":{"header-key":"header value"}} | false |
| FieldsSnakeCase | bool | Use snake case for FieldResBody, FieldQueryParams, FieldBytesReceived, FieldBytesSent, FieldRequestID, FieldReqHeaders, FieldResHeaders.If false: {"method":"POST", "resBody":"v", "queryParams":"v"}If true: {"method":"POST", "res_body":"v", "query_params":"v"} | false |
| Messages | []string | Custom response messages. | []string{"Server error", "Client error", "Success"} |
| Levels | []zerolog.Level | Custom response levels. | []zerolog.Level{zerolog.ErrorLevel, zerolog.WarnLevel, zerolog.InfoLevel} |
| GetResBody | func(c fiber.Ctx) []byte | Define a function to get the response body when it returns non-nil. For example, with compress middleware the body can be unreadable; GetResBody lets you provide a readable body. | nil |
The body and resBody fields never read a body stream, and
bytesReceived and bytesSent never read one to measure it. A request body
that is a stream or whose Content-Length exceeds BodyLimit (possible with
fiber.Config{StreamRequestBody: true}) is logged with an empty body, and
bytesReceived is the announced Content-Length (0 when chunked). A
streamed response (SendStream, SendStreamWriter, SendFile) is logged
with an empty resBody unless GetResBody provides one, and its announced
length as bytesSent (0 when unknown).
A multipart form that fasthttp parsed within BodyLimit is marshaled back for
body, as in Fiber's logger; use SkipField to leave it out.
Example
package main
import (
"os"
middleware "github.com/gofiber/contrib/v3/zerolog"
"github.com/gofiber/fiber/v3"
"github.com/rs/zerolog"
)
func main() {
app := fiber.New()
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()
app.Use(middleware.New(middleware.Config{
Logger: &logger,
}))
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})
if err := app.Listen(":3000"); err != nil {
logger.Fatal().Err(err).Msg("Fiber app error")
}
}