Skip to content

Bug: logger_handler argument is ignored if logging.getLogger() already has a handler. #4277

@nostromoJohn

Description

@nostromoJohn

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

  1. Create a custom log handler
  2. Create a Logger instance using the custom handler in the logging_handler parameter.
  3. 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

Labels

bugSomething isn't workinglogger

Type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions