Skip to content
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

Docs: The output of the bring_your_own_formatter_from_scratch.py example does not match the documentation. #3378

Closed
1 task done
wchaws opened this issue Nov 21, 2023 · 5 comments
Assignees
Labels
documentation Improvements or additions to documentation logger pending-close-response-required

Comments

@wchaws
Copy link

wchaws commented Nov 21, 2023

What were you searching in the docs?

In https://docs.powertools.aws.dev/lambda/python/latest/core/logger/#bring-your-own-formatter doc, the output is

{
    "event": "Collecting payment",
    "timestamp": "2021-05-03 11:47:12,494",
    "my_default_key": "test",
    "cold_start": true,
    "function_name": "test",
    "function_memory_size": 128,
    "function_arn": "arn:aws:lambda:eu-west-1:12345678910:function:test",
    "function_request_id": "52fdfc07-2182-154f-163f-5f0f9a621d72"
}

But actually, in my lambda environment the output is

{"event": "Collecting payment", "timestamp": null, "my_default_key": "test", "level": null, "location": null, "message": null, "service": "payment", "sampling_rate": null, "cold_start": true, "function_name": "PowertoolsTest", "function_memory_size": "128", "function_arn": "arn:aws:lambda:us-west-2:*******:function:PowertoolsTest", "function_request_id": "67a57243-9819-4637-af53-8b2116bd5dbd"}

timestamp and log level are null.

My lambda layer version is arn:aws:lambda:us-west-2:017000801446:layer:AWSLambdaPowertoolsPythonV2:48

Is this related to an existing documentation section?

No response

How can we improve?

Fix the wrong document

Got a suggestion in mind?

No response

Acknowledgment

  • I understand the final update might be different from my proposed suggestion, or refused.
@wchaws wchaws added documentation Improvements or additions to documentation triage Pending triage from maintainers labels Nov 21, 2023
Copy link

boring-cyborg bot commented Nov 21, 2023

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
Copy link
Contributor

Thank you @wchaws for taking the time to report this - we'll look into it this afternoon.

@heitorlessa heitorlessa self-assigned this Nov 21, 2023
@heitorlessa
Copy link
Contributor

I couldn't replicate in Lambda using the same version, would you be able to share how does your custom formatter look like?

I've just used the same custom formatter code and invoke a Lambda function via API Gateway to be sure.


Code used and CloudWatch Logs

image

Code snippet

from aws_lambda_powertools import Logger
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.logging.formatter import LambdaPowertoolsFormatter
from aws_lambda_powertools.logging.types import LogRecord


class CustomFormatter(LambdaPowertoolsFormatter):
    def serialize(self, log: LogRecord) -> str:
        """Serialize final structured log dict to JSON str"""
        # in this example, log["message"] is a required field
        # but we want to remap to "event" and delete "message", hence mypy ignore checks
        log["event"] = log.pop("message")  # type: ignore[typeddict-unknown-key,misc]
        return self.json_serializer(log)


app = APIGatewayRestResolver()
logger = Logger(logger_formatter=CustomFormatter())


@app.get("/todos")
def get_todos():
    logger.info("Timestamp and level shouldn't be null")
    return {
        "issue": "https://github.com/aws-powertools/powertools-lambda-python/issues/3378"
    }


@logger.inject_lambda_context
def lambda_handler(event: dict, context):
    return app.resolve(event, context)

@heitorlessa heitorlessa moved this from Triage to Pending customer in Powertools for AWS Lambda (Python) Nov 21, 2023
@heitorlessa heitorlessa added pending-close-response-required and removed triage Pending triage from maintainers labels Nov 21, 2023
@heitorlessa
Copy link
Contributor

Closing; please feel free to reopen if I missed anything when trying to reproduce.

@github-project-automation github-project-automation bot moved this from Pending customer to Coming soon in Powertools for AWS Lambda (Python) Dec 6, 2023
@heitorlessa heitorlessa moved this from Coming soon to Shipped in Powertools for AWS Lambda (Python) Dec 6, 2023
Copy link
Contributor

github-actions bot commented Dec 6, 2023

⚠️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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation logger pending-close-response-required
Projects
Status: Shipped
Development

No branches or pull requests

2 participants