logger

package
v0.0.26 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2026 License: MIT Imports: 9 Imported by: 0

README

Logger Middleware

What it does

Writes access logs with request/response metadata such as method, path, status, latency, IP, request ID, and errors.

How to implement

package main

import (
	"github.com/oarkflow/fh"
	"github.com/oarkflow/fh/mw/logger"
)

func main() {
	app := fh.New()
	app.Use(logger.New(logger.Config{}))

	app.Get("/", func(c fh.Ctx) error {
		return c.Status(fh.StatusOK).SendString("ok")
	})
}

Impact

Adds I/O overhead; structured asynchronous logging is preferred for high throughput.

Ordering guidance

Run after request/correlation ID and real IP so logs include normalized identifiers. Usually wraps most middleware.

Production considerations

Redact secrets, authorization headers, cookies, and sensitive query parameters. Use sampling for very high RPS routes.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(config ...Config) fh.HandlerFunc

Types

type Config

type Config struct {
	// Format is used when FormatName is empty or unknown.
	//
	// Supported tokens:
	// ${time} ${ip} ${method} ${path} ${query} ${uri} ${status} ${latency} ${error}
	Format string

	// FormatName can be:
	// default, common, combined, tiny, json
	FormatName string

	// TimeFormat defaults to time.RFC3339.
	TimeFormat string

	// Logger enables fh.Logger output (defaults to log/slog).
	// If nil and Slog is nil and Writers is empty, fh.NewDefaultLogger() is used.
	Logger fh.Logger

	// Slog enables structured slog output.
	Slog *slog.Logger

	// SlogLevel defaults to slog.LevelInfo.
	SlogLevel slog.Level

	// Writers receives rendered text/json log lines.
	// Writers are called only from the async worker.
	Writers []io.Writer

	// QueueSize defaults to 4096.
	QueueSize int

	// MaxLineBytes caps one rendered log line.
	// Defaults to 4096. Larger logs are truncated.
	MaxLineBytes int

	// DropPolicy controls behavior when async queue is full.
	// Default: DropNewest.
	DropPolicy DropPolicy

	// Skip allows custom skipping before logging.
	// It runs before ctx.Next(). If true, no log is written.
	Skip func(fh.Ctx) bool

	// SkipAfter allows custom skipping after ctx.Next().
	// Useful for status-code based rules.
	SkipAfter func(fh.Ctx, error) bool

	// SkipDefaultStatic skips common static asset types.
	// Defaults to true.
	SkipDefaultStatic bool

	// SkipExtensions are matched case-insensitively.
	// Example: []string{".css", ".js", ".png"}
	SkipExtensions []string

	// SkipPaths are exact path matches.
	SkipPaths []string

	// SkipPrefixes are prefix path matches.
	SkipPrefixes []string

	// SkipMethods are exact method matches.
	SkipMethods []string

	// SkipStatusCodes skips after request handling.
	SkipStatusCodes []int

	// IncludeQueryInPath makes ${path} include query string.
	IncludeQueryInPath bool

	// DisableAsync is not recommended.
	// If true, writes are performed in request path.
	DisableAsync bool
}

type DropPolicy

type DropPolicy uint8
const (
	// DropNewest drops the current log entry when queue is full.
	DropNewest DropPolicy = iota

	// DropOldest removes one queued entry and inserts the current one.
	DropOldest
)

type Middleware

type Middleware struct {
	// contains filtered or unexported fields
}

func NewMiddleware

func NewMiddleware(config ...Config) *Middleware

func (*Middleware) Close

func (m *Middleware) Close() error

func (*Middleware) Dropped

func (m *Middleware) Dropped() uint64

func (*Middleware) Handler

func (m *Middleware) Handler() fh.HandlerFunc

Jump to

Keyboard shortcuts

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