Repository navigation
Description
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 after logFormatter 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:
interface LogPrintterInterface {
/**
* it serializes the log attributes to a string to be printed out.
*
* @param {LogAttributesWithMessage} attributes
* @returns {string}
*/
printAttributes(attributes: LogAttributesWithMessage): string;
}Usage could look like this:
const localPrinter = {
printAttributes: (attributes: LogAttributesWithMessage) => `${attributes.logLevel} ${attributes.message}`
};
const log = new Logger({
logPrinter: process.env.IS_LOCAL ? localPrinter : undefined
});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 via pino-pretty:
const pino = require('pino')
const pretty = require('pino-pretty')
const logger = pino(pretty())
logger.info('hi')Which then outputs logs that look like this:
Related issues, RFCs
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsShipped

Activity
dreamorosi commentedon Apr 11, 2022
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.
krivachy commentedon Apr 12, 2022
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?
dreamorosi commentedon Apr 12, 2022
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
AWS_EXECUTION_ENVvariable (source), while the sam CLI sets aAWS_SAM_LOCALone when testing locally (source). If any of these variables is present in the environment then we assume that function is running locally.aleguern commentedon Sep 23, 2022
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
| pino-pretty.dreamorosi commentedon Sep 28, 2022
A similar PR was filed for the Python version of Powertools: aws-powertools/powertools-lambda-python#1548
saragerion commentedon Sep 28, 2022
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.
This way developers can decide to enable this functionality locally or in their dev environments if they wish.
If env var
POWERTOOLS_DEVis present and set to 'true', the following happens:Logger:
Tracer:
krivachy commentedon Sep 29, 2022
Thanks @saragerion, that would work nicely! Looking forward to having a better local dev experience.
saragerion commentedon Sep 29, 2022
@krivachy would you be so kind to leave your feedback in this RFC?
aws-powertools/powertools-lambda#86
I'd love to hear your perspective and vision on the matter.
dreamorosi commentedon Oct 19, 2022
Linking also this PR as reference: aws-powertools/powertools-lambda-python#1548
shdq commentedon Oct 29, 2022
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.
7 remaining items
github-actions commentedon Nov 10, 2022
Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.
[-]Feature (logger): pretty printing logs in local development[/-][+]Feature request: pretty printing logs in local development[/+]