Repository navigation
fix(logger): match suppressed loggers by hierarchy - #8835
roonakyadav wants to merge 2 commits into
Conversation
SuppressFilter matched loggers via substring (`self.logger not in record.name`),
which unintentionally suppressed unrelated third-party loggers whose names
contained the service name (e.g. `botocore` when service was `core`).
Change the matching logic to match either the exact logger name or child
loggers prefixed with `{self.logger}.`. Add functional regression tests.
Closes aws-powertools#8821
|
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
Manishram-ai
left a comment
There was a problem hiding this comment.
Checked this out and ran pytest tests/functional/logger tests/unit/logger (205 passed). The new test fails on develop with assert 'credentials expire soon' in '', so it does catch the bug, and the fix itself looks right to me.
One thing about the test. It uses a fixed "core" service, and logging.getLogger("core") lives for the whole process. If the test runs a second time in the same process (reruns, --count, or another test that ever uses "core"), the logger is already marked as configured, _init_logger returns early, and no SuppressFilter is added to the new root handler. Then the child assertion fails:
$ pytest -q --keep-duplicates tests/functional/logger/required_dependencies/test_logger.py tests/functional/logger/required_dependencies/test_logger.py -k suppress_filter_does_not
E AssertionError: assert 'child message' not in 'credentials...ld message\n'
1 failed, 1 passed
The other tests in this file use the random service_name fixture for that reason. I left a suggestion that does the same and builds the unrelated name around it (f"boto{service_name}", like botocore for core). With it, the double run above gives 2 passed, the test still fails on develop with the same assertion, and ruff check and ruff format are clean.
| def test_suppress_filter_does_not_suppress_unrelated_logger_containing_service_name(stdout): | ||
| # GIVEN a root logger configured with a handler (as in Lambda runtime) | ||
| root_stream = io.StringIO() | ||
| root_handler = logging.StreamHandler(root_stream) | ||
| root_logger = logging.getLogger() | ||
| root_logger.addHandler(root_handler) | ||
|
|
||
| try: | ||
| # AND a Powertools logger with service "core" | ||
| Logger(service="core", stream=stdout) | ||
| child_logger = Logger(service="core", child=True, stream=stdout) | ||
| unrelated_logger = logging.getLogger("botocore") |
There was a problem hiding this comment.
Using the service_name fixture keeps this test independent of logger state left by other tests or a rerun:
| def test_suppress_filter_does_not_suppress_unrelated_logger_containing_service_name(stdout): | |
| # GIVEN a root logger configured with a handler (as in Lambda runtime) | |
| root_stream = io.StringIO() | |
| root_handler = logging.StreamHandler(root_stream) | |
| root_logger = logging.getLogger() | |
| root_logger.addHandler(root_handler) | |
| try: | |
| # AND a Powertools logger with service "core" | |
| Logger(service="core", stream=stdout) | |
| child_logger = Logger(service="core", child=True, stream=stdout) | |
| unrelated_logger = logging.getLogger("botocore") | |
| def test_suppress_filter_does_not_suppress_unrelated_logger_containing_service_name(stdout, service_name): | |
| # GIVEN a root logger configured with a handler (as in Lambda runtime) | |
| root_stream = io.StringIO() | |
| root_handler = logging.StreamHandler(root_stream) | |
| root_logger = logging.getLogger() | |
| root_logger.addHandler(root_handler) | |
| try: | |
| # AND a Powertools logger, plus an unrelated logger whose name contains the service name | |
| # (like "botocore" for service "core") | |
| Logger(service=service_name, stream=stdout) | |
| child_logger = Logger(service=service_name, child=True, stream=stdout) | |
| unrelated_logger = logging.getLogger(f"boto{service_name}") |
|



Closes #8821
Summary
Changes
aws_lambda_powertools/logging/filters.py, updateSuppressFilter.filter()to match the exact logger name or child loggers prefixed with{self.logger}., rather than checking substring membership (self.logger not in logger).tests/functional/logger/required_dependencies/test_logger.py, add functional testtest_suppress_filter_does_not_suppress_unrelated_logger_containing_service_namevalidating that unrelated loggers containing the service name (e.g.botocorewhen service iscore) are not suppressed, while genuine child loggers remain suppressed from root handlers.Root cause
SuppressFilter.filter()checkedself.logger not in record.name. When a Powertools logger was initialized withservice="core", this substring test matched any third-party logger whose name contained"core"(such asbotocore). As a result,botocorerecords propagating to the runtime root handler were unintentionally dropped from CloudWatch logs.User experience
Logger(service="core")caused unrelated loggers containing"core"(likebotocorewarnings) to be swallowed and never reach the Lambda root handler.record.name == "core") or its children (record.name.startswith("core.")) are suppressed from the root handler. Unrelated loggers likebotocoreare preserved and emitted as expected.Verification
I used an AI assistant to prepare this fix and ran all validations locally:
developwherebotocorewarnings were suppressed (AssertionError: botocore was unexpectedly suppressed!).uv run pytest tests/functional/logger/required_dependencies/test_logger.py -k test_suppress_filter_does_not_suppress_unrelated_logger_containing_service_name -v->
1 passed, 89 deselected in 0.11suv run pytest tests/functional/logger/ tests/unit/logger/->
205 passed, 7 warnings in 0.72smake lint(ruff format + ruff check) ->1417 files left unchanged,All checks passed!make mypy->Success: no issues found in 963 source filesgit diff --check-> Clean, 0 errors.By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.