Skip to content

Feature request: logger should write to stdout directly #747

Closed
@olanb7

Description

@olanb7

Logger should write to stdout, and not use console

Problem statement

The powertools logger is currently outputting it's logs via console Logger.ts#L451. Lambda appears to use a patched version of console which prefixes the timestamp, requestId and log level. This means that a powertools logged event is output to stdout as below, which is clearly not JSON:

2022-04-08T10:58:44.811Z	b6511ee9-4873-404e-b60c-a23cb45d3ff2	INFO {"cold_start":false,"function_arn":"arn:aws:lambda:eu-west-2:12345:function:test","function_memory_size":1024,"function_name":"test","function_request_id":"b6511ee9-4873-404e-b60c-a23cb45d3ff2","level":"INFO","message":"test message","service":"test","timestamp":"2022-04-08T10:58:44.811Z"}

Cloudwatch handles this OK, but other tools like the newrelic and honeycomb extensions work under the assumption that JSON logs are being sent to stdout. Honeycomb even warns against the use of console.log in their documentation.

Following the honeycomb suggestion of using bunyan (which does write to stdout) I have validated (as has @dreamorosi on slack) that these prefixes can be avoided by bypassing console.log.

The information in these prefixes is also duplicated already in the JSON message itself, so there is no loss of querying (although some may prefer to keep these prefixes so that cloudwatch queries aren't impacted.

Summary of the feature

Update the logger to avoid the patched console.log by writing logs to stdout. This will ensure that valid JSON messages can be intercepted by lambda layers and easily forwarded on to other observability tools.

Benefits for you and the wider AWS community

This is a blocker right now to anyone using centralised logging tools like NewRelic or Honeycomb, for whom structured logs are critical to the maintenance and support.

Describe alternatives you've considered

The only alternative is to use a different logger like pino or bunyan.

Additional context

I have not been able to find the source for the lambda logger internals (presumably closed source) but have verified that it must be adding the prefixes by bypassing.

Related issues, RFCs

#646
#lambda-powertools slack discussion

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

Relationships

None yet

Development

No branches or pull requests

Issue actions