Skip to main content
Version: opafiber_v1.x.x

Fiberzerolog

Release Discord Test Security Linter

Zerolog logging support for Fiber.

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 ...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"}
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")
}
}