Overview
Loggers are used to debug scripts, and help you understand how your script is behaving. Using Loggers in App Scripts allows you to output an error message to the App Script error log during script execution.
They are invaluable troubleshooting tools for diagnosing issues with your scripts as they can show you which values are being passed through the script, and you can see which parts of the script are being executed, even if the script failed.
This guide details the two types of Logger Messages, as well as Best Practices to follow.
🗒️ Logger Messages
There are two types of logger messages: INFO, and ERROR.
logger.info(message)
- Logs an "INFO" message to the App Scripts error log.
- When returning a log message, the result MUST be a string
.toString()
logger.error(message)
- Logs an "ERROR" message to the App Scripts error log without stopping the execution of the script.
- When returning a log message, the result MUST be a string
.toString()
Example
The example below shows an INFO logger with a custom message used to identify the user who nulled out the "Inactive Date" field's value, and the timestamp of when it happened.
And this is the syntax used to generate the logger message:
logger.info(currentValues[lastUser]['First Name'].toString() + " " +
currentValues[lastUser]['Last Name'].toString() +
" caused Inactive Date to null out on " + currentValues[updated].toString())
For additional examples of how to use loggers while troubleshooting, see the App Script Troubleshooting - Common Errors and Resolutions.
🥇 Best Practices for Using Loggers
1. Use Loggers Early and Often During Development
Use logger.info()
to track values at different stages while building out your script:
logger.info("Starting script...")
logger.info("Priority value: " + currentValues["Priority"])
2. Check for null
Before Logging
Avoid null
value confusion or errors in logs by checking for null
before logging:
if (currentValues["Due Date"] != null) {
logger.info("Due Date: " + currentValues["Due Date"])
} else {
logger.info("Due Date is null")
}
3. Use Loggers to Check Conditional Logic
When using if
statements or branching logic, log the path taken:
if (priority == "High") {
logger.info("High priority condition met")
} else {
logger.info("Condition not met, priority: " + priority)
}
4. Use Loggers to View Results Returned
If your script loops through records, you can log the number of items returned:
def results = recordSearch.findAllMatchingRecordsByViewId(12345)
logger.info("Found " + results.size() + " records")
for (record in results) {
logger.info("Record Name: " + record["Name"])
}
5. Use Meaningful Labels in Logger Messages
Make it easy to search and understand logs by labeling what you're logging.
What to Avoid:
logger.info(previousValues["Status"])
What to use instead:
logger.info("Task Status before update: " + previousValues["Status"])
6. User Loggers Sparingly in Production
- It is important to keep in mind that loggers tend to slow down the execution time of the script and so it is recommended to use them sparingly.
- You can leave loggers commented out in your script so that they are not running each time the script executes, and then if the script begins throwing errors, you can temporarily comment the loggers back in to use them while troubleshooting the script errors.
7. Checking Edge Cases
Use logs to trace values when exceptions could occur:
if (currentValues["Start Date"] == null) {
logger.info("Start Date is missing; skipping duration check")
}
8. Avoid Logging Sensitive Data
Don't log confidential or personal information (e.g. passwords, SSNs) as the error logs may be accessible by other admins.
Related Articles:
Comments
0 comments
Please sign in to leave a comment.