Skip to content

feat(logging): add request ID and JSON logs #620

Closed

Description

@uniqueg

Problem

FOCA apps can't correlate the log lines of one request: there is no app-wide request ID and no machine-readable log format. The plugin engine has its own request_id(), but nothing else does. Apps such as cloud-registry set logger levels in code, because the log config model has no loggers or filters sections.

Proposal

Framework-neutral except for the setter; no new runtime dependency.

  1. foca.context.request_id(), held in a ContextVar. This creates the foca.context module, which the request accessors in 0.22.0 extend. The plugin engine's request_id() uses it.
  2. A plain WSGI middleware around app.wsgi_app, not Flask hooks. An ASGI port follows with 1.0.0-rc1.
    • Header from server.request_id.header (default X-Request-ID).
    • An incoming value is accepted only if it matches ^[A-Za-z0-9._:-]{1,128}$ and server.request_id.trust_incoming is on (default on); otherwise uuid4().hex.
    • The ID is echoed in the response header.
  3. foca.utils.logging.ContextFilter adds request_id, operation_id, subject and client to every record; empty outside requests; Celery uses the task ID.
  4. foca.utils.logging.JsonFormatter (stdlib): one JSON object per line with timestamp, level, logger, message, context fields, exc_info and extra keys. No double escaping of what escape_log_value does for text logs.
  5. Config model: log.filters, log.loggers, formatter extras and a built-in formatter json. The default stays standard.
  6. foca.gunicorn_config: access log format with the request ID.

The ID is for log correlation only. Traces, metrics and health endpoints are out of scope.

Acceptance criteria

  • A valid incoming ID is echoed; an invalid or missing one, or trust_incoming: false, gives a generated ID; the header name is configurable.
  • Records in a request carry the fields; outside, they are empty and nothing fails.
  • formatter: json gives one valid JSON object per line, including exceptions and extra fields.
  • log.loggers and log.filters are validated and passed to dictConfig.
  • Defaults unchanged, not breaking. 100% coverage, mypy, ruff, strict docs build; docs section on request IDs and JSON logs.

Activity

  1. added this to the v0.21.0 milestone on Nov 3, 2026
  2. added
    priority: medium
    status: triage
    type: feature
    workload: days
    status: in progress
    on Nov 3, 2026
  3. removed
    status: in progress
    on Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: mediumstatus: triagetype: featureworkload: days

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions