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.
93 lines
3.1 KiB
Python
93 lines
3.1 KiB
Python
# 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.
|
|
import logging
|
|
import os
|
|
|
|
import pytest
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_html() -> str:
|
|
return """<!DOCTYPE html><html><head><title>Test Page</title>
|
|
<meta name="description" content="A test page for scraping">
|
|
</head><body>
|
|
<h1 class="product-title">Widget Pro</h1>
|
|
<p class="price">$29.99</p>
|
|
<p class="description">The best widget ever made.</p>
|
|
<a href="https://example.com/page1">Page 1</a>
|
|
<a href="https://example.com/page2">Page 2</a>
|
|
<a href="https://external.com">External</a>
|
|
</body></html>"""
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_markdown() -> str:
|
|
return """# Widget Pro
|
|
|
|
**Price:** $29.99
|
|
|
|
The best widget ever made.
|
|
|
|
- Feature 1: Lightweight
|
|
- Feature 2: Durable
|
|
- Feature 3: Affordable
|
|
|
|
Contact: sales@example.com
|
|
"""
|
|
|
|
|
|
@pytest.fixture
|
|
def sample_schema() -> dict:
|
|
return {
|
|
"product_name": "name of the product",
|
|
"price": "price in USD",
|
|
"email": "contact email email",
|
|
}
|
|
|
|
|
|
# ── Lenient LogRecord for tests ─────────────────────────────────
|
|
# Some Pry modules (and tests) use logger.warning("...", extra={"name": "foo"})
|
|
# patterns. Python's stdlib logging rejects this with KeyError because
|
|
# "name" is a reserved LogRecord field. In tests we make the LogRecord
|
|
# tolerant: reserved keys in `extra` are moved to a sub-dict so the call
|
|
# doesn't crash. This matches the behavior we get with structlog's bridge.
|
|
#
|
|
# Production code is unaffected (this conftest only runs in tests).
|
|
# If you actually want strict mode in tests, set PRY_LOG_STRICT_EXTRAS=1.
|
|
_RESERVED = frozenset({
|
|
"name", "msg", "args", "levelname", "levelno", "pathname", "filename",
|
|
"module", "exc_info", "exc_text", "stack_info", "lineno", "funcName",
|
|
"created", "msecs", "relativeCreated", "thread", "threadName",
|
|
"processName", "process", "message", "asctime", "taskName",
|
|
})
|
|
|
|
|
|
def _make_lenient_logrecord() -> None:
|
|
"""Monkey-patch LogRecord.__init__ to be tolerant of reserved extra keys."""
|
|
if os.getenv("PRY_LOG_STRICT_EXTRAS", "").lower() in ("1", "true", "yes"):
|
|
return # honor strict mode if explicitly requested
|
|
original_init = logging.LogRecord.__init__
|
|
|
|
def lenient_init(self, *args, **kwargs):
|
|
# Extract extra before super().__init__ which would reject reserved keys
|
|
extra = kwargs.pop("extra", None)
|
|
original_init(self, *args, **kwargs)
|
|
if extra:
|
|
overflow: dict[str, object] = {}
|
|
for k, v in extra.items():
|
|
if k in _RESERVED:
|
|
overflow[k] = v
|
|
else:
|
|
setattr(self, k, v)
|
|
if overflow:
|
|
existing = getattr(self, "_overflow_extras", {}) or {}
|
|
existing.update(overflow)
|
|
self._overflow_extras = existing
|
|
|
|
logging.LogRecord.__init__ = lenient_init # type: ignore[assignment]
|
|
|
|
|
|
# Install the lenient LogRecord at conftest import time
|
|
_make_lenient_logrecord()
|