logging

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package logging provides a slog.Handler that enriches log records with trace_id, span_id, request_id, correlation_id, and cell_id from the request context. Supports JSON and text output formats.

Security contract: contextHandler is the process-global sink-side redaction barrier. Every Handle call scrubs log messages with pkg/redaction.RedactString and every attr with pkg/redaction.RedactSlogAttr (key-aware mask + free-form regex). Pre-bound attrs (logger.With) are redacted at bind time in WithAttrs. Call-site redaction helpers (RedactAny, RedactSlogAttr at call sites) are defense-in-depth only — the handler guarantees fail-closed scrubbing regardless.

Enforcement: SLOG-HANDLER-SEALED-FUNNEL-01 in tools/archtest/slog_handler_sealed_funnel_test.go.

Package logging provides a slog.Handler that enriches log records with trace_id, span_id, request_id, correlation_id, and cell_id from the request context. Supports JSON and text output formats.

ref: go stdlib log/slog — Handler interface + Handle(context, Record) pattern Adopted: wrapping an inner slog.Handler, extracting context values in Handle. Deviated: adds GoCell-specific ctxkeys (request_id, correlation_id, cell_id).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(opts Options) slog.Handler

NewHandler creates a slog.Handler that redacts every record (fail-closed) and enriches it with GoCell context values. It builds a fresh JSON/Text sink writing to opts.Writer (default os.Stdout).

Security contract: every Handle call scrubs the log message with redaction.RedactString and every attr with redaction.RedactSlogAttr (two-layer: key-aware mask + free-form regex). WithAttrs pre-redacts at bind time. This makes contextHandler the process-global redaction barrier — call-site redaction helpers are defense-in-depth only.

Production entry points must seal the process-global slog default before any log emission:

slog.SetDefault(slog.New(logging.NewHandler(logging.Options{...})))

This is enforced by archtest SLOG-HANDLER-SEALED-FUNNEL-01 A3 in two segments: generated assemblies are sealed by the assembly template and locked by generated-verify + marker-derived archtest coverage (Hard, #1401 delivered); hand-written entry points are sealed by a bounded allowlist (Medium, residual tracked at gh #1424).

Building a fresh sink (rather than wrapping the stdlib default) avoids the slog↔log recursion that wrapping the default handler would trigger.

Types

type Format

type Format string

Format specifies the log output format.

const (
	// FormatJSON outputs structured JSON logs.
	FormatJSON Format = "json"
	// FormatText outputs human-readable text logs.
	FormatText Format = "text"
)

type Options

type Options struct {
	// Level is the minimum log level. Defaults to slog.LevelInfo.
	Level slog.Leveler
	// Format selects between JSON and text output. Defaults to FormatJSON.
	Format Format
	// Writer is the output destination. Defaults to os.Stdout.
	Writer io.Writer
}

Options configures the logging handler.

Jump to

Keyboard shortcuts

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