Skip to content

fix(logger): match suppressed loggers by hierarchy - #8835

Merged
leandrodamascena merged 3 commits into
aws-powertools:developfrom
roonakyadav:fix/8821-suppress-filter-logger-hierarchy
Nov 20, 2026
Merged

leandrodamascena merged 3 commits into
aws-powertools:developfrom
roonakyadav:fix/8821-suppress-filter-logger-hierarchy

Conversation

@roonakyadav

Copy link
Copy Markdown
Contributor

Closes #8821

Summary

Changes

  • In aws_lambda_powertools/logging/filters.py, update SuppressFilter.filter() to match the exact logger name or child loggers prefixed with {self.logger}., rather than checking substring membership (self.logger not in logger).
  • In tests/functional/logger/required_dependencies/test_logger.py, add functional test test_suppress_filter_does_not_suppress_unrelated_logger_containing_service_name validating that unrelated loggers containing the service name (e.g. botocore when service is core) are not suppressed, while genuine child loggers remain suppressed from root handlers.

Root cause

SuppressFilter.filter() checked self.logger not in record.name. When a Powertools logger was initialized with service="core", this substring test matched any third-party logger whose name contained "core" (such as botocore). As a result, botocore records propagating to the runtime root handler were unintentionally dropped from CloudWatch logs.

User experience

  • Before: Logger(service="core") caused unrelated loggers containing "core" (like botocore warnings) to be swallowed and never reach the Lambda root handler.
  • After: Only records from the Powertools logger itself (record.name == "core") or its children (record.name.startswith("core.")) are suppressed from the root handler. Unrelated loggers like botocore are preserved and emitted as expected.

Verification

I used an AI assistant to prepare this fix and ran all validations locally:

  1. Reproduction & regression verification:
    • Confirmed failure on develop where botocore warnings were suppressed (AssertionError: botocore was unexpectedly suppressed!).
    • Ran newly added regression test:
      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.11s
  2. Full logger test suite:
    • uv run pytest tests/functional/logger/ tests/unit/logger/
      -> 205 passed, 7 warnings in 0.72s
  3. Linting and formatting:
    • make lint (ruff format + ruff check) -> 1417 files left unchanged, All checks passed!
  4. Static type checking:
    • make mypy -> Success: no issues found in 963 source files
  5. Diff hygiene:
    • git 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.

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
@roonakyadav
roonakyadav requested a review from a team as a code owner November 15, 2026 16:09
@roonakyadav
roonakyadav requested a review from hjgraca November 15, 2026 16:09
@boring-cyborg

boring-cyborg Bot commented Nov 15, 2026

Copy link
Copy Markdown

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@powertools-for-aws-oss-automation powertools-for-aws-oss-automation Bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Nov 15, 2026
@leandrodamascena leandrodamascena added this to the 3.42.0 milestone Nov 15, 2026

@Manishram-ai Manishram-ai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +636 to +647
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")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the service_name fixture keeps this test independent of logger state left by other tests or a rerun:

Suggested change
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}")

@leandrodamascena leandrodamascena left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @roonakyadav! Matching on the exact name or the name. prefix is how logging itself defines the hierarchy, so this is the right boundary, and the test shows botocore surviving next to a real child being suppressed. Goes out in 3.42.0, no need to rebase, you'll be in the release notes.

@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.20%. Comparing base (840eeab) to head (0014a73).
⚠️ Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #8835   +/-   ##
========================================
  Coverage    97.20%   97.20%           
========================================
  Files          327      327           
  Lines        16761    16761           
  Branches      1523     1523           
========================================
  Hits         16292    16292           
  Misses         328      328           
  Partials       141      141           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@leandrodamascena
leandrodamascena merged commit ba73859 into aws-powertools:develop Nov 20, 2026
14 checks passed
@boring-cyborg

boring-cyborg Bot commented Nov 20, 2026

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

@powertools-for-aws-oss-automation

Copy link
Copy Markdown

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

logger size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: SuppressFilter hides unrelated loggers whose name contains the service name

3 participants