Skip to content

Duplicate logs: structured and unstructured #115

Closed

Description

@Nxtra

My logs are appearing twice in CloudWatch: once in a structured format and once unstructured.
I'd only like to have the structured logs.
I didn't see this behavior before so it might be something on my side.

Expected Behavior

Having the logs once in structured format.

Current Behavior

Having the logs both structured and unstructured.

Environment

  • Powertools version used: 1.1.2
  • AWS Lambda function runtime: Python
  • Debugging logs

Powertools is setup like this:

from aws_lambda_powertools import Logger, Tracer, Metrics
from aws_lambda_powertools.metrics import MetricUnit

logger = Logger()
tracer = Tracer()
metrics = Metrics()

@metrics.log_metrics
@logger.inject_lambda_context
@tracer.capture_lambda_handler
def handle(event, context):
   ...
   logger.info('validated')

I am seeing the logs twice:
image

Seems like the root logger is still capturing and logging.

Why do I have duplicate logs? What am I missing here?

Activity

  1. added
    bug
    triage
    on Aug 18, 2020
  2. heitorlessa commented on Aug 18, 2020

    @heitorlessa
    Contributor
  3. heitorlessa commented on Aug 18, 2020

    @heitorlessa
    Contributor

    @Nxtra managed to replicate: It's what I suspected - Lambda configures the root logger with a basic unstructured info (it shouldn't), and since you're configuring a new Logger it emits the message twice due to how Logging works in Python (hierarchy: root logger -> your logger, the record traverses from your logger upwards to the root).

    Why wasn't this happening before

    On 1.1.0, we introduced Logging Inheritance feature which also made our Logger implementation fully compliant with how Python Logging works, however that also means changes you create in your Parent Logger, or Child Logger propagates between themselves.

    Solution

    We remove the root logger handler set by Lambda since you're using ours. An alternative solution would be to disable log propagation when we create a Logger for your app, however, this would remove the ability to propagate structured logging changes between the Parent and the Child - I chose the former.

    This fix will come within an hour on 1.1.3

  4. added a commit that references this issue on Aug 18, 2020
  5. heitorlessa commented on Aug 18, 2020

    @heitorlessa
    Contributor

    1.1.3 is now live in PyPi -- Could you please try that and let us know here @Nxtra?

    I've deployed a fresh function, run it locally w/ SAM CLI too, and managed to replicate the issue in tests but... just in case

  6. Nxtra commented on Aug 18, 2020

    @Nxtra
    Author

    I updated my project to 1.1.3.
    The issue has disappeared!
    Thanks for elaborating on what caused the issue.
    And wow, big thanks for the solution on such a short term!

  7. viniciusao commented on Nov 20, 2021

    @viniciusao

    just an info for everyone coming in here and it's using AWS Chalice.
    You must set to false the configure_logs parameter when instantiating Chalice() app's object.

    image

  8. removed
    triage
    on Feb 13, 2023
  9. warvariuc commented on Sep 3, 2023

    @warvariuc

    I had a similar issue. In my project the PowerTools logger was configured before the main logging conf. I ended up doing this:

    pt_logger = aws_lambda_powertools.Logger(service="...")
    # PowerTools logger sets up its own handler to stdout and adds a filter to prevent duplicates
    # in the root logger, but the logging is not configured at this point, so the filter is not attached
    # to any handler of the root logger, so it's easier just to disable the propagation
    pt_logger._logger.propagate = False  # noqa
  10. EricBizet commented on Nov 14, 2024

    @EricBizet

    I had a similar issue. In my project the PowerTools logger was configured before the main logging conf. I ended up doing this:

    pt_logger = aws_lambda_powertools.Logger(service="...")
    pt_logger._logger.propagate = False # noqa

    Worked like a charm for me as well. Helped me solve a duplicate log issue due to using multiprocessing

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions