Skip to content

Bug: Logs populating into cloudwatch as multiple lines instead of a single object #3763

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
thault opened this issue Feb 13, 2024 · 10 comments
Closed
Assignees

Comments

@thault
Copy link

thault commented Feb 13, 2024

Expected Behaviour

I'm expecting the logs to populate as a single json object in cloudwatch.

expected

Current Behaviour

Logger logs are shown as single lines within cloudwatch creating a lot of lines within the log stream. I have tried the log format as text and JSON and get the issue both ways.

actual

Code snippet

from aws_lambda_powertools import Logger
from aws_lambda_powertools.utilities.typing import LambdaContext
logger = Logger(log_uncaught_exceptions=True)

@logger.inject_lambda_context(log_event=True)
def lambda_handler(event, context)
    logger.info("Token retrieval started")

Possible Solution

No response

Steps to Reproduce

Lambda using the powertools layer arn:aws:lambda:us-east-1:017000801446:layer:AWSLambdaPowertoolsPythonV2:60, followed the guide for basic logging with context included. All logs created via logger.info() or logger.debug() have the issue.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.12

Packaging format used

Lambda Layers

Debugging logs

No response

@thault thault added bug Something isn't working triage Pending triage from maintainers labels Feb 13, 2024
Copy link

boring-cyborg bot commented Feb 13, 2024

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

@heitorlessa heitorlessa self-assigned this Feb 14, 2024
@heitorlessa
Copy link
Contributor

trying to reproduce it now... from my recollection that only happens when you have \n since CloudWatch Logs creates a new line like that. Logger prints as a string. The only behavior change is when you have POWERTOOLS_DEV env var where we increase indentation .

@heitorlessa
Copy link
Contributor

heitorlessa commented Feb 14, 2024

Couldn't reproduce, will try more scenarios

  • Same code snippet on Python 3.11
  • Same code snippet older Powertools for AWS Lambda version (2.31.0)
  • Same code snippet on Python 3.12
  • Use POWERTOOLS_DEV env
  • Use POWERTOOLS_DEBUG env
  • Copy existing Logger config to other std/libraries (noticed in your screenshot X-Ray/boto appears)
image

@heitorlessa heitorlessa added not-a-bug and removed bug Something isn't working triage Pending triage from maintainers labels Feb 14, 2024
@heitorlessa
Copy link
Contributor

heitorlessa commented Feb 14, 2024

As expected, reproduced with POWERTOOLS_DEV where indentation is increased for local debugging. Checking the last scenario to triple check.

Local effect (intended use case)

image

Local effect with SAM CLI logs pulling from CloudWatch Logs

image

CloudWatch Logs effect (extra indentation is transformed

image

@heitorlessa
Copy link
Contributor

All scenarios tested, this only seems to occur due to POWERTOOLS_DEV as expected -- could you please check whether you have that turned on?


Logger config copied to boto3+X-Ray SDK loggers + DEBUG log level

image

@heitorlessa heitorlessa moved this from Triage to Pending customer in Powertools for AWS Lambda (Python) Feb 14, 2024
@thault
Copy link
Author

thault commented Feb 14, 2024

Looks like you are correct that it's from the POWERTOOLS_DEV; when I set that to false the logs started populating as single lines.

So this is expected behavior when POWERTOOLS_DEV is true; is there a way to work around that? It makes sense when running locally with SAM, but it makes the logs a pain when testing in AWS.

@heitorlessa
Copy link
Contributor

I'm sure we can figure something out -- what made you enable POWERTOOLS_DEV? That will help understand alternative solutions or trade-offs we might be able to make.

Customers also use this feature with pytest live debugging feature.

@heitorlessa
Copy link
Contributor

Summarizing our discussion on Discord.

What you were after was POWERTOOLS_DEBUG to get extra details about every operation (Logger) while using CloudWatch Logs view.

POWERTOOLS_DEV on the other hand is focused on local development, where you want to run your code locally with tools like SAM CLI, or pytest live log feature.

I'm closing this issue and created a PR to make this more explicit in our docs to prevent future confusion.

Thank you so much for taking the time to report this and help improve the docs!

@github-project-automation github-project-automation bot moved this from Pending customer to Coming soon in Powertools for AWS Lambda (Python) Feb 14, 2024
Copy link
Contributor

⚠️COMMENT VISIBILITY WARNING⚠️

This issue is now closed. Please be mindful that future comments are hard for our team to see.

If you need more assistance, please either tag a team member or open a new issue that references this one.

If you wish to keep having a conversation with other community members under this issue feel free to do so.

@github-actions github-actions bot added the pending-release Fix or implementation already in dev waiting to be released label Feb 15, 2024
Copy link
Contributor

This is now released under 2.34.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Feb 21, 2024
@heitorlessa heitorlessa moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Shipped
Development

No branches or pull requests

2 participants