Description
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 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
from logging import Handler
from aws_lambda_powertools import Logger
class CustomHandler(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)>
def handler(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.
Powertools for AWS Lambda (Python) version
latest
AWS Lambda function runtime
3.10
Packaging format used
Lambda Layers
Debugging logs
No response
Metadata
Metadata
Assignees
Labels
Type
Projects
Status
Shipped
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_logger
attribute (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_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
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.
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 :)
50 remaining items