-
Notifications
You must be signed in to change notification settings - Fork 406
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
Conversation
Thanks a lot for your first contribution! Please check out our contributing guidelines and don't hesitate to ask whatever you need. |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
4 if os.getenv("AWS_SAM_LOCAL", "").lower() == "true" else None | |
PRETTY_INDENT if os.getenv("AWS_SAM_LOCAL", "").lower() == "true" else COMPACT_INDENT |
OK, I will change this later today. Is there a good place to put the constants in, pls? |
Yes! |
We're making progress in the RFC but this might be of interest if others agree this is a clearer intent: |
OK, I changed everything to 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. |
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. |
ok, yes, no problem @heitorlessa . If you need me to set something (permissions wise) on my fork, let me know. |
Looking at it now ;) Appreciate the patience very much |
There was a problem hiding this 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
Co-authored-by: Heitor Lessa <lessa@amazon.nl>
Co-authored-by: Heitor Lessa <lessa@amazon.nl>
Co-authored-by: Heitor Lessa <lessa@amazon.nl>
I've committed all your suggestions, @heitorlessa , with a small change - I counted |
Looking into now ;) Thank you so much for these changes! |
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 |
Awesome work, congrats on your first merged pull request and thank you for helping improve everyone's experience! |
thanks for all the support, @heitorlessa |
Issue number: #1527
Summary
Changes
json.dumps
will use indentation by four spaces when in AWS SAM Local. Detection happens usingAWS_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
will be in AWS SAM Local by default indented by four spaces:
Checklist
If your change doesn't seem to apply, please leave them unchecked.
Is this a breaking change?
RFC issue number:
Checklist:
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