Skip to content

Bug: Logger constructor ignores level argument after first call #5591

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

Closed
dennis-benchling opened this issue Nov 19, 2024 · 4 comments
Closed

Comments

@dennis-benchling
Copy link

Expected Behaviour

According to the docs, we should be able to set the log level explicitly through the level parameter to Logger.

Current Behaviour

The level parameter is properly set the first time you instantiate Logger, but subsequent instantiations with the same service name will not set the log level. (It will always be the original value from the first instance.)

Code snippet

from aws_lambda_powertools import Logger

first = Logger(level="INFO")
second = Logger(level="DEBUG")

print(f"first level = {first.log_level}") # first level = 20
print(f"second level = {second.log_level}") # second level = 20. (Should be 10)

Possible Solution

In Logger._init_logger, setLevel should be called before the early return.

Steps to Reproduce

Run the following code:

from aws_lambda_powertools import Logger

first = Logger(level="INFO")
second = Logger(level="DEBUG")

print(f"first level = {first.log_level}") # first level = 20
print(f"second level = {second.log_level}") # second level = 20. (Should be 10)

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.8

Packaging format used

PyPi

Debugging logs

@dennis-benchling dennis-benchling added bug Something isn't working triage Pending triage from maintainers labels Nov 19, 2024
Copy link

boring-cyborg bot commented Nov 19, 2024

Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link

@anafalcao
Copy link
Contributor

Hello @dennis-benchling !
Thank you so much for opening this issue. The Logger is designed to follow a singleton-like pattern for each unique service name. When you create multiple Logger instances with the same service name (and not defining a service name means it's service_undefined), they will reuse the same underlying logger instance, maintaining consistent log levels and configurations.
This behavior is intentional and documented, as it allows for seamless logging across different modules of your application without the need to pass logger instances explicitly.
For your use case, you could follow the example from setting_log_level_programmatically.py or assign a different service name for each Logger.
Let me know if it solves your question or if there's anything else we can help you with!

@anafalcao anafalcao added the need-customer-feedback Requires more customers feedback before making or revisiting a decision label Jan 13, 2025
@leandrodamascena
Copy link
Contributor

Hey @dennis-benchling! I'm closing this issue as "not a bug" and please reopen if you need.

@leandrodamascena leandrodamascena added not-a-bug and removed bug Something isn't working triage Pending triage from maintainers need-customer-feedback Requires more customers feedback before making or revisiting a decision labels Jan 20, 2025
@leandrodamascena leandrodamascena moved this from Next iteration to Shipped in Powertools for AWS Lambda (Python) Jan 20, 2025
@github-project-automation github-project-automation bot moved this from Shipped to Coming soon in Powertools for AWS Lambda (Python) Jan 20, 2025
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@leandrodamascena leandrodamascena moved this from Coming soon to Closed in Powertools for AWS Lambda (Python) Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

No branches or pull requests

3 participants