Ed Van Horne
Ed Van Horne

Reputation: 203

Amazon Cloudwatch JSON format not correct

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?

Upvotes: 4

Views: 4080

Answers (2)

Jay A. Little
Jay A. Little

Reputation: 3287

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.

Upvotes: 3

Bhanu Chhabra
Bhanu Chhabra

Reputation: 576

With the limited experience i have, all the new lines are converted into a separate log entry. IMHO it is behaving as line logger, instead of entry logger.

So what you can do is, remove the new lines from log entries, when sending to cloudwatch.

It should automatically show the log json in formatted structure.

Upvotes: 0

Related Questions