Logging in Google Apps Script

I have some Google Apps Script code that archives emails. I can see if the code ran without errors in the execution log, but I would like to also see how many emails were archived on a run to know if it changed the state of my inbox.

We can use the execution log to do some simple logging.

function archiveInbox() {
    var threads = GmailApp.search('label:inbox from:(jobalerts-noreply@linkedin.com) older_than:4d');
    for (var i = 0; i < threads.length; i++) {
        console.log(
            'Moving thread to archive: "%s"',
            threads[i].getFirstMessageSubject()
        );
        threads[i].moveToArchive();
    }
}

In the snippet above, the console.log statement gets pushed directly to the execution log. This will leave a note whenever a thread gets archived with the subject of the thread.

To view the logs, check the execution log from the Executions sidebar menu.

Get Notified of New Posts

Sign up for the newsletter and I'll send you an email when there's a new post.