# Logs

The logs endpoint shows any debug logs produced by executed Functions. To have logs show up on this endpoint, add

```javascript
console.log("This is an info level log message from my function")

console.warn("This is a warning log message from my function")

console.error("This is an error log message from my function")
```

to your Function code and invoke it. The log line will show up at the `/Logs` sub-resource of an [Environment](/docs/serverless/api/resource/environment). Only Environments with a deployed build will generate logs.

## Serverless toolkit usage

The [Serverless Toolkit](/docs/labs/serverless-toolkit) has debug log tail ability built in to aid in debugging from the command line. To tail logs for deployed functions, execute the following command from within a deployed Serverless project:

```bash
twilio serverless:logs --tail
```

## Debugger and webhooks

Log lines written at WARN or ERROR level will also show up in the [Twilio Debugger](https://www.twilio.com/console/debugger). Debugger will store WARN and ERROR logs for up to 30 days. You can set up a webhook to trigger on all (not just Functions) errors and warnings [here](https://www.twilio.com/console/debugger/alert-triggers).

## Stream live logs in the browser

For Functions created via the API, you can show live logs for debugging by loading [the Services page](/console/functions/overview/services) in the Twilio Console, opening the Service to track, and clicking the **Show Logs** toggle in the bottom-right corner of the UI.

> \[!NOTE]
>
> The **Show Logs** toggle will only be visible if you have a live build associated with your environment via a [Deployment](/docs/serverless/api/resource/deployment).

### Limits and constraints

* Logs are available for the last 30 days only.
* Logs are limited to the last 3KB of output per invocation. If you log over this limit, the output will be truncated.
* Only 10,000 results are returned for a given query. Use the `StartDate` and `EndDate` filters to limit your query.
* The `/Logs` endpoint is rate limited to 5 requests per second.
* Timestamps for logs are to millisecond precision, which means that logs produced within the same millisecond could appear out of order.

### Supported filters

Use these parameters on the `/Logs` request to retrieve specific logs.

| Name          | Type                |
| ------------- | ------------------- |
| `StartDate`   | ISO 8601(optional)  |
| `EndDate`     | ISO 8601(optional)  |
| `FunctionSid` | sid\<ZH> (optional) |
| `PageToken`   | string (optional)   |
| `PageSize`    | int (default: 50)   |

## Log Properties

```json
{"type":"object","refName":"serverless.v1.service.environment.log","modelName":"serverless_v1_service_environment_log","properties":{"sid":{"type":"string","minLength":34,"maxLength":34,"pattern":"^NO[0-9a-fA-F]{32}$","nullable":true,"description":"The unique string that we created to identify the Log resource."},"account_sid":{"type":"string","minLength":34,"maxLength":34,"pattern":"^AC[0-9a-fA-F]{32}$","nullable":true,"description":"The SID of the [Account](/docs/iam/api/account) that created the Log resource."},"service_sid":{"type":"string","minLength":34,"maxLength":34,"pattern":"^ZS[0-9a-fA-F]{32}$","nullable":true,"description":"The SID of the Service that the Log resource is associated with."},"environment_sid":{"type":"string","minLength":34,"maxLength":34,"pattern":"^ZE[0-9a-fA-F]{32}$","nullable":true,"description":"The SID of the environment in which the log occurred."},"build_sid":{"type":"string","minLength":34,"maxLength":34,"pattern":"^ZB[0-9a-fA-F]{32}$","nullable":true,"description":"The SID of the build that corresponds to the log."},"deployment_sid":{"type":"string","minLength":34,"maxLength":34,"pattern":"^ZD[0-9a-fA-F]{32}$","nullable":true,"description":"The SID of the deployment that corresponds to the log."},"function_sid":{"type":"string","minLength":34,"maxLength":34,"pattern":"^ZH[0-9a-fA-F]{32}$","nullable":true,"description":"The SID of the function whose invocation produced the log."},"request_sid":{"type":"string","minLength":34,"maxLength":34,"pattern":"^RQ[0-9a-fA-F]{32}$","nullable":true,"description":"The SID of the request associated with the log."},"level":{"type":"string","nullable":true,"description":"The log level."},"message":{"type":"string","nullable":true,"description":"The log message."},"date_created":{"type":"string","format":"date-time","nullable":true,"description":"The date and time in GMT when the Log resource was created specified in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format."},"url":{"type":"string","format":"uri","nullable":true,"description":"The absolute URL of the Log resource."}}}
```

## Fetch a Log resource

`GET https://serverless.twilio.com/v1/Services/{ServiceSid}/Environments/{EnvironmentSid}/Logs/{Sid}`

### Path parameters

```json
[{"name":"ServiceSid","in":"path","description":"The SID of the Service to fetch the Log resource from.","schema":{"type":"string"},"required":true},{"name":"EnvironmentSid","in":"path","description":"The SID of the environment with the Log resource to fetch.","schema":{"type":"string","minLength":34,"maxLength":34,"pattern":"^ZE[0-9a-fA-F]{32}$"},"required":true},{"name":"Sid","in":"path","description":"The SID of the Log resource to fetch.","schema":{"type":"string","minLength":34,"maxLength":34,"pattern":"^NO[0-9a-fA-F]{32}$"},"required":true}]
```

Fetch a Log

```js
// Download the helper library from https://www.twilio.com/docs/node/install
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";

// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = twilio(accountSid, authToken);

async function fetchLog() {
  const log = await client.serverless.v1
    .services("ServiceSid")
    .environments("ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
    .logs("NOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
    .fetch();

  console.log(log.sid);
}

fetchLog();
```

```python
# Download the helper library from https://www.twilio.com/docs/python/install
import os
from twilio.rest import Client

# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
client = Client(account_sid, auth_token)

log = (
    client.serverless.v1.services("ServiceSid")
    .environments("ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
    .logs("NOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
    .fetch()
)

print(log.sid)
```

```csharp
// Install the C# / .NET helper library from twilio.com/docs/csharp/install

using System;
using Twilio;
using Twilio.Rest.Serverless.V1.Service.Environment;
using System.Threading.Tasks;

class Program {
    public static async Task Main(string[] args) {
        // Find your Account SID and Auth Token at twilio.com/console
        // and set the environment variables. See http://twil.io/secure
        string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
        string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");

        TwilioClient.Init(accountSid, authToken);

        var log = await LogResource.FetchAsync(
            pathServiceSid: "ServiceSid",
            pathEnvironmentSid: "ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
            pathSid: "NOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");

        Console.WriteLine(log.Sid);
    }
}
```

```java
// Install the Java helper library from twilio.com/docs/java/install

import com.twilio.Twilio;
import com.twilio.rest.serverless.v1.service.environment.Log;

public class Example {
    // Find your Account SID and Auth Token at twilio.com/console
    // and set the environment variables. See http://twil.io/secure
    public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
    public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");

    public static void main(String[] args) {
        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
        Log log = Log.fetcher("ServiceSid", "ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "NOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
                      .fetch();

        System.out.println(log.getSid());
    }
}
```

```go
// Download the helper library from https://www.twilio.com/docs/go/install
package main

import (
	"fmt"
	"github.com/twilio/twilio-go"
	"os"
)

func main() {
	// Find your Account SID and Auth Token at twilio.com/console
	// and set the environment variables. See http://twil.io/secure
	// Make sure TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN exists in your environment
	client := twilio.NewRestClient()

	resp, err := client.ServerlessV1.FetchLog("ServiceSid",
		"ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
		"NOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	} else {
		if resp.Sid != nil {
			fmt.Println(*resp.Sid)
		} else {
			fmt.Println(resp.Sid)
		}
	}
}
```

```php
<?php

// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once "/path/to/vendor/autoload.php";

use Twilio\Rest\Client;

// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);

$log = $twilio->serverless->v1
    ->services("ServiceSid")
    ->environments("ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
    ->logs("NOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
    ->fetch();

print $log->sid;
```

```ruby
# Download the helper library from https://www.twilio.com/docs/ruby/install
require 'twilio-ruby'

# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_AUTH_TOKEN']
@client = Twilio::REST::Client.new(account_sid, auth_token)

log = @client
      .serverless
      .v1
      .services('ServiceSid')
      .environments('ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
      .logs('NOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
      .fetch

puts log.sid
```

```bash
# Install the twilio-cli from https://twil.io/cli

twilio api:serverless:v1:services:environments:logs:fetch \
   --service-sid ServiceSid \
   --environment-sid ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \
   --sid NOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
```

```bash
curl -X GET "https://serverless.twilio.com/v1/Services/ServiceSid/Environments/ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Logs/NOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
```

```json
{
  "sid": "NOaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "account_sid": "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "service_sid": "ServiceSid",
  "environment_sid": "ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
  "build_sid": "ZB00000000000000000000000000000000",
  "deployment_sid": "ZD00000000000000000000000000000000",
  "function_sid": "ZH00000000000000000000000000000000",
  "request_sid": "RQ00000000000000000000000000000000",
  "level": "warning",
  "message": "This is a warning",
  "date_created": "2018-11-10T20:00:00Z",
  "url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments/ZE00000000000000000000000000000000/Logs/NO00000000000000000000000000000000"
}
```

## Read multiple Log resources

`GET https://serverless.twilio.com/v1/Services/{ServiceSid}/Environments/{EnvironmentSid}/Logs`

### Path parameters

```json
[{"name":"ServiceSid","in":"path","description":"The SID of the Service to read the Log resource from.","schema":{"type":"string"},"required":true},{"name":"EnvironmentSid","in":"path","description":"The SID of the environment with the Log resources to read.","schema":{"type":"string","minLength":34,"maxLength":34,"pattern":"^ZE[0-9a-fA-F]{32}$"},"required":true}]
```

### Query parameters

```json
[{"name":"FunctionSid","in":"query","description":"The SID of the function whose invocation produced the Log resources to read.","schema":{"type":"string","minLength":34,"maxLength":34,"pattern":"^ZH[0-9a-fA-F]{32}$"},"examples":{"readEmpty":{"value":"ZH00000000000000000000000000000000"}}},{"name":"StartDate","in":"query","description":"The date/time (in GMT, ISO 8601) after which the Log resources must have been created. Defaults to 1 day prior to current date/time.","schema":{"type":"string","format":"date-time"},"examples":{"readEmpty":{"value":"2018-11-10T20:00:00Z"}}},{"name":"EndDate","in":"query","description":"The date/time (in GMT, ISO 8601) before which the Log resources must have been created. Defaults to current date/time.","schema":{"type":"string","format":"date-time"},"examples":{"readEmpty":{"value":"2018-12-10T20:00:00Z"}}},{"name":"PageSize","in":"query","description":"How many resources to return in each list page. The default is 50, and the maximum is 1000.","schema":{"type":"integer","format":"int64","minimum":1,"maximum":1000}},{"name":"Page","in":"query","description":"The page index. This value is simply for client state.","schema":{"type":"integer","minimum":0}},{"name":"PageToken","in":"query","description":"The page token. This is provided by the API.","schema":{"type":"string"}}]
```

List multiple Logs

```js
// Download the helper library from https://www.twilio.com/docs/node/install
const twilio = require("twilio"); // Or, for ESM: import twilio from "twilio";

// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = twilio(accountSid, authToken);

async function listLog() {
  const logs = await client.serverless.v1
    .services("ServiceSid")
    .environments("ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
    .logs.list({ limit: 20 });

  logs.forEach((l) => console.log(l.sid));
}

listLog();
```

```python
# Download the helper library from https://www.twilio.com/docs/python/install
import os
from twilio.rest import Client

# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = os.environ["TWILIO_ACCOUNT_SID"]
auth_token = os.environ["TWILIO_AUTH_TOKEN"]
client = Client(account_sid, auth_token)

logs = (
    client.serverless.v1.services("ServiceSid")
    .environments("ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
    .logs.list(limit=20)
)

for record in logs:
    print(record.sid)
```

```csharp
// Install the C# / .NET helper library from twilio.com/docs/csharp/install

using System;
using Twilio;
using Twilio.Rest.Serverless.V1.Service.Environment;
using System.Threading.Tasks;

class Program {
    public static async Task Main(string[] args) {
        // Find your Account SID and Auth Token at twilio.com/console
        // and set the environment variables. See http://twil.io/secure
        string accountSid = Environment.GetEnvironmentVariable("TWILIO_ACCOUNT_SID");
        string authToken = Environment.GetEnvironmentVariable("TWILIO_AUTH_TOKEN");

        TwilioClient.Init(accountSid, authToken);

        var logs = await LogResource.ReadAsync(
            pathServiceSid: "ServiceSid",
            pathEnvironmentSid: "ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
            limit: 20);

        foreach (var record in logs) {
            Console.WriteLine(record.Sid);
        }
    }
}
```

```java
// Install the Java helper library from twilio.com/docs/java/install

import com.twilio.Twilio;
import com.twilio.rest.serverless.v1.service.environment.Log;
import com.twilio.base.ResourceSet;

public class Example {
    // Find your Account SID and Auth Token at twilio.com/console
    // and set the environment variables. See http://twil.io/secure
    public static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID");
    public static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN");

    public static void main(String[] args) {
        Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
        ResourceSet<Log> logs = Log.reader("ServiceSid", "ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa").limit(20).read();

        for (Log record : logs) {
            System.out.println(record.getSid());
        }
    }
}
```

```go
// Download the helper library from https://www.twilio.com/docs/go/install
package main

import (
	"fmt"
	"github.com/twilio/twilio-go"
	serverless "github.com/twilio/twilio-go/rest/serverless/v1"
	"os"
)

func main() {
	// Find your Account SID and Auth Token at twilio.com/console
	// and set the environment variables. See http://twil.io/secure
	// Make sure TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN exists in your environment
	client := twilio.NewRestClient()

	params := &serverless.ListLogParams{}
	params.SetLimit(20)

	resp, err := client.ServerlessV1.ListLog("ServiceSid",
		"ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
		params)
	if err != nil {
		fmt.Println(err.Error())
		os.Exit(1)
	} else {
		for record := range resp {
			if resp[record].Sid != nil {
				fmt.Println(*resp[record].Sid)
			} else {
				fmt.Println(resp[record].Sid)
			}
		}
	}
}
```

```php
<?php

// Update the path below to your autoload.php,
// see https://getcomposer.org/doc/01-basic-usage.md
require_once "/path/to/vendor/autoload.php";

use Twilio\Rest\Client;

// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
$sid = getenv("TWILIO_ACCOUNT_SID");
$token = getenv("TWILIO_AUTH_TOKEN");
$twilio = new Client($sid, $token);

$logs = $twilio->serverless->v1
    ->services("ServiceSid")
    ->environments("ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
    ->logs->read([], 20);

foreach ($logs as $record) {
    print $record->sid;
}
```

```ruby
# Download the helper library from https://www.twilio.com/docs/ruby/install
require 'twilio-ruby'

# Find your Account SID and Auth Token at twilio.com/console
# and set the environment variables. See http://twil.io/secure
account_sid = ENV['TWILIO_ACCOUNT_SID']
auth_token = ENV['TWILIO_AUTH_TOKEN']
@client = Twilio::REST::Client.new(account_sid, auth_token)

logs = @client
       .serverless
       .v1
       .services('ServiceSid')
       .environments('ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')
       .logs
       .list(limit: 20)

logs.each do |record|
   puts record.sid
end
```

```bash
# Install the twilio-cli from https://twil.io/cli

twilio api:serverless:v1:services:environments:logs:list \
   --service-sid ServiceSid \
   --environment-sid ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
```

```bash
curl -X GET "https://serverless.twilio.com/v1/Services/ServiceSid/Environments/ZEaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa/Logs?PageSize=20" \
-u $TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN
```

```json
{
  "logs": [],
  "meta": {
    "first_page_url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments/ZE00000000000000000000000000000000/Logs?StartDate=2018-11-10T20%3A00%3A00Z&EndDate=2018-12-10T20%3A00%3A00Z&FunctionSid=ZH00000000000000000000000000000000&PageSize=50&Page=0",
    "key": "logs",
    "next_page_url": null,
    "page": 0,
    "page_size": 50,
    "previous_page_url": null,
    "url": "https://serverless.twilio.com/v1/Services/ZS00000000000000000000000000000000/Environments/ZE00000000000000000000000000000000/Logs?StartDate=2018-11-10T20%3A00%3A00Z&EndDate=2018-12-10T20%3A00%3A00Z&FunctionSid=ZH00000000000000000000000000000000&PageSize=50&Page=0"
  }
}
```
