powertools-lambda-python: copy_config_to_registered_loggers results in duplicate logs when using with existing loggers

When using utils.copy_config_to_registered_loggers() to copy the PowerLogger config to existing loggers created by a library, this results in duplicate log entries in AWS Lambda

Expected Behavior

When copying config from the AWS Powertools logger to existing loggers instantiated by a library, I expect the log statements of that library to appear once in the log stream, formatted as JSON

Current Behavior

When copying config from the AWS Powertools logger to existing loggers, I see log statements from existing loggers appearing twice in the log stream: once properly formatted as JSON and once as plain text

image

Possible Solution

I don’t know why is this happening, so I can’t really comment on a solution 😅

Steps to Reproduce (for bugs)

  1. Have a library that instantiates loggers the “regular” Python way, like this:
logger = logging.getLogger(__name__)

class MyClass:
    def my_func():
        logger.info("Hello")
  1. Import this library into your Lambda function
  2. Instantiate a logger from AWS Lambda Powertools and copy the config to existing loggers:
from aws_lambda_powertools import Logger
from mylibrary import MyClass

logger = Logger() # POWERTOOLS_SERVICE_NAME=myservice
utils.copy_config_to_registered_loggers(source_logger=logger)
  1. Observe duplicated logs in the Lambda log stream

Environment

  • Powertools version used: 1.25.1
  • Packaging format (Layers, PyPi): Docker image
  • AWS Lambda function runtime: Python 3.9

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 21 (13 by maintainers)

Most upvoted comments

Can confirm it works! Thanks for the hard work!

If I understand #1001 correctly, that is about the source logger being excluded. And from what I understand, the source logger is the Lambda Powertools logger, from which the configs are copied, right?

The problem I’m having, is that the logs from another library are duplicated. They appear once properly formatted as JSON, and once as plain text (as per the original config)