feat(logger): pretty printing logs in local and non-prod environment#1141
Conversation
…nting logs when var is set to truthy value
…et to truthy value
|
Hello @dreamorosi. I forgot about the docs, so I'm going to commit and push it tomorrow. And we can discuss current changes until then. |
dreamorosi
left a comment
There was a problem hiding this comment.
Hi @shdq thanks a lot for the code contribution, this is looking promising :)
I have left a comment around the placement/evaluation order of the isDevMode/INDENTATION values in packages/logger/src/Logger.ts. I would like to hear your opinion on the idea.
Another point that I would like to bring on the table is whether or not the changes currently applied under packages/logger/src/config/* should stay there or they should be placed under packages/commons/src/config.
On one hand, at least for now, the only utility to use this variable & this logic from the EnvironmentVariablesService would be Logger. So having the logic under this package makes sense.
On the other hand, the original RFC talks about the POWERTOOLS_DEV variable influencing the behavior of multiple utilities, some of which don't exist today in this project.
One could argue that in order to be future proof, the logic could be placed today in the EnvironmentVariablesService that is present in commons (link) and that is extended by all other utilities.
Having the logic already in the commons package would allow to easily integrate the feature to other utilities in the future, however this might be a case of premature optimization since we don't know if/when these features will land and until then users who use only the Metrics or Tracer utilities will be having extra unused code as dependency.
Would be keen to hear your opinion on the topic.
I would stick to the RFC since there is a decision made that When I wrote the plan in the issue I mentioned that I didn't find a "common" place. My fault that I didn't sync and see the recent change #1123 that you moved the config to |
…aluation of this property to setOptions()
|
Hi @shdq thank you for the changes, I'll wait for your confirmation before reviewing again. |
|
@dreamorosi hello! If we stick to |
|
Okay, let's keep it as it is now and if we add similar features to other utilities we'll move this logic in I've already done a first pass of review and I see that the tests are passing. Before marking it as approved I'd like to test it locally to see how it would look from an user perspective. I'll try to do that by Monday end of morning (CEST). In the meantime I'll also ask for a second review from other maintainers. Thank you! |
|
I have built the package from this branch & tested the following dummy lambda code: import { Logger, injectLambdaContext } from "@aws-lambda-powertools/logger";
import middy from "@middy/core";
const logger = new Logger({ serviceName: "someName" });
export const handler = middy(async () => {
logger.info("test", { details: "foo" });
try {
throw new Error("error");
} catch (err) {
logger.error("some error", err as Error);
}
}).use(injectLambdaContext(logger));Then run the file with Details{
"cold_start": true,
"function_memory_size": null,
"level": "INFO",
"message": "test",
"service": "someName",
"timestamp": "2022-11-07T11:03:23.627Z",
"details": "foo"
}
{
"cold_start": true,
"function_memory_size": null,
"level": "ERROR",
"message": "some error",
"service": "someName",
"timestamp": "2022-11-07T11:03:23.629Z",
"error": {
"name": "Error",
"location": "file:///Users/aamorosi/Codes/powertools-playground/node_modules/@middy/core/index.js:85",
"message": "error",
"stack": "Error: error\n at file:///Users/aamorosi/Codes/powertools-playground/lib/test.mjs:10:11\n at runRequest (file:///Users/aamorosi/Codes/powertools-playground/node_modules/@middy/core/index.js:85:17)"
}
} |
dreamorosi
left a comment
There was a problem hiding this comment.
Hi @shdq I'm ready to approve the PR, however I have left one minor comment in the docs section. Apologies for not flagging this earlier, I noticed how it reads only after rendering the docs locally.
|
Hey @dreamorosi! Thank you for the fast feedback loop and help with this PR. I enjoyed working on it. If you need help with other issues or maybe have a particular one in mind – let me know. |
We need to take a look at them, they're failing in |
|
We have a PR open #1146 that should fix the issue highlighted by the e2e tests. |
|
Hi @shdq, after discussing this with the other maintainers I think we should tweak the function that resolves the value of the value = value.lower()
if value in ("1", "y", "yes", "t", "true", "on"):
return True
if value in ("0", "n", "no", "f", "false", "off"):
return FalseBasically we should accept more values for Also, we should update the value in the docs to say Thanks for your understanding! |
dreamorosi
left a comment
There was a problem hiding this comment.
See comment above about default value of POWERTOOLS_DEV and value resolution.
|
@shdq Sorry about this last minute after you have addressed everything. I was the one who raised this discrepancy to Python team. It sounds wrong to me that I am happy to make an adjustment + add test cases myself if you don't have time for this. Just let me know. |
|
Hello, @ijemmy I'll make the changes! I'm actually looking at the python implementation of the function that resolves the value and in the situation, when the env var value is "somethingsilly" it returns Here we have tests for such values. @dreamorosi @ijemmy what do you think if I implement it as a switch statement with |
|
Another question: There is the same check for the And there was a function |
Possibly yes, since this new function is going to be implemented (at least for now) in
I opted to remove that function because at the time that was the only usage.
At the moment they're not really checking for truth values but exactly and only For now I would keep the change scoped to this PR. Later if you want, in a separate PR after this one, we could move up the logic in the Let's discuss this again once this is merged. |
…n-prod-env' of https://github.com/shdq/aws-lambda-powertools-typescript into 751-feature-logger-pretty-printing-logs-in-local-and-non-prod-env
|
@dreamorosi sorry for being dense, but I want to make sure that I got it right. I need to implement a function Am I right, that we treat any other than Speaking of implementation details, I have two options: Another option is to put it in an array of truthy values: I like it, it is short and concise, but it uses an array. What do you think? Thank you for your time and help! |
|
Oops. I synced my fork and branch and I think I pushed it wrong. |
Not at all, I appreciate the questions 😄
Yes, that's correct.
I have to admit that I'm on the fence about this, but ultimately yes, anything that it's not in that list of truth-y values can be considered as If we look at the Python's implementation they actually throw an error if the value is not in the list of falsy values, but I don't think we should do that at this stage and that we should instead default to
Personally I like the |
|
I hope I didn't mess up with the previous push. And I can't find the commit to the docs with the default Should we add tests for If any other changes are needed I'm happy to make them! |
Don't worry, it happens! As far as I can see the diff looks okay and anyway we'll squash before merging so the single commits don't matter as much.
Yes please, would be great to have them so we can detect regressions in the future. Maybe you could reduce the ones for For Just make sure each string (case-insensitive) is represented once to test we support it.
As far as I can see, and from my side, that would be all 🙂 |
| [ '0', false ] | ||
| ]; | ||
|
|
||
| test.each(valuesToTest)('It takes string "%s" and returns %s', (input, output) => { |
dreamorosi
left a comment
There was a problem hiding this comment.
Looks good to me, thank you for the attention to details 💯
|
E2E tests are passing: https://github.com/awslabs/aws-lambda-powertools-typescript/actions/runs/3437044442 |
Description of your changes
Changes increase JSON.stringify indentation to 4 if the POWERTOOLS_DEV env variable has a truthy value.
Feature described and discussed in #751
How to verify this change
Related issues, RFCs
RFC aws-powertools/powertools-lambda#86
Issue number: #751
PR status
Is this ready for review?: YES
Is it a breaking change?: NO
Checklist
Breaking change checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.