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/logSignature
zerolog.New(config ...zerolog.Config) fiber.HandlerConfig
| 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).
For a request Fiber's timeout middleware answered, GetResBody is not
called: status, resBody, bytesSent, requestId and resHeaders describe
the response the client received, so requestId is empty for the default
timeout response, which carries no X-Request-ID. Its handler may still be
using the request, so, as in Fiber's logger, the entry leaves out referer,
ips, ua, queryParams, body and reqHeaders, behind a trusted proxy
also ip and host, and bytesReceived is only the announced
Content-Length. Skip, SkipField, SkipHeader, RedactHeader and
GetLogger still receive c, whose request and response a timed-out handler
may still be using, so they should read neither. As Fiber's
logger does, the logger returns the timeout error for such a request, so middleware
registered before it, such as cache, leaves that response alone. An
error-handling middleware registered before it receives that error too, so it
should check c.IsAbandoned() before it writes a response. Register the logger
before timeout.New: after it, the logger runs before that middleware answers.
A multipart form that fasthttp parsed within BodyLimit is marshaled back for
body, as in Fiber's logger; use SkipField to leave it out. body does not
decode a Content-Encoding: such a body is logged as received.
Zerolog hooks get c.Context() through e.GetCtx(), not c itself: what
c.SetContext stored, such as the span of an otel middleware registered
before the logger, with its cancellation and deadline, and a fallback to
c.Locals. For a request the timeout middleware answered, the timed-out
handler may still be using c, so hooks get the context c had when the
logger was reached, without that fallback; PassLocalsToContext puts values
such as the request ID in that context as well.
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")
}
}