-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Description
tests/test_json_logging.py thoroughly tests the JsonFormatter class itself (valid JSON output, required fields, ISO-8601 timestamps, no ANSI codes), but it does not test the module-level initialization logic that selects the formatter based on the JSON_LOG environment variable:
# main.py lines 191-196
_use_json_log: bool = bool(os.getenv("JSON_LOG"))
if _use_json_log:
USE_COLORS = False
handler.setFormatter(JsonFormatter() if _use_json_log else ColoredFormatter())This means a bug in the initialization path (e.g., bool(os.getenv("JSON_LOG")) being changed to always return False, or the ternary being accidentally inverted) would not be caught by the test suite. The JsonFormatter class could remain perfectly functional while the module initialization never selects it.
This gap was identified while reviewing the structured logging feature highlighted in Daily QAReport – March 5 #562.
Suggested Changes
Add 2 tests to tests/test_json_logging.py that verify module-level handler selection:
import importlib
import os
import logging
import unittest
class TestJsonLogEnvVarActivation(unittest.TestCase):
"""Verify JSON_LOG env var causes main.py to use JsonFormatter."""
def _reimport_main(self, env_vars: dict) -> types.ModuleType:
"""Reload main with patched env vars to test module-level init."""
with patch.dict(os.environ, env_vars, clear=False):
import sys
sys.modules.pop("main", None)
import main as m
return m
def test_json_log_env_var_selects_json_formatter(self):
"""Setting JSON_LOG=1 must cause the root handler to use JsonFormatter."""
with patch.dict(os.environ, {"JSON_LOG": "1"}):
import importlib, sys
sys.modules.pop("main", None)
import main as m
importlib.reload(m)
logger = logging.getLogger("control-d-sync")
if logger.handlers:
self.assertIsInstance(logger.handlers[0].formatter, m.JsonFormatter)
self.assertTrue(m._use_json_log)
def test_json_log_empty_string_disables_json_formatter(self):
"""JSON_LOG='' (empty) must leave _use_json_log as False."""
with patch.dict(os.environ, {"JSON_LOG": ""}):
import importlib, sys
sys.modules.pop("main", None)
import main as m
importlib.reload(m)
self.assertFalse(m._use_json_log)Files Affected
tests/test_json_logging.py— addTestJsonLogEnvVarActivationclass (2 methods)
Success Criteria
- ✅ New tests confirm
_use_json_log = TruewhenJSON_LOG=1 - ✅ New tests confirm
_use_json_log = FalsewhenJSON_LOG=(empty) - ✅ All existing tests continue to pass:
uv run pytest tests/ -v - ✅ No changes to
main.pyrequired
Source
Identified from review of structured logging coverage in context of Daily QAReport – March 5 #562 noting JSON_LOG as an active feature. The formatter class is tested; the env-var-driven initialization is not.
Priority
Low — 2-test addition that turns untested module-level initialization into a verified contract. Prevents silent regression if the bool(os.getenv("JSON_LOG")) initialization logic is ever refactored.
🔍 Task mining by Discussion Task Miner - Code Quality Improvement Agent
To install this agentic workflow, run
gh aw add github/gh-aw/.github/workflows/discussion-task-miner.md@94662b1dee8ce96c876ba9f33b3ab8be32de82a4
- expires on Mar 7, 2026, 3:19 AM UTC