Debugging Google Apps Scripts with Stackdriver Logs

Debugging Google Apps Scripts with Stackdriver Logs

Since the inception of Fulcrum, we have made integrating with other platforms using our robust developer tools a top feature. One powerful and easy integration is to utilize Google Apps Script with Fulcrum webhooks. You can read about integrating with Google Apps Script in a previous article.

There are several ways to capture logs from your Google Apps Scripts. A straightforward approach is to use Logger.log() or console.log() directly; however, this does not work for every scenario. When processing a Fulcrum webhook, you often want to inspect the payload. One way to check the payload is to email it to yourself. Other times you might want to log specific variables. To do this, you could integrate an extra function to write a new row to a Google Sheet each time you want to write a log entry.

The setup discussed herein uses a project in GCP as a destination for log entries using Stackdriver Logs to monitor and debug your apps script code. This article assumes you have read our apps script integration article, know how to create an apps script, and can configure a webhook to point at your script URL. Google allows up to 50gb per month for logs on the free tier.

Project Setup in GCP

First, create a project in your Google Cloud Platform account. Do this in the resource manager page of your GCP account.

GCP new project

After you create a project, copy the project number for easy retrieval. We use the project number to assign the Google apps script to the project where we capture the logs for our Google apps script.

GCP project number

GCP settings & project number

Next, navigate back to the apps script editor, grab the project number, and assign our script to the GCP project number.

GAS resources gcp

Google Apps Resources menu

GAS gcp project number

Setting the project number for the google apps script

We now have a stream of logs for each time our script catches a webhook. Below is a simple hello world apps script that captures the webhook, checks if it is for the form we wish to listen for and console.log() the payload. You can add the console.log() anywhere in your script now to create a new log entry.

// Special Apps Script function to process HTTP POST request
function doPost(e) {
  var jsonString = e.postData.getDataAsString();
  var payload = JSON.parse(jsonString);
  var formId = "your-form-id-here";
  if (formId == payload["data"]["form_id"]) {
    console.log(
      "We caught a webhook that matches our form id! " +
        payload["data"]["form_id"]
    );
    console.log(payload);
    // Do other stuff with the payload
  }
}
Stackdriver log gif

There is a limit to the number of projects you can create in GCP; however, you can assign multiple apps scripts to the same project and then filter your logs as necessary.

Fulcrum is a data collection platform that enables organizations to reduce costs, access critical data in real time, and improve decision making at every level. With Fulcrum, you can create custom apps using our simple drag-and-drop builder to turn your paper documents into digital forms that your field teams can quickly complete on mobile devices.

About the author

Ry is an integrations engineer with Fulcrum. He works with clients to leverage the full power of Fulcrum platform in each of their unique workflows.

Subscribe via Email for updates

Get started with Fulcrum today

Try it FREE