Skip to content

Feature request: pretty printing logs in local development #751

Description

@krivachy

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

Activity

  1. added
    triageThis item has not been triaged by a maintainer, please wait
    on Apr 11, 2022
  2. dreamorosi commented on Apr 11, 2022

    @dreamorosi
    Contributor

    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.

  3. added
    loggerThis item relates to the Logger Utility
    RFCTechnical design documents related to a feature request
    and removed
    RFCTechnical design documents related to a feature request
    on Apr 11, 2022
  4. krivachy commented on Apr 12, 2022

    @krivachy
    Author

    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?

  5. dreamorosi commented on Apr 12, 2022

    @dreamorosi
    Contributor

    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 AWS_EXECUTION_ENV variable (source), while the sam CLI sets a AWS_SAM_LOCAL one when testing locally (source). If any of these variables is present in the environment then we assume that function is running locally.

  6. aleguern commented on Sep 23, 2022

    @aleguern

    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.

  7. dreamorosi commented on Sep 28, 2022

    @dreamorosi
    Contributor

    A similar PR was filed for the Python version of Powertools: aws-powertools/powertools-lambda-python#1548

  8. saragerion commented on Sep 28, 2022

    @saragerion
    Contributor

    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_DEV is present and set to 'true', the following happens:

    Logger:

    • pretty print JSON enabled

    Tracer:

    • traces are disabled
  9. krivachy commented on Sep 29, 2022

    @krivachy
    Author

    Thanks @saragerion, that would work nicely! Looking forward to having a better local dev experience.

  10. saragerion commented on Sep 29, 2022

    @saragerion
    Contributor

    @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.

  11. dreamorosi commented on Oct 19, 2022

    @dreamorosi
    Contributor

    Linking also this PR as reference: aws-powertools/powertools-lambda-python#1548

  12. shdq commented on Oct 29, 2022

    @shdq
    Contributor

    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.

  13. 7 remaining items

  14. github-actions commented on Nov 10, 2022

    @github-actions
    Contributor

    ⚠️ COMMENT VISIBILITY WARNING ⚠️

    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.

  15. added
    completedThis item is complete and has been merged/shipped
    feature-requestThis item refers to a feature request for an existing or new utility
    and removed
    completedThis item is complete and has been merged/shipped
    on Nov 14, 2022
  16. changed the title [-]Feature (logger): pretty printing logs in local development[/-] [+]Feature request: pretty printing logs in local development[/+] on Nov 14, 2022
  17. added
    completedThis item is complete and has been merged/shipped
    and removed
    pending-releaseThis item has been merged and will be released soon
    on Jan 13, 2023
  18. moved this from Coming soon to Shipped in AWS Lambda Powertools for TypeScripton Jan 13, 2023
  19. removed
    feature-requestThis item refers to a feature request for an existing or new utility
    completedThis item is complete and has been merged/shipped
    on Sep 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

loggerThis item relates to the Logger Utility

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions