log

package
v0.2.0 Latest Latest
Warning

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

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

Documentation

Overview

Package log provides slog-based structured logging for paper-board services.

Phase 1.0: minimal JSONHandler. Redaction rules + OTel correlation Phase 1.5.

Index

Constants

View Source
const REDACT = "[REDACTED]"

REDACT is the placeholder string substituted for redacted values.

Variables

This section is empty.

Functions

func DefaultDenyKeys added in v0.2.0

func DefaultDenyKeys() []string

DefaultDenyKeys returns the canonical denylist (lower-case match).

func DefaultPatterns added in v0.2.0

func DefaultPatterns() []*regexp.Regexp

DefaultPatterns returns the canonical regex set applied to string values.

func ErrorAttrs added in v0.2.0

func ErrorAttrs(err error) []any

ErrorAttrs returns slog attrs describing err — its sentinel kind and message. Designed to be spread into logger calls:

log.Pkg(ctx).Error("load profile failed", log.ErrorAttrs(err)...)
slog.LogAttrs(ctx, slog.LevelError, "x", log.ErrorAttrsAttr(err)...)

Returns nil for nil err so it composes harmlessly.

func ErrorAttrsAttr added in v0.2.0

func ErrorAttrsAttr(err error) []slog.Attr

ErrorAttrsAttr is the []slog.Attr variant for use with slog.LogAttrs.

func LevelFromEnv added in v0.2.0

func LevelFromEnv() slog.Level

LevelFromEnv reads LOG_LEVEL and returns the corresponding slog.Level. Accepted values (case-insensitive): debug, info, warn|warning, error|err. Defaults to info when LOG_LEVEL is unset, empty, or unrecognised.

func New

func New(w io.Writer, service string, level slog.Level) *slog.Logger

New returns a slog.Logger writing structured JSON to w. If w is nil, os.Stderr is used. Pass service name for the "service" attribute.

func OrgFromContext added in v0.2.0

func OrgFromContext(ctx context.Context) string

OrgFromContext returns the stored org_id, or "" if absent.

func Pkg

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

Pkg returns a logger pre-loaded with context attributes (request_id, trace_id, org_id, user_id). Use this everywhere instead of slog.Default() directly so auto-fields are included.

log.Pkg(ctx).Info("user created", "id", u.ID)

func RequestIDFromContext added in v0.2.0

func RequestIDFromContext(ctx context.Context) string

RequestIDFromContext returns the stored request_id, or "" if absent.

func RolesFromContext added in v0.2.0

func RolesFromContext(ctx context.Context) []string

RolesFromContext returns the stored roles slice, or nil if absent.

func SetDefault

func SetDefault(l *slog.Logger)

SetDefault installs `l` as the package default. Once called, slog.Default() returns this logger across the process.

func TraceIDFromContext added in v0.2.0

func TraceIDFromContext(ctx context.Context) string

TraceIDFromContext returns the stored W3C trace ID, or "" if absent.

func UserFromContext added in v0.2.0

func UserFromContext(ctx context.Context) string

UserFromContext returns the stored user_id, or "" if absent.

func WithOrg

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

WithOrg stores org_id (tenant context).

func WithRequestID

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

WithRequestID stores request_id in ctx for downstream Pkg() calls.

func WithRoles added in v0.2.0

func WithRoles(ctx context.Context, roles []string) context.Context

WithRoles stores the caller's role list (RBAC; populated by Auth middleware). Roles intentionally do not auto-attach to Pkg() loggers — they vary in size and most log lines do not need them.

func WithTraceID

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

WithTraceID stores W3C trace ID.

func WithUser

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

WithUser stores user_id.

Types

type RedactOpts added in v0.2.0

type RedactOpts struct {
	DenyKeys []string
	Patterns []*regexp.Regexp
}

RedactOpts extends the default deny/regex sets.

type RedactingHandler added in v0.2.0

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

RedactingHandler wraps an inner slog.Handler and redacts sensitive values from log records using two strategies in series:

  1. Key denylist — any attribute whose Key matches DenyKeys (case-insensitive) has its value replaced with REDACT.
  2. Value regex — any string-typed attribute (and the message) has matching substrings replaced with REDACT.

Defaults cover bearer tokens, JWT-shaped strings, sk-prefixed API keys, and email addresses. Callers may extend via RedactOpts.

func NewRedactingHandler added in v0.2.0

func NewRedactingHandler(inner slog.Handler, opts RedactOpts) *RedactingHandler

NewRedactingHandler wraps inner with default redaction merged with opts.

func (*RedactingHandler) Enabled added in v0.2.0

func (h *RedactingHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled forwards to inner.

func (*RedactingHandler) Handle added in v0.2.0

func (h *RedactingHandler) Handle(ctx context.Context, r slog.Record) error

Handle redacts the record's message + attrs, then forwards to inner.

func (*RedactingHandler) WithAttrs added in v0.2.0

func (h *RedactingHandler) WithAttrs(attrs []slog.Attr) slog.Handler

WithAttrs returns a new handler with attrs (pre-redacted) prepended on inner.

func (*RedactingHandler) WithGroup added in v0.2.0

func (h *RedactingHandler) WithGroup(name string) slog.Handler

WithGroup forwards.

Jump to

Keyboard shortcuts

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