From 9443c5878a59e218d489d7793865404b9af6582d Mon Sep 17 00:00:00 2001 From: Dmitrii Safronov Date: Fri, 22 May 2026 16:16:17 +0400 Subject: [PATCH] fix: respect logger filters for validation errors Signed-off-by: Dmitrii Safronov --- .../schema_logger.py | 3 +- tests/private/test_schema_logger.py | 4 +-- tests/test_logging_compatibility.py | 32 +++++++++++++++++++ 3 files changed, 35 insertions(+), 4 deletions(-) diff --git a/src/logging_objects_with_schema/schema_logger.py b/src/logging_objects_with_schema/schema_logger.py index 3b4fbf8..1af1387 100644 --- a/src/logging_objects_with_schema/schema_logger.py +++ b/src/logging_objects_with_schema/schema_logger.py @@ -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") diff --git a/tests/private/test_schema_logger.py b/tests/private/test_schema_logger.py index f189285..d6ad39e 100644 --- a/tests/private/test_schema_logger.py +++ b/tests/private/test_schema_logger.py @@ -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, diff --git a/tests/test_logging_compatibility.py b/tests/test_logging_compatibility.py index 556e0be..5a30cd2 100644 --- a/tests/test_logging_compatibility.py +++ b/tests/test_logging_compatibility.py @@ -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