Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/logging_objects_with_schema/schema_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ def _log(
sinfo, # sinfo - stack info from caller
)
try:
self.callHandlers(error_record)
self.handle(error_record)
except Exception:
# If handler failed, log error to stderr (standard logging behavior)
sys.stderr.write(f"Error in logging handler: {error_record}\n")
4 changes: 2 additions & 2 deletions tests/private/test_schema_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,11 @@ def mock_dumps(*args, **kwargs):
assert "validation_errors" in output


def test_schema_logger_log_handles_callhandlers_exception(
def test_schema_logger_log_handles_validation_error_handler_exception(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""SchemaLogger._log should handle callHandlers exception gracefully."""
"""SchemaLogger._log should handle validation error handler exceptions."""
monkeypatch.chdir(tmp_path)
_write_schema(
tmp_path,
Expand Down
32 changes: 32 additions & 0 deletions tests/test_logging_compatibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,35 @@ def test_schema_logger_accepts_extra_fields_without_error(

output = stream.getvalue()
assert "msg" in output


def test_schema_logger_validation_errors_respect_logger_filters(
tmp_path: Path,
monkeypatch: pytest.MonkeyPatch,
) -> None:
"""Validation error records should pass through logger filters."""

monkeypatch.chdir(tmp_path)
_write_schema(
tmp_path,
{
"ServicePayload": {
"RequestID": {"type": "str", "source": "request_id"},
},
},
)

stream = StringIO()
handler = logging.StreamHandler(stream)
handler.setFormatter(logging.Formatter("%(levelname)s:%(message)s"))

logger = SchemaLogger("test-filtered-validation-errors")
logger.addHandler(handler)
logger.setLevel(logging.INFO)
logger.addFilter(lambda record: not record.getMessage().startswith("{"))

logger.info("msg", extra={"request_id": 42})

output = stream.getvalue()
assert "INFO:msg" in output
assert "validation_errors" not in output