Zap
Zap 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/zap
go get -u go.uber.org/zapSignature
zap.New(config ...zap.Config) fiber.HandlerConfig
| Property | Type | Description | Default |
|---|---|---|---|
| Next | func(fiber.Ctx) bool | Define a function to skip this middleware when returned true | nil |
| Logger | *zap.Logger | Add custom zap logger. | zap.NewProduction() |
| Fields | []string | Add fields that you want to see. | []string{"latency", "status", "method", "url"} |
| FieldsFunc | func(fiber.Ctx) []zap.Field | Define a function to add custom fields. | nil |
| 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 |
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: resBody is the body of the response the client received.
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. FieldsFunc and
SkipResBody should not read the request of such a c either.
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 should check c.IsAbandoned()
before it writes a response.
A multipart form that fasthttp parsed within BodyLimit is marshaled back for
body, as in Fiber's logger; use SkipBody to leave it out. body does not
decode a Content-Encoding: such a body is logged as received.
Example
package main
import (
"log"
middleware "github.com/gofiber/contrib/v3/zap"
"github.com/gofiber/fiber/v3"
"go.uber.org/zap"
)
func main() {
app := fiber.New()
logger, _ := zap.NewProduction()
defer logger.Sync()
app.Use(middleware.New(middleware.Config{
Logger: logger,
}))
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})
log.Fatal(app.Listen(":3000"))
}NewLogger
Signature
zap.NewLogger(config ...zap.LoggerConfig) *zap.LoggerConfigLoggerConfig
| Property | Type | Description | Default |
|---|---|---|---|
| CoreConfigs | []CoreConfig | Define Config for zapcore | zap.LoggerConfigDefault |
| SetLogger | *zap.Logger | Add custom zap logger. if not nil, ZapOptions, CoreConfigs, SetLevel, SetOutput will be ignored. | nil |
| ExtraKeys | []string | Allow users log extra values from context. | []string{} |
| ZapOptions | []zap.Option | Allow users to configure the zap.Option supplied by zap. | []zap.Option{} |
Example
package main
import (
"context"
middleware "github.com/gofiber/contrib/v3/zap"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
)
func main() {
app := fiber.New()
logger := middleware.NewLogger(middleware.LoggerConfig{
ExtraKeys: []string{"request_id"},
})
log.SetLogger(logger)
defer logger.Sync()
app.Use(func(c fiber.Ctx) error {
ctx := context.WithValue(c.Context(), "request_id", "123")
c.SetContext(ctx)
return c.Next()
})
app.Get("/", func(c fiber.Ctx) error {
log.WithContext(c.Context()).Info("Hello, World!")
return c.SendString("Hello, World!")
})
log.Fatal(app.Listen(":3000"))
}