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

Activity

added
bugSomething isn't working
triagePending triage from maintainers
on May 5, 2024
boring-cyborg

boring-cyborg commented on May 5, 2024

@boring-cyborg

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

nostromoJohn commented on May 5, 2024

@nostromoJohn
Author

I have created a temporary workaround for this issue:

  1. Create a Logger() instance
  2. Remove the preconfigured handler using the private _logger attribute (logger._logger.removeHandler(logger._logger.handlers[0]))
  3. Add the custom handler logger.addHandler(CustomHandler())
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
heitorlessa

heitorlessa commented on May 6, 2024

@heitorlessa
Contributor

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.

Thanks!


image
added and removed
triagePending triage from maintainers
on May 6, 2024
moved this from Triage to Pending customer in Powertools for AWS Lambda (Python)on May 6, 2024
self-assigned this
on May 6, 2024
nostromoJohn

nostromoJohn commented on May 7, 2024

@nostromoJohn
Author

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)

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 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

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't workinglogger

Type

Projects

Status

Shipped

Milestone

No milestone

Relationships

None yet

    Participants

    @heitorlessa@leandrodamascena@dreamorosi@nostromoJohn

    Issue actions

      Bug: logger_handler argument is ignored if logging.getLogger() already has a handler. · Issue #4277 · aws-powertools/powertools-lambda-python