fiberslog

package module
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 24, 2026 License: MIT Imports: 5 Imported by: 0

README

fiberslog

CI CodeQL codecov Go Reference

Fiber middleware for structured request logging with slog.

Install

go get github.com/gringolito/fiberslog

Usage

import (
    "log/slog"

    "github.com/gofiber/fiber/v2"
    "github.com/gringolito/fiberslog"
)

app := fiber.New()
logger := slog.Default()

app.Use(fiberslog.New(fiberslog.Config{
    Logger: logger,
    Fields: []string{"latency", "status", "method", "path", "requestId"},
}))

Features

  • Configurable logged fields (22 built-in fields, see reference below)
  • URI skip list
  • Conditional body and response body logging
  • Automatic level mapping based on HTTP status code (2xx→Info, 4xx→Warn, 5xx→Error)
  • Sensitive header redaction via SkipHeaders

Field Reference

The Fields config option accepts any combination of the names below. Unknown names are silently ignored. The default set is latency, status, method, url, pid.

Field Log attribute key Type Description
latency latency time.Duration Time to process the request
status status int HTTP response status code
method method string HTTP request method
url url string Full original URL (includes query string)
path path string Request path without query string
pid pid int Process ID
ip ip string Client IP address
ips ips string X-Forwarded-For header value
host host string Request hostname
port port string Request port
protocol protocol string Request protocol (http or https)
referer referer string Referer header value
user-agent user-agent string User-Agent header value
requestId requestId string X-Request-ID response header
route route string Matched Fiber route pattern
queryParams queryParams string Serialized query parameters
body body []byte Request body bytes
responseBody responseBody []byte Response body bytes
bytesReceived bytesReceived int Request body size in bytes
bytesSent bytesSent int Response body size in bytes
requestHeaders (one attr per header) []byte All request headers (see Security)
responseHeaders (one attr per header) []byte All response headers (see Security)

Security

requestHeaders and responseHeaders log all headers verbatim, including Authorization, Cookie, and API keys. Use SkipHeaders to redact sensitive ones:

app.Use(fiberslog.New(fiberslog.Config{
    Fields:      []string{"latency", "status", "method", "requestHeaders"},
    SkipHeaders: []string{"Authorization", "Cookie"},
}))

Header name matching is case-insensitive.

Contributing

Contributions are welcome! Feel free to:

  • Open an issue to report a bug or request a new feature
  • Submit a pull request — please include tests for any new behavior
  • Suggest ideas or improvements by starting a discussion

This project has moved from the Beerware License to MIT, but the spirit lives on: if we ever meet and you think this stuff is worth it, you're still very welcome to buy me a beer.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

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

New returns a fiber.Handler (middleware) that logs requests using slog. New creates a new middleware handler

Types

type Config

type Config struct {
	// Next defines a function to skip this middleware when returned true.
	//
	// Optional. Default: nil
	Next func(c *fiber.Ctx) bool

	// SkipBody defines a function to skip log  "body" field when returned true.
	//
	// Optional. Default: nil
	SkipBody func(c *fiber.Ctx) bool

	// SkipResBody defines a function to skip log  "resBody" field when returned true.
	//
	// Optional. Default: nil
	SkipResBody func(c *fiber.Ctx) bool

	// GetResBody defines a function to get ResBody.
	//  eg: when use compress middleware, resBody is unreadable. you can set GetResBody func to get readable resBody.
	//
	// Optional. Default: nil
	GetResBody func(c *fiber.Ctx) []byte

	// Skip logging for these uri
	//
	// Optional. Default: nil
	SkipURIs []string

	// Add custom slog logger.
	//
	// Optional. Default: slog.Default()
	Logger *slog.Logger

	// Add fields what you want see.
	//
	// Optional. Default: {"latency", "status", "method", "url", "pid"}
	Fields []string

	// SkipHeaders is a list of header names to exclude from requestHeaders logging.
	// Matching is case-insensitive. Use this to redact sensitive headers such as
	// Authorization or Cookie.
	// Warning: if omitted, ALL headers including credentials will be logged.
	//
	// Optional. Default: nil
	SkipHeaders []string
}

Directories

Path Synopsis
examples
basic command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL