When developing any kind of app, you often want to log information to help diagnose faults during development, to identify and diagnose customer issues, and for other purposes.

Apps Script provides three different mechanisms for logging:

  • The built-in Apps Script Logger, which is lightweight but persists only for a short time.

  • The Stackdriver Logging interface in the Developer Console, which provides logs that persist for many days after their creation.

  • The Stackdriver Error Reporting interface in the Developer Console, which collects and records errors that occur while your script is running.

These are described in the following sections. In addition to these mechanisms, you can also build your own logger code that, for example, writes information to a logging Spreadsheet or JDBC database.

Basic logging

A basic approach to logging in Apps Script is to use the built-in Logger. Logs created this way can be viewed by selecting View > Logs in the script editor. These logs are intended for simple checks during development and debugging, and do not persist very long.

For example, consider this function:

 * Logs Google Sheet information.
 * @param {number} rowNumber The spreadsheet row number.
 * @param {string} email The email to send with the row data.
function emailDataRow(rowNumber, email) {
  Logger.log('Emailing data row ' + rowNumber + ' to ' + email);
  var sheet = SpreadsheetApp.getActiveSheet();
  var data = sheet.getDataRange().getValues();
  var rowData = data[rowNumber-1].join(" ");
  Logger.log('Row ' + rowNumber + ' data: ' + rowData);
                    'Data in row ' + rowNumber,

When this script is run with inputs "2" and "john@example.com" the following logs are written:

[16-09-12 13:50:42:193 PDT] Emailing data row 2 to john@example.com
[16-09-12 13:50:42:271 PDT] Row 2 data: Cost 103.24

The Logger.log() method expects a string or other JavaScript object. The logs can only hold a limited amount of data, so avoid logging large amounts of text.

Stackdriver Logging

Apps Script also provides partial access to Google Cloud Platform's Stackdriver Logging (formerly known as "Cloud Logging"). When you require logging that persists for several days, or need a more complex logging solution for a multi-user production environment, Stackdriver Logging is the preferred choice. See the Stackdriver Logging quotas and limits for data retention and other quota details.

If you need more logging quota, you can submit a Google Cloud Platform quota request. This requires that you have access to the Cloud Platform project that your script uses.

Using Stackdriver Logging

These logs are attached to the project associated with your Apps Script, and you can view them by selecting View > Stackdriver Logging in the script editor. This opens the Stackdriver UI in a new tab (if the UI tab does not appear, check to see if your browser's pop-up blocker has blocked it). If there are no logs to display, you'll see a "Welcome to StackDriver Logging" page.

When logging, it is good privacy practice to avoid recording any personal information about the user, such as email addresses. Stackdriver logs are automatically labeled with active user keys you can use to locate a specific user's log messages when necessary.

You can log strings, formatted strings, and even JSON objects using the functions provided by the Apps Script console service.

The following example shows how to use the console service to log information in Stackdriver.

 * Logs the time taken to execute 'myFunction'.
function measuringExecutionTime() {
  // A simple INFO log message, using sprintf() formatting.
  console.info('Timing the %s function (%d arguments)', 'myFunction', 1);

  // Log a JSON object at a DEBUG level. The log is labeled
  // with the message string in the log viewer, and the JSON content
  // is displayed in the expanded log structure under "structPayload".
  var parameters = {
      isValid: true,
      content: 'some string',
      timestamp: new Date()
  console.log({message: 'Function Input', initialData: parameters});

  var label = 'myFunction() time'; // Labels the timing log entry.
  console.time(label); // Starts the timer.
  try {
    myFunction(parameters); // Function to time.
  } catch (e) {
    // Logs an ERROR message.
    console.error('myFunction() yielded an error: ' + e);
  console.timeEnd(label); // Stops the timer, logs execution duration.

Active user keys

Temporary active user keys provide a convenient way to spot unique users in Stackdriver Log entries without revealing the identities of those users. Keys are per script and change roughly once a month to provide additional security should a user reveal their identity to a developer, for example while reporting an issue.

Temporary active user keys are superior to logging identifiers like email addresses because:

  • You don't have to add anything to your logging; they're already there!
  • They don't require user authorization.
  • They protect user privacy.

To find temporary active user keys in your Stackdriver Log entries, select View > Stackdriver Logging in the script editor. Select a log entry of interest and expand it to view metadata > labels > script.googleapis.com/user_key.

You can also get the temporary active user key by calling Session.getTemporaryActiveUserKey() in your script. One way to use this method is to display the key to the user while they are running your script. Then users may choose to include their keys when reporting issues to help you identify the relevant logs.

Exception logging

When exception logging is enabled, unhandled exceptions are automatically sent to Stackdriver Logging with a stack trace. The script editor prompts you to enable exception logging the first time you select View > Stackdriver Logging or View > Stackdriver Error Reporting in a new script. You can also enable exception logging by checking the box File > Project properties > Info > Log exceptions.

Stackdriver Error Reporting

Exception logging automatically integrates with Stackdriver Error Reporting, a service that aggregates and displays errors produced in your script. To view error reports select View > Stackdriver Error Reporting in the script editor. If you are prompted to "Set up Error Reporting" this is because your script has not yet logged any exceptions. No setup is required beyond enabling exception logging.

Logging requirements

There are no requirements for using Basic logging.

For both Stackdriver Logging and Stackdriver Error Reporting, you need to have access to the Apps Script's Cloud Platform project in order to view the logs the script creates. If the script resides in a Team Drive, your access to the Cloud Platform project may be limited. See the Cloud Projects and Team Drives guide section for details.

Enviar comentários sobre…

Precisa de ajuda? Acesse nossa página de suporte.