s3-orchestrator

logfmt

import "github.com/afreidah/s3-orchestrator/internal/observe/logfmt"

Package logfmt provides typed slog.Attr constructors for the project’s logging conventions.

Index

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.

const (
    // OutcomeOK marks a successful terminal log line.
    OutcomeOK = "ok"
    // OutcomeError marks a terminal failure.
    OutcomeError = "error"
    // OutcomeSkipped marks a no-op (precondition false, already done, etc).
    OutcomeSkipped = "skipped"
    // OutcomeTimeout marks a deadline-exceeded failure.
    OutcomeTimeout = "timeout"
    // OutcomeNotFound marks a 404-equivalent failure.
    OutcomeNotFound = "not_found"
)

func Component

func Component(name string) slog.Attr

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

func Err(err error) slog.Attr

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

func LoggerFromCtx(ctx context.Context, base *slog.Logger) *slog.Logger

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

func Outcome(value string) slog.Attr

Outcome returns a slog.Attr under the canonical “outcome” key. Use one of the Outcome* constants for the value.

func RequestIDFromCtx

func RequestIDFromCtx(ctx context.Context) slog.Attr

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

func SetRequestIDFunc(fn func(context.Context) string)

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

func TransformAttr(a slog.Attr) slog.Attr

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.

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

func NewErrAttrHandler

func NewErrAttrHandler(inner slog.Handler) *ErrAttrHandler

NewErrAttrHandler wraps inner so error-typed attribute values render as their Error() string instead of the JSON handler’s “{}” placeholder.

func (*ErrAttrHandler) Enabled

func (h *ErrAttrHandler) Enabled(ctx context.Context, lvl slog.Level) bool

Enabled forwards to the inner handler.

func (*ErrAttrHandler) Handle

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

Handle walks record attributes, converting error-typed values to strings, then delegates to the inner handler.

func (*ErrAttrHandler) WithAttrs

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

WithAttrs delegates after transforming any error-typed attrs in the scoping list so With(…) chains preserve the same rendering.

func (*ErrAttrHandler) WithGroup

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

WithGroup delegates unchanged; group nesting is handled by the inner handler.

Generated by gomarkdoc