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

feat(logger): pretty-print JSON when POWERTOOLS_DEV is set #1548

Merged
merged 6 commits into from
Oct 3, 2022

Conversation

pmarko1711
Copy link
Contributor

@pmarko1711 pmarko1711 commented Sep 27, 2022

Issue number: #1527

Summary

Changes

  • Logger's default serialize function json.dumps will use indentation by four spaces when in AWS SAM Local. Detection happens using AWS_SAM_LOCAL env var (if =="true").

User experience

There should be no impact unless in AWS SAM Local.

In AWS SAM Local, logs are output to console. To make the serialized json messages more readable and consequently debugging with AWS SAM Local easier, logger's default formatter will in this case automatically use indentation by four spaces. That means that messages normally serialized as

{"level": "INFO", "location": "lambda_handler:39", "message": "Some log message...", "timestamp": "2022-09-26 09:03:38,349+0000", "service": "issue-1527"}

will be in AWS SAM Local by default indented by four spaces:

{
    "level": "INFO",
    "location": "lambda_handler:39",
    "message": "Some log message...",
    "timestamp": "2022-09-26 09:02:05,262+0000",
    "service": "issue-1527"
}
This is to make debugging easier

Checklist

If your change doesn't seem to apply, please leave them unchecked.

Is this a breaking change?

RFC issue number:

Checklist:

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

Acknowledgment

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

Disclaimer: We value your time and bandwidth. As such, any pull requests created on non-triaged issues might not be successful.


View rendered docs/core/logger.md

@pmarko1711 pmarko1711 requested a review from a team as a code owner September 27, 2022 23:09
@pmarko1711 pmarko1711 requested review from heitorlessa and removed request for a team September 27, 2022 23:09
@boring-cyborg boring-cyborg bot added area/logger documentation Improvements or additions to documentation tests labels Sep 27, 2022
@pull-request-size pull-request-size bot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Sep 27, 2022
@boring-cyborg
Copy link

boring-cyborg bot commented Sep 27, 2022

Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need.
In the meantime, check out the #python channel on our AWS Lambda Powertools Discord: Invite link

Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for kicking the tires here. Made one suggestion, and I have one important note: let's use POWERTOOLS_DEV instead of AWS_SAM_LOCAL, I'll create a RFC today.

During maintainers sync, the TypeScript team also had customers for the same request except that they want to use node <app.ts>, or Amplify-CLI, which makes this scenario muddy.

We thought of a new environment variable POWERTOOLS_DEV to detect intent on overriding decisions like this and others in the future (e.g., Event Handler has a debug mode which we can deprecate in favour of this in v2). This helps provide a consistent experience across Powertools languages, document behaviours this would enable upfront in our docs, and avoid a proliferation of multiple env vars.

This also futureproof a potential move to AWS Powertools where we could support[1] both Lambda, Fargate, etc.

If your're busy, I'm happy to contribute these changes directly to your fork.

[1] Despite our tenets and name, we've been told by customers who use Powertools in Glue jobs, Fargate, and even IoT; classic Hyrum's law.

self.json_deserializer = json_deserializer or json.loads
self.json_default = json_default or str
self.json_serializer = json_serializer or partial(json.dumps, default=self.json_default, separators=(",", ":"))
self.json_indent = (
4 if os.getenv("AWS_SAM_LOCAL", "").lower() == "true" else None
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: Use a constant to ease maintaining this later, e.g.

Suggested change
4 if os.getenv("AWS_SAM_LOCAL", "").lower() == "true" else None
PRETTY_INDENT if os.getenv("AWS_SAM_LOCAL", "").lower() == "true" else COMPACT_INDENT

@heitorlessa heitorlessa added the triage Pending triage from maintainers label Sep 28, 2022
@pmarko1711
Copy link
Contributor Author

OK, I will change this later today. Is there a good place to put the constants in, pls?

@heitorlessa
Copy link
Contributor

OK, I will change this later today. Is there a good place to put the constants in, pls?

Yes! shared/constants.py is where we keep them.

@heitorlessa
Copy link
Contributor

We're making progress in the RFC but this might be of interest if others agree this is a clearer intent:

aws-powertools/powertools-lambda#86 (comment)

@pmarko1711 pmarko1711 changed the title feat(logger): Conditional JSON indentation for AWS SAM Local feat(logger): Conditional JSON indentation Sep 28, 2022
@pmarko1711
Copy link
Contributor Author

OK, I changed everything to POWERTOOLS_DEV and also renamed the test functions and this PR, @heitorlessa .

Since this env var will have multiple effects, shouldn't it be explained in a more prominent place in the documentation? For now I only put an info note in the page about logger.

Maybe it's not for this PR though.

@heitorlessa
Copy link
Contributor

thanks a lot @pmarko1711 and yes, I'll create a separate PR documenting the effects.

I'll look into the PR as soon as I complete another task today, hopefully by EOD the latest. With your permission, I might push changes directly to your fork to speed up merging this change (in case I have delays with my primary task) - I'll share feedback nevertheless.

@pmarko1711
Copy link
Contributor Author

pmarko1711 commented Sep 30, 2022

ok, yes, no problem @heitorlessa . If you need me to set something (permissions wise) on my fork, let me know.

@heitorlessa
Copy link
Contributor

Looking at it now ;) Appreciate the patience very much

@heitorlessa heitorlessa removed the triage Pending triage from maintainers label Sep 30, 2022
Copy link
Contributor

@heitorlessa heitorlessa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!! I love the wording - straight to the point.

Made a few suggestions to improve test accuracy, and use our strtobool to support cases where customers may use boolean strings like 0 or 1

aws_lambda_powertools/logging/formatter.py Outdated Show resolved Hide resolved
aws_lambda_powertools/shared/constants.py Outdated Show resolved Hide resolved
docs/core/logger.md Show resolved Hide resolved
tests/functional/test_logger_powertools_formatter.py Outdated Show resolved Hide resolved
tests/functional/test_logger_powertools_formatter.py Outdated Show resolved Hide resolved
tests/functional/test_logger_powertools_formatter.py Outdated Show resolved Hide resolved
@heitorlessa heitorlessa changed the title feat(logger): Conditional JSON indentation feat(logger): pretty-print JSON output when POWERTOOLS_DEV is set Sep 30, 2022
pmarko1711 and others added 2 commits September 30, 2022 19:45
Co-authored-by: Heitor Lessa <lessa@amazon.nl>
Co-authored-by: Heitor Lessa <lessa@amazon.nl>
@pmarko1711
Copy link
Contributor Author

pmarko1711 commented Sep 30, 2022

I've committed all your suggestions, @heitorlessa , with a small change - I counted os.linesep instead of \n in the functional tests

@heitorlessa
Copy link
Contributor

Looking into now ;) Thank you so much for these changes!

@heitorlessa
Copy link
Contributor

Looks great @pmarko1711 - thank you so much for working throughout the review process. Merging now.

Next steps: I'll (1) update the RFC with the latest info on POWERTOOLS_DEV and POWERTOOLS_DEBUG (separate feat altogether), (2) create a PR to update the documentation on these flags, and (3) release this feature by the end of the week.

@heitorlessa heitorlessa changed the title feat(logger): pretty-print JSON output when POWERTOOLS_DEV is set feat(logger): pretty-print JSON when POWERTOOLS_DEV is set Oct 3, 2022
@heitorlessa heitorlessa merged commit a0ef408 into aws-powertools:develop Oct 3, 2022
@boring-cyborg
Copy link

boring-cyborg bot commented Oct 3, 2022

Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience!

@pmarko1711
Copy link
Contributor Author

thanks for all the support, @heitorlessa

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 feature New feature or functionality size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants