Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logger): correctly pick powertools or custom handler in custom environments #6083

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

leandrodamascena
Copy link
Contributor

Original PR: #4295

Issue number: #4277

Summary

This PR fixes a bug when a compute environment or customer preconfigures a standard logging logger with the same Powertools logger name.

Previously, we'd always pick the first handler - child or parent. However, it led to pick the incorrect one if a logger we're creating for the first time (Logger) was already pre-setup through other means (logging.getLogger).

import logging
import os

from aws_lambda_powertools import Logger

service_name = os.getenv("POWERTOOLS_SERVICE_NAME")

# A environment, third-party library, or customer intentionally creates a logger with the same name as Powertools
# and add a Handler

logging.getLogger(service_name).addHandler(logging.StreamHandler())

logger = Logger(service=service_name)  # Powertools will ask standard logging to give the previously setup logger, then configure another handler etc as expected 

Changes

Please provide a summary of what's being changed

  • Encapsulate handler selection in _get_handler
  • Reverse depend registered_handler on _get_handler
  • Logger with two handlers and log items use both of them
  • The above with a child logger
  • Add explicit test for orphaned child without handler that will fail with AttributeError
  • Create a backwards compatible exception to provide actionable error on orphaned child formatter (e.g., child init before parent and append_keys)
  • Update docs to clarify child logger and their caveats

User experience

Please share what the user experience looks like before and after this change

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

  • Migration process documented
  • Implement warnings (if it can live side by side)

Acknowledgment

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.

@leandrodamascena leandrodamascena requested a review from a team as a code owner February 12, 2025 00:05
@boring-cyborg boring-cyborg bot added documentation Improvements or additions to documentation logger tests labels Feb 12, 2025
@pull-request-size pull-request-size bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 12, 2025
@github-actions github-actions bot added bug Something isn't working and removed documentation Improvements or additions to documentation labels Feb 12, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 12, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 12, 2025
@leandrodamascena leandrodamascena self-assigned this Feb 12, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 12, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 12, 2025
Copy link

codecov bot commented Feb 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.26%. Comparing base (2b4729a) to head (a023969).

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #6083   +/-   ##
========================================
  Coverage    96.25%   96.26%           
========================================
  Files          234      234           
  Lines        11140    11153   +13     
  Branches       822      826    +4     
========================================
+ Hits         10723    10736   +13     
  Misses         327      327           
  Partials        90       90           

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

@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 12, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 12, 2025
@boring-cyborg boring-cyborg bot added the documentation Improvements or additions to documentation label Feb 12, 2025
@github-actions github-actions bot removed the documentation Improvements or additions to documentation label Feb 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working do-not-merge logger size/L Denotes a PR that changes 100-499 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: logger_handler argument is ignored if logging.getLogger() already has a handler.
1 participant