Class Logger

Logger

This class allows the developer to write out text to the debugging logs.

Methods

MethodReturn typeBrief description
clear()voidClears the log.
getLog()StringReturns a complete list of messages in the current log.
log(data)LoggerWrites the string to the logging console.
log(format, values)LoggerWrites a formatted string to the logging console, using the format and values provided.

Detailed documentation

clear()

Clears the log.


getLog()

Returns a complete list of messages in the current log. This method can be used to save or email the entire log output generated during script execution.

// Generate a log, then email it to the person who ran the script.
var files = DriveApp.getFiles();
while (files.hasNext()) {
  Logger.log(files.next().getName());
}
var recipient = Session.getActiveUser().getEmail();
var subject = 'A list of files in your Google Drive';
var body = Logger.getLog();
MailApp.sendEmail(recipient, subject, body);

Return

String — the log from the logging console


log(data)

Writes the string to the logging console. To view the logged output, select View > Show logs. This can be very useful for debugging scripts.

Parameters

NameTypeDescription
dataObjectthe message to log

Return

Logger — the Logger, for chaining.


log(format, values)

Writes a formatted string to the logging console, using the format and values provided. The string can include multiple %s placeholders, which are replaced with corresponding values from the list of arguments, converted to strings.

// Log the number of Google Groups you belong to.
var groups = GroupsApp.getGroups();
Logger.log('You are a member of %s Google Groups.', groups.length);

Parameters

NameTypeDescription
formatStringa format string that contains as many instances of %s as the number of values arguments
valuesObject...a variable number of values to insert into the format string

Return

Logger — the Logger, for chaining