Skip to content

Latest commit

 

History

History
 
 

fiberzap

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 
 
 

Fiberzap

Release Discord Test Security Linter

Zap 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/fiberzap
go get -u go.uber.org/zap

Signature

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

Config

Property Type Description Default
Next func(*Ctx) bool Define a function to skip this middleware when returned true nil
Logger *zap.Logger Add custom zap logger. zap.NewDevelopment()
Fields []string Add fields what you want see. []string{"latency", "status", "method", "url"}
Messages []string Custom response messages. []string{"Server error", "Client error", "Success"}
Levels []zapcore.Level Custom response levels. []zapcore.Level{zapcore.ErrorLevel, zapcore.WarnLevel, zapcore.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 (
    "log"

    "github.com/gofiber/fiber/v2"
    "github.com/gofiber/contrib/fiberzap"
    "go.uber.org/zap"
)

func main() {
    app := fiber.New()
    logger, _ := zap.NewProduction()

    app.Use(fiberzap.New(fiberzap.Config{
        Logger: logger,
    }))

    app.Get("/", func (c *fiber.Ctx) error {
        return c.SendString("Hello, World!")
    })

    log.Fatal(app.Listen(":3000"))
}