-
Notifications
You must be signed in to change notification settings - Fork 403
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
Bug: logger_handler argument is ignored if logging.getLogger() already has a handler. #4277
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
I have created a temporary workaround for this issue:
|
hey @nostromoJohn, thanks a lot for opening up an issue with us -- I'm not sure I fully understood so I'll ask a few questions to help me get a better grasp. Logger does use the given Logger Handler instead of creating one. However what I'd guess it's happening is the Question
I haven't used Serverless framework in years so I appreciate your patience in what may seem basic to the non-initiated. Thanks! |
Hi @heitorlessa, thanks for you comment! To clarify things, I've recreated the issue without Serverless Framework. import os
import logging
from logging import Handler, FileHandler
from aws_lambda_powertools import Logger
SERVICE_NAME = "test_service"
os.environ["POWERTOOLS_SERVICE_NAME"] = SERVICE_NAME
class MyCustomHandler(Handler):
"""
A custom handler class. Truncated for clarity.
"""
python_logger = logging.getLogger(SERVICE_NAME)
# Running with this line results on the log output written to a file.
# Comment the next line to use MyCustomHandler.
python_logger.addHandler(FileHandler("./log.txt"))
logger = Logger(logger_handler=MyCustomHandler())
logger.critical("URGENT!") So what is happening here? AWS lambda powertools constructs MyCustomLogger, however it is not used as the handler for the Logger instance. This is due to the underlying python logger already having a configured handler. The code works as intended if we comment out the I hope that clears this up, let me know if something still isn't clear :) |
awesome, THANK YOU for taking this repro 🫶. tackling it after lunch :) |
Adding notes as I go through this. What works as expected
What doesn't work as expected
|
It's important to note that if you print out the |
still working on it - a few meetings in between - will probably finish it tomorrow only. undocumented and WIP fix: heitorlessa@56b6c2b |
Done, lemme know if you think I've missed anything: #4295 I was tempted to solve another suboptimal experience [not a bug] with orphaned child but it's easy to mess up billions of invocations if done wrong. |
@heitorlessa Awesome! Thank you so much for this |
great! Finished cleaning up the docs on a related area (child loggers), as it wasn't clear enough. Leandro is reviewing this week and we'll include it in the next release by Friday or early next week :) Thank you one more time for reporting this! |
|
This is now released under 2.38.0 version! |
reopening as @leandrodamascena had to revert yesterday release as this caused a regression for other customers that our test suite did not cover - we're gonna need time to talk to customers and stress test this more for backwards compatibility |
Expected Behaviour
The
logger_handler
argument for a Logger instance should set the provided logger as the registered handler, regardless of what handlers are already set onlogging.getLogger()
outputs.Current Behaviour
When providing a custom logger via the
logger_handler
argument, the custom log handler is ignored when the logger returned bylogging.getLogger(name)
already has a handler configured. This is problematic since environments like Serverless Framework causegetLogger
to return a logger with a StreamHandler preconfigured.Code snippet
Possible Solution
aws_lambda_powertools can override
getLogger
default handlers, with or without user-provided arguments, instead of assuming the returned logger's handler list is empty.Steps to Reproduce
logging_handler
parameter.Powertools for AWS Lambda (Python) version
latest
AWS Lambda function runtime
3.10
Packaging format used
Lambda Layers
Debugging logs
No response
The text was updated successfully, but these errors were encountered: