Skip to content

refactor: simplify custom formatter for minor changes #417

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

Conversation

heitorlessa
Copy link
Contributor

Issue #, if available:

Description of changes:

For minor changes such as remapping keys, removing certain keys, etc., you will no longer have to implement a whole custom formatter from BasePowertoolsFormatter.

This PR introduces a public method serialize that is called after log record processing takes place, so customers don't have to reimplement extra keys, UTC support, X-Ray (if used), etc:

UX

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

from typing import Dict

class CustomFormatter(LambdaPowertoolsFormatter):
    def serialize(self, log: Dict) -> str:
        log["event"] = log.pop("message")
        return self.json_serializer(log)

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

This PR also documents the use of LambdaPowertoolsFormatter as a standalone formatter:

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

formatter = LambdaPowertoolsFormatter(utc=True, log_record_order=["message"])
logger = Logger(service="example", logger_formatter=formatter)

Checklist

Breaking change checklist

RFC issue #:

  • Migration process documented
  • Implement warnings (if it can live side by side)

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@heitorlessa heitorlessa added this to the 1.15.0 milestone May 4, 2021
@codecov-commenter
Copy link

codecov-commenter commented May 4, 2021

Codecov Report

Merging #417 (bd18cd3) into develop (daa35aa) will increase coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #417      +/-   ##
===========================================
+ Coverage    99.84%   99.89%   +0.05%     
===========================================
  Files          101      101              
  Lines         3975     3978       +3     
  Branches       198      198              
===========================================
+ Hits          3969     3974       +5     
+ Misses           2        1       -1     
+ Partials         4        3       -1     
Impacted Files Coverage Δ
aws_lambda_powertools/tracing/tracer.py 100.00% <ø> (ø)
.../utilities/data_classes/api_gateway_proxy_event.py 100.00% <ø> (ø)
...s/utilities/data_classes/appsync_resolver_event.py 100.00% <ø> (ø)
...tools/utilities/data_classes/event_bridge_event.py 100.00% <ø> (ø)
...wertools/utilities/data_classes/s3_object_event.py 100.00% <ø> (ø)
...bda_powertools/utilities/data_classes/sns_event.py 100.00% <ø> (ø)
...bda_powertools/utilities/data_classes/sqs_event.py 100.00% <ø> (ø)
...wertools/utilities/idempotency/persistence/base.py 99.35% <ø> (ø)
aws_lambda_powertools/logging/formatter.py 100.00% <100.00%> (ø)
aws_lambda_powertools/logging/logger.py 100.00% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update daa35aa...bd18cd3. Read the comment docs.

@heitorlessa heitorlessa merged commit 213caed into aws-powertools:develop May 4, 2021
@heitorlessa heitorlessa deleted the feat/logger-simplify-custom-formatter branch May 4, 2021 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants