Skip to main content

Module logging

Module logging 

Source
Expand description

Structured logging with the application log context — Rust port of the Java LogContextConfig + JsonLogger/CompactAppender design (org.platformlambda.core.logging).

Spans tell you the causal path; application logs tell you what happened inside each step. The log-context feature is on by default: the crate ships a built-in default-log-context.yaml (embedded at compile time) carrying the standard trace context, so every structured (JSON) log line emitted inside a traced function carries a context block — correlation id, trace/span ids, service name, and any business key-values added via PostOffice::update_context — with zero setup. An application replaces the template with its own app-log-context.yaml on the resource path, or opts out entirely with app.log.context=false (default true).

The template maps an output key (your choice) to one of three forms: a reserved $token ($cid, $traceId, $tracePath, $spanId, $parentSpanId, $service, $utc — resolved live per log line), a ${ENV:default} substitution (resolved once at load, via the standard ConfigReader), or a literal. A key that resolves to nothing is omitted, never printed as null.

init installs the process logger with three formats (the log4j2 appender-selection analog): the default text is a plain console line and — like Java’s plain Console appender — is unaffected by the log context; json pretty-prints each record (Java log4j2-json.xml); compact emits single-line jsonl records, no CR/LF (Java log4j2-compact.xml). -Dkey=value runtime arguments (the JVM -D analog) are honored, so -Dlog.format=json switches at launch without editing configuration. Deliberate simplifications (doc’d): UTC timestamps, no thread id.

Structs§

LogContextConfig
Parsed log-context template (Java LogContextConfig) — the application’s app-log-context.yaml when present, otherwise the built-in default.

Functions§

init
Install the process logger, reading log.format (text | json | compact, default text) and log.level (default info; RUST_LOG env wins) from the application configuration. -Dkey=value runtime arguments (the JVM -D analog) are loaded into the override registry first, so hello_world -- -Dlog.format=json switches format at launch. Idempotent — a second call is a no-op (the log crate accepts one logger per process).