Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon Cloudwatch JSON format not correct

Tags:

aws-lambda

I am logging Amazon skill requests and replies in the Lambda function of my Alexa skill. These are JSON objects that I am logging as follows:

logger.debug('Incoming request:\n' + JSON.stringify(event, null, 2));

logger.debug('Final response:\n' + JSON.stringify(alexaResponse, null, 2) + '\n\n');

When viewing the logs in Cloudwatch with Expand all = Row I see this:

Cloudwatch Log with Expand all set to Row

When I set Expand all to Text, the result is slightly better, but the leading spaces are trimmed, causing the indentation structure of the JSON document to be lost.

Cloudwatch Log with Expand all set to Text

I got this code from a tutorial. It is possible that changes in Amazon Cloudwatch have made code that worked in the tutorial fail now.

In the tutorial, the output looks like this:

Tutorial screen shot

That's the log output I want. How do I get it?

like image 410
Ed Van Horne Avatar asked Oct 16 '25 19:10

Ed Van Horne


1 Answers

My cloudwatch logs automatically display JSON strings as you want them without using the optional 2nd and 3rd arguments of JSON.stringify()

So try removing the null and 2 from your log statement's JSON.stringify.

logger.debug('Incoming request: \n' + JSON.stringify(event));

logger.debug('Final response: \n '+ JSON.stringify(alexaResponse));

I'm not at my computer to test, but I think that new line (\n) should give you the desired effect in cloudwatch, placing the json in its own line that you can expand. And its the extra whitespace that is making cloudwatch set new rows in the JSON.

like image 118
Jay A. Little Avatar answered Oct 19 '25 00:10

Jay A. Little



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!