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
- func DefaultDenyKeys() []string
- func DefaultPatterns() []*regexp.Regexp
- func ErrorAttrs(err error) []any
- func ErrorAttrsAttr(err error) []slog.Attr
- func LevelFromEnv() slog.Level
- func New(w io.Writer, service string, level slog.Level) *slog.Logger
- func OrgFromContext(ctx context.Context) string
- func Pkg(ctx context.Context) *slog.Logger
- func RequestIDFromContext(ctx context.Context) string
- func RolesFromContext(ctx context.Context) []string
- func SetDefault(l *slog.Logger)
- func TraceIDFromContext(ctx context.Context) string
- func UserFromContext(ctx context.Context) string
- func WithOrg(ctx context.Context, id string) context.Context
- func WithRequestID(ctx context.Context, id string) context.Context
- func WithRoles(ctx context.Context, roles []string) context.Context
- func WithTraceID(ctx context.Context, id string) context.Context
- func WithUser(ctx context.Context, id string) context.Context
- type RedactOpts
- type RedactingHandler
Constants ¶
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
DefaultPatterns returns the canonical regex set applied to string values.
func ErrorAttrs ¶ added in v0.2.0
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
ErrorAttrsAttr is the []slog.Attr variant for use with slog.LogAttrs.
func LevelFromEnv ¶ added in v0.2.0
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 ¶
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
OrgFromContext returns the stored org_id, or "" if absent.
func Pkg ¶
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
RequestIDFromContext returns the stored request_id, or "" if absent.
func RolesFromContext ¶ added in v0.2.0
RolesFromContext returns the stored roles slice, or nil if absent.
func SetDefault ¶
SetDefault installs `l` as the package default. Once called, slog.Default() returns this logger across the process.
func TraceIDFromContext ¶ added in v0.2.0
TraceIDFromContext returns the stored W3C trace ID, or "" if absent.
func UserFromContext ¶ added in v0.2.0
UserFromContext returns the stored user_id, or "" if absent.
func WithRequestID ¶
WithRequestID stores request_id in ctx for downstream Pkg() calls.
func WithRoles ¶ added in v0.2.0
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 ¶
WithTraceID stores W3C trace ID.
Types ¶
type RedactOpts ¶ added in v0.2.0
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:
- Key denylist — any attribute whose Key matches DenyKeys (case-insensitive) has its value replaced with REDACT.
- 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) Handle ¶ added in v0.2.0
Handle redacts the record's message + attrs, then forwards to inner.