Skip to content

Add opt-in JSON structured logging (LOG_FORMAT=json) - #207

Merged
jantman merged 1 commit into
mainfrom
robot-army/issue-52-json-logging
Nov 21, 2026
Merged

jantman merged 1 commit into
mainfrom
robot-army/issue-52-json-logging

Conversation

@jantman

@jantman jantman commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Closes #52.

Summary

  • New LOG_FORMAT env var: text (default, unchanged) or json, case-insensitive. Unknown values keep text and log a warning instead of stopping startup.
  • In JSON mode, every record mac-server logs (application, AUTH, library and HTTP access log) goes to stderr as one JSON object per line, with these fields:
    • timestamp (ISO 8601 with offset), level, logger, message
    • remote_addr and url from the Quart request context (null outside a request)
    • exc_info / stack_info when present
    • filename / lineno / funcName in -v mode
    • any extra= fields; values that can't be serialized are rendered with str()
  • Stdlib only: JsonFormatter and configure_log_format() in src/dm_mac/__init__.py, following the logging cookbook pattern linked in the issue. No new dependency.
  • Docs: env var table plus a new "JSON Logging" section in docs/source/configuration.rst, and CLAUDE.md.
  • Spec Kit artifacts are in specs/20261121-083531-json-logging/.

Pre-existing issue found (not changed here)

Today the root logger has two handlers, the logging.basicConfig() one and Quart's default_handler, so every text log line is printed twice (once without and once with [remote_addr]). This PR leaves text mode exactly as it is, to keep backward compatibility. JSON mode keeps only default_handler, so each record appears once. Fixing the text-mode duplication would change existing output, so it's worth a separate issue.

Verification

  • nox -s tests: 379 passed; src/dm_mac/__init__.py at 100% coverage.
  • nox -s pre-commit passes. mypy shows no new errors in the changed files.
  • Live run against the fixture configs, with a GET /api/machines and an authorized POST /api/machine/update:
    • LOG_FORMAT=json: 18 of 18 stderr lines parse as JSON, no duplicates, and AUTH lines include remote_addr/url.
    • LOG_FORMAT=json -v: 69 of 69 lines parse and include the source location.
    • Unset: output matches the format from before the change.
  • Quart's startup banner (* Serving Quart app ...) is printed to stdout rather than logged, so it is still plain text, the same as before.

🤖 Generated with Claude Code

https://claude.ai/code/session_01KQ8DrMX1VdrmZKGbKERH6h

Setting LOG_FORMAT=json makes mac-server write every log record
(application, AUTH, library and HTTP access log) to stderr as one JSON
object per line: timestamp (ISO 8601 with offset), level, logger,
message, remote_addr/url from the Quart request context, exc_info and
stack_info when present, filename/lineno/funcName in verbose mode, and
any extra= fields (non-serializable values rendered with str()).

Implementation is stdlib only: a JsonFormatter next to the existing
RequestFormatter, and configure_log_format() called from main(). In
JSON mode it leaves Quart's default_handler as the only root handler,
because today the root logger has both the basicConfig handler and
default_handler and every record is printed twice; reusing both would
duplicate JSON lines or mix text into the stream. Text mode (the
default) is untouched. Unknown LOG_FORMAT values keep text and log a
warning rather than preventing startup.

Docs: LOG_FORMAT in the configuration env var table plus a JSON
Logging section with the field list; CLAUDE.md updated. Spec Kit
artifacts in specs/20261121-083531-json-logging/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KQ8DrMX1VdrmZKGbKERH6h

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code review
From Claude, via the Claude PR Review workflow -- hence github-actions[bot].

Adds an opt-in LOG_FORMAT=json mode with a stdlib JsonFormatter and configure_log_format(), plus docs and tests. I found no high-signal bugs or CLAUDE.md violations. I read the diff directly and did not launch review subagents.

No issues found. Checked for bugs and CLAUDE.md compliance.


🔎 12s · 5 turns · $0.1150 · run log

@github-actions

Copy link
Copy Markdown

Coverage

Coverage Report
FileStmtsMissBranchBrPartCoverMissing
src/dm_mac
   __init__.py1160280100% 
   cli_utils.py15000100% 
   neon_fob_adder.py2321560593%78->79, 79, 116–117, 124, 208->205, 269->270, 270, 333–334, 341, 364–367, 440->445, 454–456, 551->549
   neongetter.py231160399%313->314, 314, 455->459, 535->537
   slack_handler.py2200560100% 
   utils.py25040100% 
   webhook.py620100100% 
src/dm_mac/models
   __init__.py0000100% 
   api_schemas.py49000100% 
   machine.py658152181696%670->671, 671, 677->exit, 750->751, 751, 799->801, 1125->1126, 1126–1128, 1191->1193, 1243->1244, 1244–1253, 1416->exit
   users.py1030320100% 
src/dm_mac/views
   __init__.py0000100% 
   api.py41000100% 
   machine.py1030120100% 
   prometheus.py132012199%67->69
TOTAL1987314922598% 
Tests Skipped Failures Errors Time
379 0 💤 0 ❌ 0 🔥 25.452s ⏱️

@jantman
jantman merged commit 7ebc499 into main Nov 21, 2026
17 checks passed
@jantman
jantman deleted the robot-army/issue-52-json-logging branch November 21, 2026 14:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JSON logging

1 participant