How to get the most out of the new Google Apps Script debugger & logging

Jasper Duizendstra
Dec 8, 2020 · 5 min read

Building and maintaining code can be hard. There are multiple phases in the livecycle of the code, each phase with its own challenges. Being able to easily retrieve information about the execution of your code in each phase is key. This is where the new Google Apps Script IDE debugging and logging features can help. In this article I will show you what you can do with the new debugging and logging features.

Image for post
Image for post
Photo by Timothy Dykes on Unsplash

A simple Google Apps Script is used to walk through the powerful features of the debugger and logging. In the final part of the article we will take it a step further and implement the Google Cloud logging module. After reading this article you will have a good understanding of the options to debug and monitor your Apps Script applications.

The debugger

I will use the following code to showcase the features of the debugger:

The code can be started by clicking on the debug button. Make sure that you select the correct function to debug.

Image for post
Image for post

In the screenshot below you can see the debugger in action. The function started and stopped at the “debugger” keyword on line 6.

Image for post
Image for post

To resume the execution of the code I clicked on the play button. This action will resume the function until it encounters a debugger statement or a breakpoint. In this case it encountered a breakpoint on line 14 and stopped. The three buttons next to the play button provide finer grained control over the debugger. When you hover over the buttons they will show some information on what they do.

On line 14 I placed a breakpoint. Breakpoints can be placed by clicking left of the line number where you would like the debugger to stop. Notice the variables section and the localNumber variable pointed out by the green arrow. The variable section will show you the values of all the variables.

The execution log pointed out by the yellow arrow contains the log information. This information will also be shown if the debugger is inactive.

Image for post
Image for post

The call stack

The example code defines a global variable, and loops over two arrays. The outer loop walks through an array of four numbers and the inner loop walk through an array of 2 characters.

In the screenshot below I placed a breakpoint on row 19 and ran the debugger:

Image for post
Image for post

In the call stack I selected the first line and the localCharacter variable shows as “A”. However when you select line 16 as shown in the next screenshot it becomes interesting.

Image for post
Image for post

Now the local variable is localNumber and has the value 1. This is the value of localNumber when the inner loop over the two characters started. This way you can see all the variables that are relevant to the current state of the code.

Logging

Nothing special here, but when we run it we see the following:

Image for post
Image for post

We now have two extra log levels, Error and Warning. These levels are very useful to distinguish between the severity of the message.

Stackdriver logging

The screenshot below shows the log browser on the Google Cloud Platform:

Image for post
Image for post

Besides a great way to explore the logs it is also possible to direct the logs of multiple scripts to a single location. Another great feature are the alerts, based on metrics you can trigger an email or other alerts to notify a stakeholder when something is wrong with the application.

Timing

In the following code we can measure the time a section of code takes to execute.

Image for post
Image for post

Simple, yet powerful. This piece of code took 52 milliseconds to run.

Wrapping up

Stackdriver logging is useful in a professional environment where reliability becomes more important. It takes a bit more time to set up, however it is worth it when you get an email with the error before you get a phone call from the user of your script.

Google Developers Experts

Experts on various Google products talking tech.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store