0138: Log structured events with stdlib logging¶
- Implementation partial
Context and Problem Statement¶
Heinzelmännchen logs with stdlib logging and formatted messages (log.info("automatic update failed: %s", exc)),
so a log line can be read but not queried, and two lines about the same operation share nothing a tool can join on.
The daemon writes its own JSON lines (TickLog), and new work on the desktop app and first-run start has begun to
invent its own event helpers, each with its own field names. Installs, updates, plugin installs and app recovery need
logs that say which step of which operation did what, how long it took and why it failed, without leaking secrets.
What is the one way to log?
Decision Drivers¶
- One way to do a thing (CODING_STANDARDS); no new dependency for it.
- Logs an agent or a support tool can filter by event, operation, plugin and outcome.
- No secret, token or email address in a log, whoever wrote the call.
- Works with the handlers the heads already have (Rich on the terminal, rotating files for the daemon).
Considered Options¶
- Stdlib logging with event names as messages and fields as
extra, one kernel helper (chosen) - structlog: a good library, but a new dependency and a second logging pipeline next to stdlib, which every head, Uvicorn and third-party library already use; the repository does not depend on it.
- Keep formatted messages, per-area helpers: what exists now; the helpers already disagree on field names.
Decision Outcome¶
Chosen option: stdlib logging with event names as messages and fields as extra, because it adds no dependency,
keeps every existing handler and gives one vocabulary that a formatter can write as JSON.
- The message of a log call is the event name,
area.operation.stepin lower-case dotted words (desktop.install.download,plugin.install.deps). Event names are stable: renaming one is a breaking change for whoever reads the logs. - Everything that varies is a field, passed as
extra:operation,step,phase(start|end),outcome(ok|error|skipped),duration_ms,plugin,version,path,error_type,error,reasonandcorrelation_id. New fields use these names where they fit; fields that areNoneare left out. heinzel.kernel._loggingis the helper:get_logger(area)givesheinzel.<area>;log_event(logger, name, **fields)logs one event;step(logger, name, **fields)logs a start event and one outcome event withduration_ms, turning an exception into theerroroutcome witherror_typeand the person's message, and re-raising it.- Every event of one operation carries one
correlation_id:correlate()sets it for an operation, and the outermoststepstarts one when none is set. - Fields are redacted by key name: any key containing token, secret, password, api key, authorization, cookie,
credential, private or session key, or email is written as
[redacted], also in nested values and in rawextra. - Levels:
DEBUGfor detail only a developer needs,INFOfor step starts and ok or skipped outcomes,WARNINGfor a degraded result the operation recovered from,ERRORfor a failed step. The configuredlog_levelstill filters. JsonFormatterwrites one JSON object per line (atin UTC,level,logger,event, fields,traceback);TextFormatterwritesevent key=value .... Alog_formatsetting (HZ_LOG_FORMAT,text|json) picks the formatter for the daemon's and the desktop app's log files; the terminal stays text.- Logger calls under
src/heinzelnever format their message (f-string,%,+,.formator%-arguments).
Consequences¶
- Good: logs can be filtered and joined by event, operation and correlation id; a failure's log carries the same facts as the two-line message the person saw; secrets stay out by construction.
- Good: the existing formatted calls keep working; the JSON formatter also writes records from other libraries.
- Bad: existing formatted calls and the per-area helpers must migrate, area by area; the daemon's
TickLogand the desktop log becomeJsonFormatterhandlers and thelog_formatsetting is added with them. - Bad: field names that stdlib reserves on a log record (
module,name,msg,filename) cannot be used;log_eventrefuses them.
Confirmation¶
A standards test parses every Python file under src/heinzel and fails when a logger call formats its message
outside an allowlist of pre-existing files whose counts may only go down. Unit tests of the kernel helper check event
names, fields, redaction, step outcomes, correlation and both formatters.
More Information¶
Built so far: the kernel helper, the formatters and the standards test. Follow-ups: move the desktop app's and the
first-run start's event helpers onto step/log_event, make TickLog a JsonFormatter handler, add the
log_format setting, export the helper from heinzel.kernel for plugins, and shrink the allowlist.