Logger API

Piko ships a structured logger built on slog with three shipped outputs (pretty console, JSON, rotating file), context-propagation helpers, and a set of attribute constructors. Log levels map to a seven-level scheme: Trace, Debug, Info, Notice, Warn, Error, plus Piko-internal variants. Source of truth: wdk/logger/facade.go.

Accessors

func GetLogger(name string) Logger
func From(ctx context.Context, fallback Logger) (context.Context, Logger)
func WithLogger(ctx context.Context, l Logger) context.Context
func MustFrom(ctx context.Context) Logger
func HasLogger(ctx context.Context) bool

The name parameter groups loggers for filtering and level overrides. Typical convention: GetLogger("myapp.actions.customer").

Log levels

ConstantMeaning
LevelTraceMost verbose: Piko internals.
LevelDebugDetailed debugging.
LevelInfoGeneral operational (default).
LevelNoticeImportant events.
LevelWarnRecoverable issues.
LevelErrorErrors that need attention.

Attribute constructors

Prefer attribute constructors over raw slog.Attr for type safety:

String(key, value string) Attr
Strings(key string, value []string) Attr
Int(key string, value int) Attr
Int64(key string, value int64) Attr
Uint64(key string, value uint64) Attr
Float64(key string, value float64) Attr
Bool(key string, value bool) Attr
Time(key string, value time.Time) Attr
Duration(key string, value time.Duration) Attr
Error(err error) Attr
Field(key string, value any) Attr

Error is a single-argument helper that always emits the attribute under the key "error". To attach an error under a different key, use Field("custom_key", err.Error()) or build the slog.Attr manually.

Standard field keys

Use these constants to keep field names consistent across packages:

ConstantValue
FieldStrContextContextual attributes.
FieldStrMethodHTTP method.
FieldStrComponentComponent name.
FieldStrAdapterAdapter name.
FieldStrServiceService name.
FieldStrErrorError.
FieldStrPathURL path.
FieldStrFileFile path.
FieldStrDirDirectory path.

Integrations

Shipped integrations attach to the existing logger pipeline. Enable via their own constructor:

Sub-packagePurpose
logger_integration_sentrySentry error reporting.
logger_integration_otel_grpcOpenTelemetry export over gRPC.
logger_integration_otel_httpOpenTelemetry export over HTTP.

Bootstrap

piko.New(...) initialises the logger automatically. Application code configures output handlers through AddPrettyOutput, AddJSONOutput, and AddFileOutput inside the initialiser, not via bootstrap options.

func AddPrettyOutput(opts ...OutputOption)
func AddJSONOutput(opts ...OutputOption)
func AddFileOutput(ctx context.Context, name, path string, opts ...OutputOption)

AddPrettyOutput and AddJSONOutput write to stdout. AddFileOutput requires a context (controls the rotation goroutine), a name (identifies the output in logs), and a target path. It rotates the file in the background.

Each helper takes one or more OutputOption values:

OptionEffect
WithLevel(level slog.Level)Override the level for this output. Defaults to the LOG_LEVEL environment variable, or INFO when unset.
WithJSON()Emit JSON instead of the pretty/text format.
WithNoColour()Strip ANSI colour codes (set automatically for file outputs).

Example:

logger.AddPrettyOutput(logger.WithLevel(slog.LevelDebug))
logger.AddFileOutput(ctx, "errors", "/var/log/app.errors.log",
    logger.WithLevel(slog.LevelError),
    logger.WithJSON(),
)

For custom pipelines, construct a Logger directly and register it with the relevant integrations.

See also