How to Use Stackdriver Logging for Google Apps Script

December 1, 2017 Pooja Choudhary

In August 2017, Google announced general availability of a new integration with Google Stackdriver for Apps Script. Although Apps Script already has a built-in logging tool called Logger. With this tool, the messages are logged via functions such as Logger.log(). The purpose of adding Stackdriver Logging is to make Apps Script a user-friendly environment for developers. Log messages can now be sent to Stackdriver Logging using the familiar functions such as console.log() and Compared to the traditional logger, this provides more features that are suitable for a complex, multi-user production environment.


Setting Stackdriver logging in Google Apps Script

Stackdriver logs correlate to the project associated with the apps script. Once you set the console functions in your code, you can view them by selecting View > Stackdriver Logging. This will open the Stackdriver UI in a new tab, and all the logs will be visible in real time.


Exception logging

Along with Stackdriver logging, there is also Exception Logging, which can handle unhandled exceptions when they 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.


Console Bean provides the following app script logging methods for Stackdriver logging:

  1. log

  2. Info

  3. warn

  4. error

  5. time

  6. timeEnd

The code snippet below shows all the logging methods for Stackdriver:


function myFunction() {


 console.log('Hello World');'It\'s great to know Stackdriver');

 console.warn('Check if it works for you');

 console.error('else Quit');





In addition to basic info, it's also possible to send additional info as JSON objects. With Stackdriver logging UI, you can browse logs and expand them to view additional info sent as JSON objects. All the JSON data will be visible under the label ‘jsonPayload’.


function myFunction() {

  var input = 'Stackdriver Input'

  var parameters = {

     isValid: true,

     content: 'Stackdriver Content',

     timestamp: new Date()


   console.log({message: input, initialData: parameters});





Stackdriver UI

Stackdriver UI not only provides the capability to view logs in depth, it also offers powerful searching and filtering options to quickly find log entries by textual content or metadata. Log messages and errors are reported for all users of a script, with unique, temporary, and active user keys. This means log entries can be aggregated anonymously per user. For example, this allows developers to count unique users impacted by an issue, or analyze user behavior. All of this can be done without logging the user's personal information, ensuring user privacy. Keys are per script, and change roughly once a month to provide additional security, should a user reveal their identity to a developer, while reporting an issue.

Should I switch to Stackdriver logging?

Stackdriver provides advanced functionalities that a built-in debugger lacks. Besides storing logs for a longer period of time, it also provides logs in real time. Ultimately, it is a better solution for a multi-user production environment. Using stackdriver for apps script makes it easier to see all errors linked to a specific user, with no need to write any code to send error data. It logs stack traces and deep details automatically. It also helps in Troubleshooting or analyzing scripts across multiple executions. Stackdriver logging also provides its benefits when looking for trends or insights about their scripts and users. However, Stackdriver logging does not display the number of impacted users or the most recurring errors. But for use cases such as generating aggregate analysis and patterns, logs from stackdriver can be exported to BigQuery where data can be further aggregated. Also exporting logs can help in their long-term conservation as log retention in Stackdriver is seven days for free tier and 30 days for premium tier. Other than BigQuery, you can also choose to export logs to Cloud Storage, and Cloud Pub/Sub for further analysis. But note that these extra services may incur additional costs.

Overall, a better logging system for our apps, add-ons, and integrations can be accomplished using Stackdriver logging. This is a powerful tool with many advanced capabilities and features for filtering and searching. Stackdriver tool stands out with its functionality to export logs to other services. Thus, if you need a more complex and advanced logging system for your apps script applications, Stackdriver is the tool for you!


Previous Article
How Cargotec is Using G Suite to Work Differently
How Cargotec is Using G Suite to Work Differently

As their industry has evolved and the need to automate processes has become essential, Cargotec needed to f...

Next Article
Get Ready for Google Calendar’s New Look
Get Ready for Google Calendar’s New Look

Calendar’s new interface features a bright color palette with a sleek contemporary design, that will have p...