Class Logger

  • The Logger class is used to write to the Execution log and Google Cloud Logging, especially for structured logging and JSON payloads.

  • Use the clear() method to clear the log and getLog() to retrieve all messages in the current log.

  • The log() method can write various data types to the log, including strings, JavaScript objects, or objects with a message property.

  • The log() method also supports writing formatted strings to the log using placeholders and values.

Logger

This class allows the developer to write to the Execution log and toGoogle Cloud Logging if the script is associated withastandardCloud Project. This class is preferred for structured logging andjsonPayload supportin Cloud Logging. For time-based logging, useconsole.

Methods

MethodReturn typeBrief description
clear()voidClears the log.
getLog()StringReturns a complete list of messages in the current log.
log(data)LoggerWrites the data to the log.
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 oremail the entire log output generated during script execution.

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

Return

String — the log from the logging console


log(data)

Writes the data to the log. The data can be a string, a JavaScript object, or an object with amessage property.

Logger.log("my log message");// Info   my logmessageLogger.log({key:"value"});// Info   {key=value}Logger.log({message:"my log message",data:{key:"value"}})// Info   my logmessage

When passing an object, if the object contains amessage property, that property isused as the log message. Otherwise, thetoString() method is called to convert theobject to a string. All other properties which are JSON serializable are included as part ofthejsonPayload in theLogEntry, similar to the example below:

{"insertId":"w5eib...","jsonPayload":{"message":"my log message","serviceContext":{"service":"AKfyc..."},"data":{"key":"value"}},"resource":{"type":"app_script_function","labels":{"invocation_type":"editor","function_name":"unknown","project_id":"1234567890"}},"timestamp":"2024-11-15T23:28:19.448591Z","severity":"INFO","labels":{"script.googleapis.com/user_key":"AOX2d...","script.googleapis.com/process_id":"EAEA1...","script.googleapis.com/project_key":"MQXvl...","script.googleapis.com/deployment_id":"AKfyc..."},"logName":"projects/[PROJECT_ID]/logs/script.googleapis.com%2Fconsole_logs","receiveTimestamp":"2024-11-15T23:28:20.363790313Z"}

Parameters

NameTypeDescription
dataObjectthe object 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. Thestring can include multiple%s placeholders, which are replaced with correspondingvalues from the list of arguments, converted to strings.

// Log the number of Google Groups you belong to.constgroups=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 ofvalues arguments
valuesObject...a variable number of values to insert into the format string

Return

Logger — the Logger, for chaining

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-11 UTC.