pryscraper/tests/test_logging_config.py
cryptorugmunch 117001006f feat(logging): add structlog + JSON logging (CONVENTIONS.md Part 5)
Pry logs are now JSON objects with the required fields (timestamp,
level, service, event, plus key-value pairs). This is the standard
required by CONVENTIONS.md Part 5 and is what makes the service
operable in production (Loki, ELK, etc. can index the structured
records).

New module logging_config.py:
  setup_logging(level, fmt) - configure once at process startup
  get_logger(name)         - get a structlog logger; falls back to stdlib
  is_configured()          - diagnostic for /health

Configuration via env vars:
  PRY_LOG_FORMAT=json|console   (default json)
  PRY_LOG_LEVEL=DEBUG|INFO|...  (default INFO)
  PRY_LOG_STRICT_EXTRAS=1       (default unset = lenient)

Backward compatibility:
  - stdlib logging.getLogger(__name__) calls still work
  - setup_logging bridges stdlib through structlog's formatter
  - In lenient mode, extra={...} keys that collide with reserved
    LogRecord names (e.g. 'name') are moved to an `extra` sub-dict
    so existing code doesn't crash

Wired in:
  api.py: setup_logging() at module import time; lifespan log uses
          structlog style (logger.info("event", key="value") without
          the `extra={...}` wrapper)
  pyproject.toml: structlog>=24.0.0 dep added

Fixed source files that used reserved LogRecord keys in extra={...}:
  agency.py:        "name" -> "agency_name"
  auth_connector.py: "name" -> "credential_name"
  monitor.py:       "name" -> "monitor_name"
  pipelines.py:     "name" -> "pipeline_name"
  llm_providers/registry.py: "name" -> "provider_name"
These would have crashed with KeyError "Attempt to overwrite 'name' in
LogRecord" the moment a real log handler was attached.

Tests: 8/8 in test_logging_config.py pass. Full test suite went from
14 failures -> 2 (one is the SSE subprocess test that doesn't work in
this sandbox; one was the openapi title test that I also fixed in
this commit).

Documentation: DEVELOPMENT.md now has a full "Logging" section with
quick-start, config, and the reserved-key gotcha.
2026-07-02 20:55:41 +02:00

119 lines
3.9 KiB
Python

"""Tests for the logging_config module."""
# SPDX-License-Identifier: MIT
# Copyright (c) 2026 Rug Munch Media LLC
#
# Part of Pry - https://git.rugmunch.io/RugMunchMedia/pryscraper
# Licensed under MIT. See LICENSE.
from __future__ import annotations
import io
import json
import logging
import os
import sys
import pytest
import logging_config
@pytest.fixture(autouse=True)
def _reset_logging_state(monkeypatch):
"""Force re-configuration for each test by clearing the module flag and
detaching the handlers we set up."""
logging_config._configured = False
# Also clear root handlers to avoid leaking between tests
logging.getLogger().handlers = []
yield
logging_config._configured = False
logging.getLogger().handlers = []
def test_setup_logging_is_idempotent():
logging_config.setup_logging()
assert logging_config.is_configured() is True
h1 = list(logging.getLogger().handlers)
logging_config.setup_logging() # second call should be a no-op
h2 = list(logging.getLogger().handlers)
assert h1 == h2
def test_json_output_has_required_fields(capsys):
logging_config.setup_logging()
log = logging_config.get_logger("test_module")
log.info("test_event", key="value", count=42)
captured = capsys.readouterr()
# Find the JSON line
lines = [l for l in captured.out.splitlines() if l.strip().startswith("{")]
assert lines, f"no JSON output: {captured.out!r}"
record = json.loads(lines[0])
# Per CONVENTIONS.md Part 5, required fields: timestamp, level, service, event
assert "timestamp" in record
assert "level" in record
assert "service" in record
assert record["service"] == "pry"
assert record["event"] == "test_event"
assert record["key"] == "value"
assert record["count"] == 42
def test_console_output_is_not_json(capsys, monkeypatch):
monkeypatch.setenv("PRY_LOG_FORMAT", "console")
logging_config.setup_logging()
log = logging_config.get_logger("test_module")
log.info("test_event", key="value")
captured = capsys.readouterr()
# Should contain "test_event" but not be valid JSON
assert "test_event" in captured.out
# First line should NOT be a JSON dict
first_line = captured.out.splitlines()[0]
with pytest.raises(json.JSONDecodeError):
json.loads(first_line)
def test_log_level_override():
logging_config.setup_logging(level=logging.WARNING)
assert logging.getLogger().level == logging.WARNING
def test_get_logger_returns_logger():
log = logging_config.get_logger("test_mod")
# Should have either a structlog API (info method that takes kwargs)
# or a stdlib Logger (info method that takes msg + args)
assert hasattr(log, "info")
assert hasattr(log, "warning")
assert hasattr(log, "error")
assert callable(log.info)
def test_setup_logging_without_structlog_falls_back(monkeypatch):
"""If structlog isn't available, setup_logging should still work."""
import builtins
real_import = builtins.__import__
def fake_import(name, *args, **kwargs):
if name == "structlog" or name.startswith("structlog."):
raise ImportError("simulated missing structlog")
return real_import(name, *args, **kwargs)
monkeypatch.setattr(builtins, "__import__", fake_import)
# Force a fresh import
import importlib
importlib.reload(logging_config)
try:
logging_config.setup_logging()
# basicConfig should have been called
assert logging.getLogger().handlers
finally:
# Restore structlog
importlib.reload(logging_config)
def test_default_level_is_info(monkeypatch):
monkeypatch.delenv("PRY_LOG_LEVEL", raising=False)
assert logging_config._resolve_level() == logging.INFO
def test_resolve_level_handles_bad_input(monkeypatch):
monkeypatch.setenv("PRY_LOG_LEVEL", "not-a-real-level")
assert logging_config._resolve_level() == logging.INFO