Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log raw JSON to Cloudwatch from AWS Lambda in node.js?

I have some node.js based Lambdas that are logging data.

In order to properly query and filter the data etc, I want to log as pure JSON data from my Lambdas.

However, when I do a regular console.log it makes an ordinary string of the data.

console.log({a:1,b:2,x:"xxx"})

Results in this:

2020-04-29T14:46:45.722Z    3f64c499-fbae-4a84-996c-5e5f0cb5302c    INFO    { a: 1, b: 2, x: 'xxx' }

The logged line above does not seem to be searchable as JSON using the various filter matching options in CloudWatch.

I've tried to call the AWS.CloudWatchLogs API directly but since I'm using lambda I cannot maintain a token between invocations of the functions, so I'm not sure that's the way to go.

Have anyone else had success in logging raw JSON from a Javascript Lambda?

like image 628
Esben von Buchwald Avatar asked Apr 29 '20 15:04

Esben von Buchwald


1 Answers

The problem is that console.log() does not go directly to stdout/stderr. You can see that using this Lambda:

const process = require('process');

exports.handler = async (event) => {
    console.log("message 1");
    process.stdout.write("message 2\n");
};

If you invoke that, you will see output like this:

START RequestId: 6942bebc-1997-42cd-90c2-d76b44c637283 Version: $LATEST
2020-04-29T17:06:07.303Z    6935bebc-1d97-42cd-90c2-d76b4a637683    INFO    message 1
message 2
END RequestId: 6942bebc-1997-42cd-90c2-d76b44c637283

So to get the output you want you could either redefine console.log to go to stderr, or write to stdout/stderr directly.

Or you could use a logging framework that writes to stdout/stderr, which may give you more flexibility on how your messages are written. I don't do Node development, but I've heard that Winston is the standard logging framework.

like image 156
Parsifal Avatar answered Oct 31 '22 03:10

Parsifal