Skip to content

Latest commit

 

History

History
74 lines (57 loc) · 4.84 KB

README.md

File metadata and controls

74 lines (57 loc) · 4.84 KB
id
fiberzerolog

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

Property Type Description Default
Next func(*Ctx) bool Define a function to skip this middleware when returned true nil
Logger *zerolog.Logger Add custom zerolog logger. zerolog.New(os.Stderr).With().Timestamp().Logger()
GetLogger func(*fiber.Ctx) zerolog.Logger Get custom zerolog logger, if it's defined the returned logger will replace the Logger value. nil
Fields []string Add fields what you want see. []string{"latency", "status", "method", "url", "error"}
WrapHeaders bool Wrap headers to 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 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 []string Custom response messages. []string{"Server error", "Client error", "Success"}
Levels []zerolog.Level Custom response levels. []zerolog.Level{zerolog.ErrorLevel, zerolog.WarnLevel, zerolog.InfoLevel}
SkipURIs []string Skip logging these URI. []string{}
GetResBody func(c *fiber.Ctx) []byte Define 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")
    }
}