You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: logger_handler argument is ignored if logging.getLogger() already has a handler. #4277
The logger_handler argument for a Logger instance should set the provided logger as the registered handler, regardless of what handlers are already set on logging.getLogger() outputs.
Current Behaviour
When providing a custom logger via the logger_handler argument, the custom log handler is ignored when the logger returned by logging.getLogger(name) already has a handler configured. This is problematic since environments like Serverless Framework cause getLogger to return a logger with a StreamHandler preconfigured.
Code snippet
fromloggingimportHandlerfromaws_lambda_powertoolsimportLoggerclassCustomHandler(Handler):
""" A custom handler class. (Truncated for clarity, contents don't matter) """logger=Logger(logger_handler=CustomHandler())
print(logger.registered_handler) # This prints <StreamHandler <stdout> (NOTSET)>defhandler(event, context):
print("Lambda running")
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
Create a custom log handler
Create a Logger instance using the custom handler in the logging_handler parameter.
Deploy and run lambda using Serverless Framework with serverless-python-requirements in a lambda layer config.
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
changed the title [-]Bug: Custom handler is being ignored when deployed[/-][+]Bug: logger_handler argument is ignored if logging.getLogger already has a handler.[/+]on May 5, 2024
changed the title [-]Bug: logger_handler argument is ignored if logging.getLogger already has a handler.[/-][+]Bug: logger_handler argument is ignored if logging.getLogger() already has a handler.[/+]on May 5, 2024
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 registered_handler property is returning the first available handler -- locally this works fine [bottom], so we need to setup a repro environment with Serverless framework.
Question
Could you clarify what Serverless framework is doing, exactly? is that a plugin of sorts?
Could you share a minimal reproducible SLS config, if possible? We can try SAM quickly tomorrow, then test with any sample we can create with SLS framework
Are you using a custom handler to send data elsewhere and Logger is relying on a parent handler instead?
I haven't used Serverless framework in years so I appreciate your patience in what may seem basic to the non-initiated.
Hi @heitorlessa, thanks for you comment! To clarify things, I've recreated the issue without Serverless Framework.
Please take a look at this code snippet (Explanation below)
importosimportloggingfromloggingimportHandler, FileHandlerfromaws_lambda_powertoolsimportLoggerSERVICE_NAME="test_service"os.environ["POWERTOOLS_SERVICE_NAME"] =SERVICE_NAMEclassMyCustomHandler(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 python_logger.addHandler(FileHandler("./log.txt")) line.
Since Serverless Framework loggers always have a StreamHandler configured, custom handlers do not work (Unless that StreamHandler is explicitly removed, see my previous comment).
I hope that clears this up, let me know if something still isn't clear :)
Activity
boring-cyborg commentedon May 5, 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
nostromoJohn commentedon May 5, 2024
I have created a temporary workaround for this issue:
Logger()instance_loggerattribute (logger._logger.removeHandler(logger._logger.handlers[0]))logger.addHandler(CustomHandler())[-]Bug: Custom handler is being ignored when deployed[/-][+]Bug: logger_handler argument is ignored if logging.getLogger already has a handler.[/+][-]Bug: logger_handler argument is ignored if logging.getLogger already has a handler.[/-][+]Bug: logger_handler argument is ignored if logging.getLogger() already has a handler.[/+]heitorlessa commentedon May 6, 2024
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
registered_handlerproperty is returning the first available handler -- locally this works fine [bottom], so we need to setup a repro environment with Serverless framework.Question
Loggeris relying on a parent handler instead?I haven't used Serverless framework in years so I appreciate your patience in what may seem basic to the non-initiated.
Thanks!
nostromoJohn commentedon May 7, 2024
Hi @heitorlessa, thanks for you comment! To clarify things, I've recreated the issue without Serverless Framework.
Please take a look at this code snippet (Explanation below)
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
python_logger.addHandler(FileHandler("./log.txt"))line.Since Serverless Framework loggers always have a StreamHandler configured, custom handlers do not work (Unless that StreamHandler is explicitly removed, see my previous comment).
I hope that clears this up, let me know if something still isn't clear :)
29 remaining items
leandrodamascena commentedon Feb 10, 2025
I don't have a final solution for this yet! I'll try to focus on this in the next iteration starting next week.
leandrodamascena commentedon Feb 13, 2025
I have a PR to fix this issue and will merge it next week.
github-actions commentedon Feb 17, 2025
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.
github-actions commentedon Feb 25, 2025
This is now released under 3.7.0 version!