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
Expected Behaviour
The
logger_handlerargument 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_handlerargument, 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 causegetLoggerto return a logger with a StreamHandler preconfigured.Code snippet
Possible Solution
aws_lambda_powertools can override
getLoggerdefault handlers, with or without user-provided arguments, instead of assuming the returned logger's handler list is empty.Steps to Reproduce
logging_handlerparameter.Powertools for AWS Lambda (Python) version
latest
AWS Lambda function runtime
3.10
Packaging format used
Lambda Layers
Debugging logs
No response