ctxlog

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: MIT Imports: 5 Imported by: 0

README

go-ctxlog

CI Go Reference License

Context-aware structured logging helpers for Go's log/slog

Installation

go get github.com/philiprehberger/go-ctxlog

Usage

import "github.com/philiprehberger/go-ctxlog"
Attaching fields to context
ctx := ctxlog.With(ctx, "user_id", 42, "tenant", "acme")
ctx = ctxlog.WithAttrs(ctx, slog.String("service", "api"))
ctx = ctxlog.WithRequestID(ctx, "req-abc-123")
Logging with context fields
// Uses slog.Default() enriched with context fields
ctxlog.Logger(ctx).Info("request handled", "status", 200)

// Enrich a specific logger
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
ctxlog.From(ctx, logger).Error("something failed", "err", err)
HTTP middleware
mux := http.NewServeMux()
mux.HandleFunc("/", handler)

// Auto-generate request IDs
http.ListenAndServe(":8080", ctxlog.Middleware(mux))
// Use an existing header for the request ID
wrapped := ctxlog.MiddlewareWithHeader("X-Trace-ID")(mux)
http.ListenAndServe(":8080", wrapped)

API

Function Description
With(ctx, args...) Attach slog key-value pairs to context
WithAttrs(ctx, attrs...) Attach typed slog.Attr values to context
WithRequestID(ctx, id) Attach a request ID to context
RequestID(ctx) Extract request ID from context (returns "" if not set)
Logger(ctx) Returns slog.Default() enriched with all context fields
From(ctx, logger) Enrich a specific *slog.Logger with context fields
Middleware(next) HTTP middleware that generates a UUID v4 request ID
MiddlewareWithHeader(header) Middleware that reads request ID from a header, or generates one

Development

go test ./...
go vet ./...

License

MIT

Documentation

Overview

Package ctxlog provides context-aware structured logging helpers for Go's log/slog.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func From

func From(ctx context.Context, logger *slog.Logger) *slog.Logger

From enriches the provided logger with all fields, attributes, and request ID stored in the context.

func Logger

func Logger(ctx context.Context) *slog.Logger

Logger returns slog.Default() enriched with all fields, attributes, and request ID stored in the context.

func Middleware

func Middleware(next http.Handler) http.Handler

Middleware generates a UUID v4 request ID, injects it into the request context via WithRequestID, and sets the X-Request-ID response header.

func MiddlewareWithHeader

func MiddlewareWithHeader(headerName string) func(http.Handler) http.Handler

MiddlewareWithHeader returns middleware that reads the request ID from the specified header. If the header is present and non-empty, its value is used; otherwise a new UUID v4 is generated. The request ID is injected into the context and set as the X-Request-ID response header.

func RequestID

func RequestID(ctx context.Context) string

RequestID extracts the request ID from the context. Returns an empty string if no request ID has been set.

func With

func With(ctx context.Context, args ...any) context.Context

With attaches slog key-value pairs to the context. The args should be alternating key-value pairs as accepted by slog.Logger.With. Multiple calls to With accumulate fields.

func WithAttrs

func WithAttrs(ctx context.Context, attrs ...slog.Attr) context.Context

WithAttrs attaches typed slog.Attr values to the context. Multiple calls to WithAttrs accumulate attributes.

func WithRequestID

func WithRequestID(ctx context.Context, id string) context.Context

WithRequestID attaches a request ID to the context.

Types

This section is empty.

Jump to

Keyboard shortcuts

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