Spreadsheet Automation 101 Lesson 2: Logger.log()

About this Tutorial

Learn to log any variable you want and get notes inside your log. Debug your code to understand what’s going on and find out where you went wrong.

Video Transcript

0:01 Hello, welcome. In this video, I wanna go over the final points of logger dot log, the logger that we use to see what's inside of our code while it's running.
0:10 And I wanna show you a couple cool tricks we can use with it and, and keep us, you know, involved with what's going on inside of our script.
0:18 So here we have our go back to our for loop. We have our array. First off, we can log an entire array logger dot log.
0:27 Just wanna mention that the syntax here is a capital L logger. Logger is one of those special functions that exist and give us a lot of options inside of Google Script here.
0:38 It does not have a parenthesis after it, but after the log it does, let me type that again slower so you can see what goes on.
0:46 So if we type logger dot, we have three options here. We can clear the log, get the log or log something, we can add something to the log of these three.
0:55 I think I have used log a hundred percent of the time. I don't think I've literally ever gotten the log.
1:00 I might have cleared the log. Sometimes it does help if you're logging a lot of things and you're trying to do something to, you're seeing it here.
1:07 Clear might help you. I don't think I've ever used it honestly, but we're just gonna use log. We do need parentheses for the log to work, and we can log any variable we want.
1:19 We can also log just any note. Hello. So we can write a string within quotes. We can write logger dot log Again, put the array here.
1:28 We can log anything and everything. So let me save this and let's run this and just see what happens. Right?
1:34 Log starts at, hello. Then we have literally the entire array here. We have three. Then each array is put, each part of the array is put out here.
1:44 Okay, well this is great, but our code here looks pretty messy. We have logger dot log, hello, logger dot log array array length.
1:53 Something we could do is, is sort of name each part of this and say array, have a colon, and then add plus sign and have the array.
2:05 We can also turn this into quote array length, give in another quote, and then the plus sign, right? This plus sign is not actually adding anything.
2:19 It's concatenating these two things together. It's, it's combining them together. So we have array length and then we can even type in here, you know, something else, but we don't want to do that right now.
2:31 So let's look at this, what will happen here? Array, first, second, third, array length three. So we get some notes inside of our log here.
2:41 Execution log. Let me show you one other place where this execution log shows up, because sometimes you might cross this out.
2:49 You might be like, where did that go? I want to review it later. Go over here on executions, over to the left.
2:55 Click on executions. Here are all the executions of our scripts. The last one we did, we did it from the editor or deployment, was from the head.
3:06 In the editor, the for loop. If we click on it, we will also see our logs here. So these logs, I do not think they last forever but they do last I think for some amount of minutes or hours.
3:23 So they're, they're helping us. What I use them for is debugging to really help me understand what's going on in my code.
3:29 Where am I in my code, what's the output of that? And let me see that again and again, really helps with arrays.
3:36 So you don't have to like print things to the sheet. You can do it right here. You can push things.
3:43 Well, I think in bracket notation you saw that we can push to an array. So you can build up an array and then if something goes wrong, you can be like, okay, where did I go wrong here?
3:53 I really like the logger. I think it's fantastic and useful all the time, especially when we're starting. Just log anything.
4:01 If you're, if you're worried that something is not necessarily correct, log it, look at it, see what's going on. One thing I do want to point out here is that this array logger dot log, if I just do the variable array, it looks a little different than if I add this string and I'll explain why in one second.
4:21 Let me show it to you. So the first one is with a bracket for second, third, but the second one doesn't have the brackets.
4:27 The reason is, is that this string makes the entire log all of that a string, so it un araise it and puts it into a string.
4:40 So that's something to watch out for. That's one weird thing to watch out for. But yeah, it's really fun to label your logs, understand what you're reading here, how it matches what you're doing in your script.
4:50 Very, very useful. Especially when we will use Log a lot when we use the mail app so that we don't email ourselves a ton of stuff or email somebody else.
4:59 It, it's great for prototyping, doing stuff on a sort of a scratch, like a note like, oh, can, can I actually execute this?
5:07 If you can print it to log, you can usually print it to a, a spreadsheet. You can usually email it to yourself.
5:12 All those types of cool things you can do. Love log, love logging stuff. Thanks for watching. Bye.

Courses

Spreadsheet Automation 101: Introduction to Pre-course Videos

Breaking Through Errors In Apps Script

Think Like a Programmer: Develop The Mindset of an Apps Script Coder

Tips to Navigating Thousands of Lines of Code In Apps Script

Spreadsheet Automation 101: Functions

Spreadsheet Automation 101: Variables

Spreadsheet Automation 101: Dot Notation

Spreadsheet Automation 101: Camel Case

Spreadsheet Automation 101: Parentheses

Spreadsheet Automation 101 Lesson 1: GetValue - Introduction to SpreadsheetApp

Spreadsheet Automation 101 Lesson 1: Spreadsheet Taxonomy

Spreadsheet Automation 101 Lesson 1: A1 Notation vs Row,Column Syntax

Spreadsheet Automation 101 Lesson 1: getActiveSpreadsheet() vs getActiveSheet()

Spreadsheet Automation 101 Lesson 1: onOpen() Trigger - Custom Menu

This Seems Like Automation

Spreadsheet Automation 101 Lesson 2: Get Values - Introduction

Spreadsheet Automation 101 Lesson 2: Arrays

Spreadsheet Automation 101 Lesson 2: For Loop

Spreadsheet Automation 101 Lesson 2: Bracket Notation

Spreadsheet Automation 101 Lesson 2: Logger.log()

Spreadsheet Automation 101 Lesson 2: If ( ){ } and Checkboxes

Spreadsheet Automation 101 Lesson 2: onEdit() Trigger

Introduction to Spreadsheet Automation 101 Lesson 3

Spreadsheet Automation 101 Lesson 3: MailApp

Spreadsheet Automation 101 Lesson 3: Email Yourself For Loop

Spreadsheet Automation 101 Lesson 3: Send Email Every Week Trigger

Spreadsheet Automation 101 Lesson 3: Email Other People For Loop

Spreadsheet Automation 101 Lesson 4: Access APIs Introduction

Spreadsheet Automation 101 Lesson 4: UrlFetchApp

Spreadsheet Automation 101 Lesson 4: OmdbAPI get ApiKey, get Data in URL

Spreadsheet Automation 101 Lesson 4: OmdbAPI get data in Apps Script

Spreadsheet Automation 101 Lesson 4: JSON (beautifier) and OmdbAPI parameters

Spreadsheet Automation 101 Lesson 4: OmdbAPI Parameter Picker

Automatically Clear Content | Refresh Reuse Recycle Templates

Automate Google Sheets With Zero Experience

Automatically Uncheck A Daily Checklist

Activate A Certain Sheet When Opening a Spreadsheet

Scoping Functions in Apps Script