-
Notifications
You must be signed in to change notification settings - Fork 146
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: pretty printing logs in local development #751
Comments
Hi @krivachy, thanks for opening an issue and proposing the feature. I can see where you are coming from and what is the use case you're suggesting. I haven't formed an opinion on the topic yet though; the comments that follow are aimed at having an open discussion to evaluate options. On one hand structured logging is one of the key features of this opinionated logger and optimising these libraries to run on AWS Lambda is a key part of our ethos. On the other hand, I think developer experience is also extremely important, and we have received a similar-ish request not so long ago; so we'll have to discuss this a bit more before making a decision. In the meanwhile, and just to evaluate other options, what about having a behaviour similar to the one that Tracer has, which consists in Powertools Logger being able to detect it's not running on Lambda. In the case of Tracer, when running locally or inside SAM, we simply don't do traces, in the case of Logger we would pretty-print the JSON objects instead of emitting the string only. I know this option doesn't cover colouring nor allows complete control over the output, but I'm inclined to think that giving such a big escape hatch (bring your own format) would kind of defeat the purpose of what we are trying to achieve, which, in the case of logger, is to foster the usage of best practices around structured logging and such. |
Thanks for your detailed response @dreamorosi. Having this lib a very opinionated logger makes sense. Building in pretty printing would be a good solution, even if it doesn't support all of the fancy features that other pretty printers. How would you detect if it's running in a lambda vs. not? |
The Tracer utility already does this (see here). It basically consists of some heuristics logic around environment variables. For example, the AWS Lambda execution environment always sets an |
Wouldn't it be simpler to ask the client to add a pipe at the end of his command when developing on his local environment ? I come from pino and you can just pretty print your commands with |
A similar PR was filed for the Python version of Powertools: aws-powertools/powertools-lambda-python#1548 |
We discussed it this internally among maintainers cross-languages and we came to the decision that we will implement a new environment variable that will enable among other things also printing pretty JSON in Logger. If env var Logger:
Tracer:
|
Thanks @saragerion, that would work nicely! Looking forward to having a better local dev experience. |
@krivachy would you be so kind to leave your feedback in this RFC? I'd love to hear your perspective and vision on the matter. |
Linking also this PR as reference: aws-powertools/powertools-lambda-python#1548 |
Hey, @dreamorosi, thanks for pointing out this issue. I want to work on this feature. I'll dive into the discussion and write a top-level plan here, then we will discuss it. |
@shdq great stuff, thanks for the bias for action. Please make sure to also read the RFC and PR that are linked in the last two comments. That RFC mentions a new environment variable that, when used, influences the behavior of the logger (as well as other utilities). For now let's keep the changes related to logger and pretty-printing which interest this issue. As per last time, if you have any question please let me know :) |
Hey @dreamorosi. I hope you're doing well. I've been working on this feature and have some questions to ask. According to RFC, the scope of this PR is basically to increase JSON.stringify indentation to 4 if the I implemented the feature and added tests. It works. I had to introduce this env variable because it wasn't used before. First of all, I have naming questions, I called the constant Second thing, I looked at the python implementation, they use Maybe I went too far and added this "dev mode" as an option to the logger options like And the last thing. Should I update the docs and add the same section like this? And also add Let me know. |
Hi @shdq, doing great and happy to hear from you. Will reply below to each paragraph/topic :)
Correct
I think the naming choices are sensible, but I'll be able to give a more definitive answer in the PR review. I trust that if we decide to change the name the impact won't be major. If that's not the case please let me know.
You're right in that we don't have an equivalent for these variables here. If I remember correctly their implementation the two variables are needed because they somewhat manually configure the indent based on the mode. In our case I think we could be fine with only one for
I do appreciate the bias for action here. I see where you're coming from and how by reading the The reason for not having it there is that we would like customers to use this new
Yes, ideally if there's a change in the API it should be represented in the docs.
Yes, definitely. I see that you've already put a lot of thought into the feature and based on the message above I think you're going in the right direction so I agree that the discussion would be better suited to continue while looking at the code. Thank you again for the care & consideration you've put in the messages & changes. |
The first doubt about this was when other tests failed to pass because of the new config option. Next, when I was describing it here. Now I'm sure. Thank you for the clarification. I will revert the changes tied to the config and open a PR tomorrow. |
|
Description of the feature request
Have the ability to pretty print log statements when running lambdas locally. This would make local development nicer.
Problem statement
The logger module always prints out JSON which is required for CloudWatch, but not a nice experience when running a lambda locally. It is hard to parse the JSON that is printed in a single line.
There is no way to override this via
Logger
options.Summary of the feature
To solve this, I would propose an overridable
logPrinter
function that would default to JSON printing. The function would only be invoked if the log level will be outputted. The function would also be invoked afterlogFormatter
has run.This would allow users of the library to conditionally override the printer when running a lambda locally.
Code examples
The interface could look like this:
Usage could look like this:
Benefits for you and the wider AWS community
This overridable config would allow users of the AWS community to develop various output formats, for example ones that colorize the console output.
Describe alternatives you've considered
Overriding the actually outputting to stdout, but most users of the library would just use the default
console.log
so overriding printing seemed like a better option.Additional context
pino
is a library that has pluggable prettification viapino-pretty
:Which then outputs logs that look like this:
Related issues, RFCs
The text was updated successfully, but these errors were encountered: