Skip to main content
Version: websocket_v1.x.x

Fiberzerolog

Release Discord Test Security Linter

Zerolog logging support for Fiber.

Note: Requires Go 1.18 and above

Install

This middleware supports Fiber v2.

go get -u github.com/gofiber/fiber/v2
go get -u github.com/gofiber/contrib/fiberzerolog
go get -u github.com/rs/zerolog/log

Signature

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

Config

PropertyTypeDescriptionDefault
Nextfunc(*Ctx) boolDefine a function to skip this middleware when returned truenil
Logger*zerolog.LoggerAdd custom zerolog logger.zerolog.New(os.Stderr).With().Timestamp().Logger()
GetLoggerfunc(*fiber.Ctx) zerolog.LoggerGet custom zerolog logger, if it's defined the returned logger will replace the Logger value.nil
Fields[]stringAdd fields what you want see.[]string{"latency", "status", "method", "url", "error"}
WrapHeadersboolWrap headers to dictionary.
If false: {"method":"POST", "header-key":"header value"}
If true: {"method":"POST", "reqHeaders": {"header-key":"header value"}}
false
FieldsSnakeCaseboolUse snake case for fields: 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[]stringCustom response messages.[]string{"Server error", "Client error", "Success"}
Levels[]zerolog.LevelCustom response levels.[]zerolog.Level{zerolog.ErrorLevel, zerolog.WarnLevel, zerolog.InfoLevel}
SkipURIs[]stringSkip logging these URI.[]string{}
GetResBodyfunc(c *fiber.Ctx) []byteDefine a function to get response body when return non-nil.
eg: When use compress middleware, resBody is unreadable. you can set GetResBody func to get readable resBody.
nil

Example

package main

import (
"github.com/gofiber/fiber/v2"
"github.com/gofiber/contrib/fiberzerolog"
"github.com/rs/zerolog"
)

func main() {
app := fiber.New()
logger := zerolog.New(os.Stderr).With().Timestamp().Logger()

app.Use(fiberzerolog.New(fiberzerolog.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")
}
}