-
Notifications
You must be signed in to change notification settings - Fork 421
Feature request: Simplified logger formatter when in AWS SAM Local #1527
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
Comments
Thanks for opening your first issue here! We'll come back to you as soon as we can. |
Hey @pmarko1711, thanks a lot for taking the time to help us improve! Would you have the output handy to make it easier to reason the next steps? I need to spin up a project to test. I can't remember if it's an one line JSON (cost optimization), or indented -- would it be more helpful to have indented JSON when running locally if that's not the case yet? My initial concern: transparently swapping for a no JSON output could lead to incorrect assumptions how the final output would be when deployed. Look forward to hearing from you |
ExampleDemonstrating how it could look like without any code change. Beforesam local invoke
Invoking app.lambda_handler (python3.9)
Skip pulling image and use local one: public.ecr.aws/sam/emulation-python3.9:rapid-1.56.1-x86_64.
Mounting /Users/lessa/DEV/pt-issue-1527/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container
START RequestId: ea90d758-5a78-4777-9c46-2017b472fc08 Version: $LATEST
{"level": "INFO", "location": "lambda_handler:39", "message": "This should appear indented and not compacted into a single line...", "timestamp": "2022-09-26 09:03:38,349+0000", "service": "issue-1527"}
END RequestId: ea90d758-5a78-4777-9c46-2017b472fc08
{"statusCode": 200, "body": "{\"message\": \"hello world\"}"}REPORT RequestId: ea90d758-5a78-4777-9c46-2017b472fc08 Init Duration: 1.39 ms Duration: 1141.96 msBilled Duration: 1142 ms Memory Size: 128 MB Max Memory Used: 128 MB After> sam local invoke
Invoking app.lambda_handler (python3.9)
Skip pulling image and use local one: public.ecr.aws/sam/emulation-python3.9:rapid-1.56.1-x86_64.
Mounting /Users/lessa/DEV/pt-issue-1527/.aws-sam/build/HelloWorldFunction as /var/task:ro,delegated inside runtime container
START RequestId: 9d9eeaaa-6a1f-4355-ac3a-dc20da33f1c6 Version: $LATEST
{
"level": "INFO",
"location": "lambda_handler:39",
"message": "This should appear indented and not compacted into a single line...",
"timestamp": "2022-09-26 09:02:05,262+0000",
"service": "issue-1527"
}
END RequestId: 9d9eeaaa-6a1f-4355-ac3a-dc20da33f1c6
REPORT RequestId: 9d9eeaaa-6a1f-4355-ac3a-dc20da33f1c6 Init Duration: 1.04 ms Duration: 1120.49 ms Billed Duration: 1121 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"statusCode": 200, "body": "{\"message\": \"hello world\"}"}% |
Thanks for coming back to be, @heitorlessa . Right now the output is in this format:
In here it looks quite OK, but imagine these messages to be quite longer and auto-wrapped into multiple rows. It's then not that easy to find and read the actual messages while debugging. When I submitted the ticket I thought of simply outputting (when in AWS SAM Local) something more condensed, where message would be easier to find (since it'd be normally the last item in each row), e.g.:
For now I've achieved this by initializing a logger with a custom formatter that conditionally overwrote the inherited Nevertheless, your idea of indenting JSON outputs could be sufficient here. Maybe making use of pprint then? Wouldn't however having multi-line messages cause more issues elsewhere? (it seems a bit non-standard) (*) import os
from aws_lambda_powertools.logging.formatter import LambdaPowertoolsFormatter
AWS_SAM_LOCAL = os.getenv("AWS_SAM_LOCAL", "false").lower() == "true"
class MyCustomFormatter(LambdaPowertoolsFormatter):
def serialize(self, log: dict) -> str:
"""Use serialize"""
if AWS_SAM_LOCAL:
# https://awslabs.github.io/aws-lambda-powertools-python/latest/core/logger/#standard-structured-keys
prefix = "|".join(
[
log[k]
for k in ["timestamp", "level", "service", "location"]
if k in log
]
)
suffix = f"\n{log['exception']}" if "exception" in log else ""
return "{}: {}{}".format(prefix, log.get("message", ""), suffix)
else:
return super().serialize(log)
logger = Logger(service="ABC", logger_formatter=MyCustomFormatter()) |
For JSON multi-line, stdlib "json" has an indent param which is what I used for this POC - it's a no brainer to enable it for local dev ;-) pprint however could skew results as you have nested objects (plus multiple conversions). My main worry about plain text is setting the expectations to customers who would see different results in CloudWatch Logs (or elsewhere) -- we have a distinct set of customers persona (dev, Data engineers, Ops, InfoSec) so we always take that into account. If JSON multi-line suffices for you, we could make this available in the next release! I could either guide you where to make changes (formatter.py) so we can credit you in the release notes, or if you don't have the bandwidth I can make it later this week. Once I get my laptop tomorrow, |
Thanks. Ok, and happy to do add it myself so that I get familiar with your contribute process (and can contribute with sthg else in the future) |
should it get activated automatically (looking at AWS_SAM_LOCAL) or via a parameter / or a new envvar? |
Awesome! Let’s use AWS_SAM_LOCAL - if it’s present use a different
indentation (4) otherwise None
…On Tue, 27 Sep 2022 at 18:17, Peter Marko ***@***.***> wrote:
should it get activated automatically (looking at AWS_SAM_LOCAL) or via a
parameter / or a new envvar?
—
Reply to this email directly, view it on GitHub
<#1527 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAZPQBCNHPNLFO577H3IQDTWAMMY5ANCNFSM6AAAAAAQRFESSI>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
This is now released under 1.30.0 version! |
Use case
I use
aws_lambda_powertools.Logger
which works very nicely when looking at logs in Cloudwatch.Nevertheless, when testing/debugging lambda functions using AWS SAM local invoke, I find the json representation difficult to read and cluttering the console too much.
I know I can swap the logger, or use a custom formatter. But would it not be meaningful to include a simple formatter (instead of the
LambdaPowertoolsFormatter
that prints out jsons) and use it automatically when not really in Lambda (but e.g. in AWS SAM local invoke) ?Related issue: #409
Note:
Solution/User Experience
Alternative solutions
Acknowledgment
The text was updated successfully, but these errors were encountered: