logger

package module
v0.0.0-...-700f45d Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2023 License: GPL-3.0 Imports: 4 Imported by: 0

README

It's an opionated fiber request logger middleware using slog library.

It allows adding attribute from anywhere within the handler.

Usage

registering middleware with custom config

app.Use(logger.New(logger.Config{
    // skip logger with filter
    Filter: func(ctx *fiber.Ctx) bool {
        return ctx.Path() == "/exclude"
    },
    // customize attributes with builtin tag
    BuiltinAttrs []string{PathTag,StatusTag}
    // adding custom attributes with custom function
    CustomAttr: []logger.CustomFunc{
		func(c *fiber.Ctx, r *slog.Record, e error) {
            r.AddAttrs(slog.String("custom_key", "custom_value"))
        },
    },
    // assign slog logger
    Logger: slog.Default(),
}))

Adding extra attribute from anywhere within handler

router.Post("/", func(ctx *fiber.Ctx) error {
    ...
    logger.AddAttrs(ctx, slog.Any("params", params))
    logger.Add(ctx, "params", params))
    ...
})

Documentation

Index

Constants

View Source
const (
	RefererTag        = "referer"
	UserAgentTag      = "user_agent"
	IPTag             = "ip"
	IPsTag            = "ips"
	LatencyTag        = "latency"
	LatencyHumanTag   = "latency_human"
	StatusTag         = "status"
	PathTag           = "path"
	UrlTag            = "url"
	MethodTag         = "method"
	BodyTag           = "body"
	BytesReceivedTag  = "bytes_received"
	BytesSentTag      = "bytes_sent"
	RequestHeadersTag = "request_headers"
	QueryStringTag    = "query_string"
)

Variables

This section is empty.

Functions

func Add

func Add(c *fiber.Ctx, args ...any)

func AddAttrs

func AddAttrs(c *fiber.Ctx, attrs ...slog.Attr)

func New

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

Types

type Config

type Config struct {
	Filter       func(*fiber.Ctx) bool
	CustomAttr   []CustomFunc
	BuiltinAttrs []string
	Logger       *slog.Logger
}

type CustomFunc

type CustomFunc func(*fiber.Ctx, *slog.Record, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳