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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With