
logfmt
Package logfmt provides typed slog.Attr constructors for the project’s logging conventions.
Index
- Constants
- func Component(name string) slog.Attr
- func Err(err error) slog.Attr
- func LoggerFromCtx(ctx context.Context, base *slog.Logger) *slog.Logger
- func Outcome(value string) slog.Attr
- func RequestIDFromCtx(ctx context.Context) slog.Attr
- func SetRequestIDFunc(fn func(context.Context) string)
- func TransformAttr(a slog.Attr) slog.Attr
- type ErrAttrHandler
- func NewErrAttrHandler(inner slog.Handler) *ErrAttrHandler
- func (h *ErrAttrHandler) Enabled(ctx context.Context, lvl slog.Level) bool
- func (h *ErrAttrHandler) Handle(ctx context.Context, r slog.Record) error
- func (h *ErrAttrHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *ErrAttrHandler) WithGroup(name string) slog.Handler
Constants
Outcome values for the canonical “outcome” attribute. Use these constants rather than ad-hoc strings so dashboards can rely on a closed value set.
func Component
Component returns a slog.Attr under the canonical “component” key. Use at logger construction so every log from a long-lived service carries the same component label, not in the message string.
func Err
Err returns a slog.Attr carrying err.Error() under the canonical “error” key. Returns an empty Attr (which slog drops) when err is nil so callers can use it unconditionally.
Always prefer Err over passing the raw error: slog’s JSON handler serializes complex error types as {} via encoding/json, which renders as “[object Object]” in JS log viewers (Nomad UI, Grafana Loki) and hides the actual failure mode from operators.
func LoggerFromCtx
LoggerFromCtx returns base scoped with the request ID (if any) so all subsequent log calls inherit the correlation key. Constructor-equivalent of dbtools’ WithStr chaining.
func Outcome
Outcome returns a slog.Attr under the canonical “outcome” key. Use one of the Outcome* constants for the value.
func RequestIDFromCtx
RequestIDFromCtx returns a slog.Attr under the canonical “request_id” key when one is set on ctx, or an empty Attr otherwise. Lets worker loggers surface the inbound request ID for ops triggered by an admin call without coupling to the audit package’s API surface.
func SetRequestIDFunc
SetRequestIDFunc registers the context accessor used by RequestIDFromCtx. Called by the audit package’s init so this package does not import audit directly.
func TransformAttr
TransformAttr returns a if its value is not an error, otherwise returns a new slog.Attr with the same key and value `a.Value.Any().(error).Error()`. Group values are recursed into so nested groups carrying errors also render as strings. Exported so non-handler call sites (e.g. the in-memory log buffer) can apply the same rule outside the slog.Handler chain.
type ErrAttrHandler
ErrAttrHandler wraps a slog.Handler and replaces any attribute value of type error (passed via slog.Any or as a raw key/value pair) with the result of err.Error(). Group attributes are recursed into. Nil errors land as empty strings under the original key.
func NewErrAttrHandler
NewErrAttrHandler wraps inner so error-typed attribute values render as their Error() string instead of the JSON handler’s “{}” placeholder.
func (*ErrAttrHandler) Enabled
Enabled forwards to the inner handler.
func (*ErrAttrHandler) Handle
Handle walks record attributes, converting error-typed values to strings, then delegates to the inner handler.
func (*ErrAttrHandler) WithAttrs
WithAttrs delegates after transforming any error-typed attrs in the scoping list so With(…) chains preserve the same rendering.
func (*ErrAttrHandler) WithGroup
WithGroup delegates unchanged; group nesting is handled by the inner handler.
Generated by gomarkdoc