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

Feature request: Add typing for log parameter in LambdaPowertoolsFormatter #2401

Closed
2 tasks done
erikayao93 opened this issue Jun 7, 2023 · 4 comments · Fixed by #2419
Closed
2 tasks done

Feature request: Add typing for log parameter in LambdaPowertoolsFormatter #2401

erikayao93 opened this issue Jun 7, 2023 · 4 comments · Fixed by #2419
Assignees
Labels

Comments

@erikayao93
Copy link
Contributor

erikayao93 commented Jun 7, 2023

Use case

Change discussed in #2014
Improve the existing mechanism of the LambdaPowertoolsFormatter by typing the log parameter with TypedDict

Solution/User Experience

Create a new types.py for the logger that will include the new TypedLog class, similar to:

from typing_extensions import TypedDict


class TypedLog(TypedDict, total=False):
    level: str
    location: str
    timestamp: str
    service: str
    event: str

When overriding the serialize() function of the LambdaPowertoolsFormatter, customers can have type checking on our log fields using the new TypedLog class, similar to:

from aws_lambda_powertools import Logger
from aws_lambda_powertools.logging.formatter import LambdaPowertoolsFormatter
from aws_lambda_powertools.logging.types import TypedLog


class CustomFormatter(LambdaPowertoolsFormatter):
    def serialize(self, log: TypedLog) -> str:
        """Serialize final structured log dict to JSON str"""
        log["event"] = log.pop("message")  # rename message key to event
        return self.json_serializer(log)  # use configured json serializer


logger = Logger(service="payment", logger_formatter=CustomFormatter())
logger.info("hello")

Alternative solutions

No response

Acknowledgment

@erikayao93 erikayao93 added feature-request feature request triage Pending triage from maintainers labels Jun 7, 2023
@boring-cyborg
Copy link

boring-cyborg bot commented Jun 7, 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

@leandrodamascena leandrodamascena added logger and removed triage Pending triage from maintainers labels Jun 7, 2023
@leandrodamascena
Copy link
Contributor

Thank you for opening this issue and working to submit PR @erikayao93! 👏

@github-actions
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 Jun 14, 2023
@github-actions
Copy link
Contributor

This is now released under 2.17.0 version!

@github-actions github-actions bot removed the pending-release Fix or implementation already in dev waiting to be released label Jun 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants