Why console.log() Is Your Friend
Jeff Blankenburg Sep 15, 2017
Share:
Test Tips & Tools Tutorial
Blog_Header_Post_Img

I frequently encounter Alexa developers that are struggling to figure out what is happening in their skill. They ask questions like:

  • How can I know which intent was called by my user?
  • How can I see the slot values that are being passed into my skill?

I've been surprised that many of them have not discovered the power of console.log();.

Logging the Good Stuff

One of my first steps in constructing a new Lambda function for a skill is this line of code in each of my event handlers:

Copied to clipboard
console.log("THIS.EVENT = " + JSON.stringify(this.event));

By logging "this" to my console in each handler, I can see the entire request that was sent to my skill when something goes wrong. Here's an example of a logged request from my Alexa skill for developers, Slot Genie, which is a reference guide for all of the built-in slots that Amazon provides:

Copied to clipboard
{
    "version": "1.0",
    "session": {
        "new": false,
        "sessionId": "amzn1.echo-api.session.01f4cfaa-9d19-4639-bcba-f82227685576",
        "application": {
            "applicationId": "amzn1.ask.skill.adc621ec-b41c-40ba-bc05-489b10ba392f"
        },
        "user": {
            "userId": "amzn1.ask.account.AGO3KWZHJP75CQC6VIJJGKN5Y7RAUIZFGSGCEL46MGV535AWCNUFEK6L4NKXJ5EHMMG2LRXPGRCLLOZS4LWHGTNSG57TSSKESHWERVZQFFOYATFQCMAKETZSMEUFTVLZNK6H57QVJ3TLQ7LSXUTQWMZFD65KMSGJHCM3JGBS67H3EJVGC3LKZVIJBH4KRHTBW2YUAEI3AAKY5RQ"
        },
        "attributes": {}
    },
    "context": {
        "AudioPlayer": {
            "playerActivity": "STOPPED"
        },
        "Display": {
            "token": ""
        },
        "System": {
            "application": {
                "applicationId": "amzn1.ask.skill.adc621ec-b41c-40ba-bc05-489b10ba392f"
            },
            "user": {
                "userId": "amzn1.ask.account.AGO3KWZHJP75CQC6VIJJGKN5Y7RAUIZFGSGCEL46MGV535AWCNUFEK6L4NKXJ5EHMMG2LRXPGRCLLOZS4LWHGTNSG57TSSKESHWERVZQFFOYATFQCMAKETZSMEUFTVLZNK6H57QVJ3TLQ7LSXUTQWMZFD65KMSGJHCM3JGBS67H3EJVGC3LKZVIJBH4KRHTBW2YUAEI3AAKY5RQ"
            },
            "device": {
                "deviceId": "amzn1.ask.device.AFC32IAN3UCH73UFGTPPO2WDR7J56MNHC3XCXF6QM4YFHPO7ZTCTT2PWDU4YP5C65MJ442C5TCTOOTL2QKEEBVRZD3KE5AQWVJJVXAXPUROMANDU35C6ZD7OR7ICNYIKTA34L4R6AIEBQBU4SRZAN6IFLNSA",
                "supportedInterfaces": {
                    "AudioPlayer": {},
                    "Display": {
                        "templateVersion": "1.0",
                        "markupVersion": "1.0"
                    },
                    "VideoApp": {}
                }
            },
            "apiEndpoint": "https://api.amazonalexa.com"
        }
    },
    "request": {
        "type": "IntentRequest",
        "requestId": "amzn1.echo-api.request.36001419-fdf6-4213-8a8f-4ba08cf4758a",
        "timestamp": "2017-08-22T01:10:28Z",
        "locale": "en-US",
        "intent": {
            "name": "SlotCheckIntent",
            "confirmationStatus": "NONE",
            "slots": {
                "slottype": {
                    "name": "slottype",
                    "value": "music",
                    "resolutions": {
                        "resolutionsPerAuthority": [
                            {
                                "authority": "amzn1.er-authority.echo-sdk.amzn1.ask.skill.adc621ec-b41c-40ba-bc05-489b10ba392f.slotvalues",
                                "status": {
                                    "code": "ER_SUCCESS_MATCH"
                                },
                                "values": [
                                    {
                                        "value": {
                                            "name": "AMAZON.MusicEvent",
                                            "id": "50233763604140bd63dc3efcb4655da7"
                                        }
                                    },
                                    {
                                        "value": {
                                            "name": "AMAZON.MusicGroup",
                                            "id": "df4d5e44382fe80d63a0fdcf6a6bae14"
                                        }
                                    },
                                    {
                                        "value": {
                                            "name": "AMAZON.MusicVideo",
                                            "id": "2092faa49a74029f68298fda145aeb2f"
                                        }
                                    },
                                    {
                                        "value": {
                                            "name": "AMAZON.MusicVenue",
                                            "id": "b00a06892966bc74d69e97694cf47072"
                                        }
                                    },
                                    {
                                        "value": {
                                            "name": "AMAZON.MusicAlbum",
                                            "id": "9886c4329a7e289fcc72f2165bb62033"
                                        }
                                    }
                                ]
                            }
                        ]
                    },
                    "confirmationStatus": "NONE"
                }
            }
        }
    }
}

By having this information stored in your logs, you can refer back to them and see how users are interacting with your skills, what kinds of devices they are using, and a number of other interesting insights about how your skill is being used.

You can place a console.log() statement anywhere in your code to leave yourself any debugging or relevant information that helps you during your development as well. As I build more complex functionality, it is useful to know where my code is failing, even if it fails silently, without an error. A good example of this is when making an HTTPS call. Because it is an asynchronous call, you have to chain a few functions together in Javascript. Adding console.log() statements like any of the examples below can help you better understand what is happening in your code.

Copied to clipboard
console.log("MAKING THE HTTPS CALL.");

console.log("SLOT VALUE IS XX" + slotValue + "XX");

console.log("RETURNED FROM FUNCTION CALL");

console.log("IN AMAZON.STOP INTENT");

Finding Your Logs in Cloudwatch

The easiest way to find the specific logs for the Lambda you're working on is to test it. When you get your results, click on the "logs" link that is provided near your result outcome.

Alexa Blog

If you haven't tested your skill before using the Lambda "test" button, click the orange "Test" button in the top right corner of the screen. You will be presented with a new box to "Input test event." From the provided dropdown list, select "Alexa Start Session," as this will contain a sample test for starting your skill. Click "Save and Test," and you will run this test against your code.

Alexa Blog

The more deliberate way to access your logs is to go to Cloudwatch directly. To do this, go to aws.amazon.com/cloudwatch and sign in. On the left-hand side of the screen, there is a navigation bar with a link to "Logs."

Alexa Blog

On the following page, you should see a list of the Log Groups your skills have created. Click on the name of the Lambda function you're interested in, and it will show you all of the logs that have been created by your Lambda. The more console.log() statements you create, the more data you'll have to work with in your logs.

If you haven't already, add some console.log() statements to your Lambda code the next time you need some insight into what is happening in your code!