Class Logger
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
โปรแกรมบันทึก
คลาสนี้ช่วยให้นักพัฒนาแอปเขียนลงในบันทึกการดำเนินการและการบันทึกของ Google Cloud ได้หากสคริปต์เชื่อมโยงกับโปรเจ็กต์ Cloud มาตรฐาน เราขอแนะนำให้ใช้คลาสนี้สำหรับการบันทึกที่มีโครงสร้างและjsonPayload
การสนับสนุนใน Cloud Logging สําหรับการบันทึกตามเวลา ให้ใช้ console
เอกสารประกอบโดยละเอียด
getLog()
แสดงรายการข้อความทั้งหมดในบันทึกปัจจุบัน วิธีนี้สามารถใช้เพื่อบันทึกหรือส่งออกเอาต์พุตบันทึกทั้งหมดที่สร้างขึ้นระหว่างการเรียกใช้สคริปต์ทางอีเมล
// Generate a log, then email it to the person who ran the script.
const files = DriveApp.getFiles();
while (files.hasNext()) {
Logger.log(files.next().getName());
}
const recipient = Session.getActiveUser().getEmail();
const subject = 'A list of files in your Google Drive';
const body = Logger.getLog();
MailApp.sendEmail(recipient, subject, body);
รีเทิร์น
String
— บันทึกจากคอนโซลการบันทึก
log(data)
เขียนข้อมูลลงในบันทึก ข้อมูลอาจเป็นสตริง ออบเจ็กต์ JavaScript หรือออบเจ็กต์ที่มีพร็อพเพอร์ตี้ message
Logger.log("my log message");
// Info my logmessage
Logger.log({ key: "value" });
// Info {key=value}
Logger.log({ message: "my log message", data: { key: "value" } })
// Info my logmessage
เมื่อส่งออบเจ็กต์ หากออบเจ็กต์มีพร็อพเพอร์ตี้ message
ระบบจะใช้พร็อพเพอร์ตี้นั้นเป็นข้อความบันทึก มิเช่นนั้น ระบบจะเรียกใช้เมธอด toString()
เพื่อแปลงออบเจ็กต์เป็นสตริง พร็อพเพอร์ตี้อื่นๆ ทั้งหมดที่ซีเรียลไลซ์เป็น JSON ได้จะรวมอยู่ใน jsonPayload
ใน LogEntry
คล้ายกับตัวอย่างด้านล่าง
{
"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"
}
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
data | Object | ออบเจ็กต์ที่จะบันทึก |
รีเทิร์น
Logger
— เครื่องมือบันทึกสำหรับใช้ต่อเชื่อม
เขียนสตริงที่จัดรูปแบบไปยังคอนโซลการบันทึกโดยใช้รูปแบบและค่าที่ระบุ สตริงอาจมีตัวยึดตําแหน่ง %s
หลายรายการ ซึ่งระบบจะแทนที่ด้วยค่าที่สอดคล้องกันจากรายการอาร์กิวเมนต์ที่แปลงเป็นสตริง
// Log the number of Google Groups you belong to.
const groups = GroupsApp.getGroups();
Logger.log('You are a member of %s Google Groups.', groups.length);
พารามิเตอร์
ชื่อ | ประเภท | คำอธิบาย |
format | String | สตริงรูปแบบที่มีอินสแตนซ์ %s เท่ากับจํานวนอาร์กิวเมนต์ values |
values | Object... | จํานวนค่าแบบแปรผันที่จะแทรกลงในสตริงรูปแบบ |
รีเทิร์น
Logger
— เครื่องมือบันทึกสำหรับใช้ต่อเชื่อม
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-26 UTC
[null,null,["อัปเดตล่าสุด 2025-07-26 UTC"],[[["\u003cp\u003eThe \u003ccode\u003eLogger\u003c/code\u003e class enables developers to write text to debugging logs for script analysis and troubleshooting.\u003c/p\u003e\n"],["\u003cp\u003eIt provides methods to clear the log, retrieve the entire log as a string, and write formatted or unformatted messages to the log.\u003c/p\u003e\n"],["\u003cp\u003eLogged output can be viewed within the Apps Script editor using "View > Show logs" and utilized for debugging purposes.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003egetLog()\u003c/code\u003e method allows for capturing the log content, which can then be used for saving, emailing, or further processing.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can leverage format strings and values to create structured log messages using the \u003ccode\u003elog(format, values)\u003c/code\u003e method.\u003c/p\u003e\n"]]],[],null,["# Class Logger\n\nLogger\n\nThis class allows the developer to write to the Execution log and to [Google Cloud Logging](https://cloud.google.com/logging) if the script is associated with\na [standard\nCloud Project](https://developers.google.com/apps-script/guides/cloud-platform-projects#standard). This class is preferred for structured logging and `json``Payload` support\nin Cloud Logging. For time-based logging, use [console](/apps-script/reference/base/console). \n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------------------|-------------|-----------------------------------------------------------------------------------------|\n| [clear()](#clear()) | `void` | Clears the log. |\n| [getLog()](#getLog()) | `String` | Returns a complete list of messages in the current log. |\n| [log(data)](#log(Object)) | [Logger](#) | Writes the data to the log. |\n| [log(format, values)](#log(String,Object...)) | [Logger](#) | Writes a formatted string to the logging console, using the format and values provided. |\n\nDetailed documentation\n----------------------\n\n### `clear()`\n\nClears the log.\n\n*** ** * ** ***\n\n### `get``Log()`\n\nReturns a complete list of messages in the current log. This method can be used to save or\nemail the entire log output generated during script execution.\n\n```javascript\n// Generate a log, then email it to the person who ran the script.\nconst files = DriveApp.getFiles();\nwhile (files.hasNext()) {\n Logger.log(files.next().getName());\n}\nconst recipient = Session.getActiveUser().getEmail();\nconst subject = 'A list of files in your Google Drive';\nconst body = Logger.getLog();\nMailApp.sendEmail(recipient, subject, body);\n```\n\n#### Return\n\n\n`String` --- the log from the logging console\n\n*** ** * ** ***\n\n### `log(data)`\n\nWrites the data to the log. The data can be a string, a JavaScript object, or an object with a\n`message` property.\n\n```javascript\nLogger.log(\"my log message\");\n// Info my logmessage\nLogger.log({ key: \"value\" });\n// Info {key=value}\nLogger.log({ message: \"my log message\", data: { key: \"value\" } })\n// Info my logmessage\n```\n\nWhen passing an object, if the object contains a `message` property, that property is\nused as the log message. Otherwise, the `to``String()` method is called to convert the\nobject to a string. All other properties which are JSON serializable are included as part of\nthe `json``Payload` in the [`Log``Entry`](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry), similar to the example below:\n\n```transact-sql\n{\n \"insertId\": \"w5eib...\",\n \"jsonPayload\": {\n \"message\": \"my log message\",\n \"serviceContext\": {\n \"service\": \"AKfyc...\"\n },\n \"data\": {\n \"key\": \"value\"\n }\n },\n \"resource\": {\n \"type\": \"app_script_function\",\n \"labels\": {\n \"invocation_type\": \"editor\",\n \"function_name\": \"unknown\",\n \"project_id\": \"1234567890\"\n }\n },\n \"timestamp\": \"2024-11-15T23:28:19.448591Z\",\n \"severity\": \"INFO\",\n \"labels\": {\n \"script.googleapis.com/user_key\": \"AOX2d...\",\n \"script.googleapis.com/process_id\": \"EAEA1...\",\n \"script.googleapis.com/project_key\": \"MQXvl...\",\n \"script.googleapis.com/deployment_id\": \"AKfyc...\"\n },\n \"logName\": \"projects/[PROJECT_ID]/logs/script.googleapis.com%2Fconsole_logs\",\n \"receiveTimestamp\": \"2024-11-15T23:28:20.363790313Z\"\n}\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|--------|----------|-------------------|\n| `data` | `Object` | the object to log |\n\n#### Return\n\n\n[Logger](#) --- the Logger, for chaining.\n\n*** ** * ** ***\n\n### `log(format, values)`\n\nWrites a formatted string to the logging console, using the format and values provided. The\nstring can include multiple `%s` placeholders, which are replaced with corresponding\nvalues from the list of arguments, converted to strings.\n\n```javascript\n// Log the number of Google Groups you belong to.\nconst groups = GroupsApp.getGroups();\nLogger.log('You are a member of %s Google Groups.', groups.length);\n```\n\n#### Parameters\n\n| Name | Type | Description |\n|----------|-------------|---------------------------------------------------------------------------------------------|\n| `format` | `String` | a format string that contains as many instances of `%s` as the number of `values` arguments |\n| `values` | `Object...` | a variable number of values to insert into the format string |\n\n#### Return\n\n\n[Logger](#) --- the Logger, for chaining"]]