Skip to content

Feature request: pretty printing logs in local development #751

Closed
@krivachy

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

Metadata

Metadata

Assignees

Labels

completedThis item is complete and has been merged/shippedfeature-requestThis item refers to a feature request for an existing or new utilityloggerThis item relates to the Logger Utility

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions