Skip to contentSkip to navigationSkip to topbar
Rate this Page:

Debug your Function(s)



Debug from the Console

debug-from-the-console page anchor

If you are actively working on your Function in the Twilio Console, you can display live logs that can aid you in debugging your deployed Functions.

To start, click the Enable live logs button near the bottom-right of the Functions editor. As your Function is invoked, any log statements in your code will be displayed in real time.

Given the following Function code, deployed with Public Visibility:


_10
exports.handler = function(context, event, callback) {
_10
console.log("Invoked with: ", event);
_10
return callback(null, "OK");
_10
};

If you invoke the Function with some arguments, you should see their values displayed in the console as shown below.


_10
curl https://your-domain-2242.twil.io/log-demo?key=value

Function Live Logs.

In this case, Twilio Functions streamed the start, end, and any console output of the Function into the Twilio console.


If you leave your console session by navigating away or closing the browser window, the log messages that your Function(s) generate will still be accessible as Logs via the Serverless API.

For example, suppose the above Function generated logs at different error levels (info, warn, and error). You can use the Twilio CLI to retrieve these logs from the Serverless API:


_10
twilio api:serverless:v1:services:environments:logs:list \
_10
--service-sid ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \
_10
--environment-sid ZEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

The output may look like this:


_10
[ERROR][2021-10-27T22:07:54Z]: Invoked with: { key: 'value' }
_10
[WARNING][2021-10-27T22:07:54Z]: Invoked with: { key: 'value' }
_10
[INFO][2021-10-27T22:07:54Z]: Invoked with: { key: 'value' }

For a less verbose command, you can also install the Serverless Toolkit plugin for the CLI and issue the following command instead:


_10
twilio serverless:logs \
_10
--service-sid=ZSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX \
_10
--environment=ZEXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Just as before, the output may look like this:


_10
[ERROR][2021-10-27T22:07:54Z]: Invoked with: { key: 'value' }
_10
[WARNING][2021-10-27T22:07:54Z]: Invoked with: { key: 'value' }
_10
[INFO][2021-10-27T22:07:54Z]: Invoked with: { key: 'value' }

For more information about Logs, other ways to retrieve them, and their limitations, please refer to the Logs API Reference.


Debug in real-time with Node.js

debug-in-real-time-with-nodejs page anchor

If you want to run your Function with a fully-featured debugger attached and have the ability to step through your code, then you will need to leverage the Serverless Toolkit.

These setup instructions will allow you to run your Function on your local machine and step through its execution using any Node.js supported debugger, such as Visual Studio Code(link takes you to an external page) or the Chrome Developer Tools(link takes you to an external page).

(warning)

Warning

You cannot use this method to debug Services that are created from the Console UI. Debugging in this manner is only possible if your project was initially created with the Serverless Toolkit.


Monitor for errors and failures

monitor-for-errors-and-failures page anchor

If your Function returns an error, it will be reported in the Debugger(link takes you to an external page).

(information)

Info

The Debugger supports search queries, as well as the option to group errors by their Function's URL or by error code.


Rate this Page: