Class console
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
主控台如果指令碼與標準 Cloud 專案相關聯,這個類別可讓開發人員寫入執行記錄和 Google Cloud Logging。
如要全面支援 Cloud Logging 和結構化記錄,請使用 Logger
。console
方法會將物件序列化為字串,且不支援 jsonPayload
做為結構化記錄 LogEntry
的一部分。
function measuringExecutionTime() {
const label = "myFunction() time"; // Labels the timing log entry.
console.time(label); // Starts the timer.
try {
myFunction(); // Function to time.
} catch (e) {
// Logs an ERROR message.
console.error("myFunction() yielded an error: " + e);
}
console.timeEnd(label); // Stops the timer, logs execution duration.
}
function myFunction() {
// ...
}
內容詳盡的說明文件
error()
將空白的 ERROR 級別訊息輸出至 Stackdriver Logging。
將 ERROR 等級訊息輸出至 Stackdriver Logging。
參數
名稱 | 類型 | 說明 |
formatOrObject | Object | 字串,其中包含零個或多個替換字串,或是 JavaScript 物件 (如果沒有其他參數,則會記錄為 JavaScript 物件)。 |
values | Object... | 物件,用於取代訊息中的替換字串。這樣一來,您就能進一步控管輸出格式。 |
info()
將空白 INFO 層級訊息輸出至 Stackdriver Logging。
將 INFO 層級訊息輸出至 Stackdriver Logging。
參數
名稱 | 類型 | 說明 |
formatOrObject | Object | 字串,其中包含零個或多個替換字串,或是 JavaScript 物件 (如果沒有其他參數,則會記錄為 JavaScript 物件)。 |
values | Object... | 物件,用於取代訊息中的替換字串。這樣一來,您就能進一步控管輸出格式。 |
log()
將空白的 DEBUG 層級訊息輸出至 Stackdriver Logging。
將 DEBUG 層級訊息輸出至 Stackdriver Logging。
參數
名稱 | 類型 | 說明 |
formatOrObject | Object | 字串,其中包含零個或多個替換字串,或是 JavaScript 物件 (如果沒有其他參數,則會記錄為 JavaScript 物件)。 |
values | Object... | 物件,用於取代訊息中的替換字串。這樣一來,您就能進一步控管輸出格式。 |
time(label)
啟動計時器,用於追蹤作業所需的時間。
參數
名稱 | 類型 | 說明 |
label | String | 新計時器的名稱。 |
timeEnd(label)
停止先前透過呼叫 console.time()
啟動的計時器。時間長度會記錄在 Stackdriver 中。
參數
名稱 | 類型 | 說明 |
label | String | 要停止的計時器名稱。 |
warn()
將空白的 WARNING 級別訊息輸出至 Stackdriver Logging。
將 WARNING 等級訊息輸出至 Stackdriver Logging。
參數
名稱 | 類型 | 說明 |
formatOrObject | Object | 字串,其中包含零個或多個替換字串,或是 JavaScript 物件 (如果沒有其他參數,則會記錄為 JavaScript 物件)。 |
values | Object... | 物件,用於取代訊息中的替換字串。這樣一來,您就能進一步控管輸出格式。 |
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[null,null,["上次更新時間:2025-07-26 (世界標準時間)。"],[[["\u003cp\u003eThe \u003ccode\u003econsole\u003c/code\u003e class enables developers to write logs to Google Cloud's Stackdriver Logging service with various severity levels (DEBUG, INFO, ERROR, WARNING).\u003c/p\u003e\n"],["\u003cp\u003eIt provides methods like \u003ccode\u003econsole.log()\u003c/code\u003e, \u003ccode\u003econsole.info()\u003c/code\u003e, \u003ccode\u003econsole.error()\u003c/code\u003e, and \u003ccode\u003econsole.warn()\u003c/code\u003e for logging messages with different severity.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can use \u003ccode\u003econsole.time()\u003c/code\u003e and \u003ccode\u003econsole.timeEnd()\u003c/code\u003e to measure the execution time of specific code blocks.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003econsole\u003c/code\u003e methods accept format strings and values for structured logging, allowing objects and other data types to be included in log entries.\u003c/p\u003e\n"],["\u003cp\u003eLog entries can be viewed and analyzed in the Stackdriver Logging interface for debugging and monitoring purposes.\u003c/p\u003e\n"]]],[],null,["# Class console\n\nconsole\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).\n\nFor full support of Cloud Logging and structured logging, use [Logger](/apps-script/reference/base/logger). The `console` methods\nserialize the objects to strings and do not support `json``Payload` as part of the structured\nlogging [`Log``Entry`](https://cloud.google.com/logging/docs/reference/v2/rest/v2/LogEntry).\n\n```javascript\nfunction measuringExecutionTime() {\n const label = \"myFunction() time\"; // Labels the timing log entry.\n console.time(label); // Starts the timer.\n try {\n myFunction(); // Function to time.\n } catch (e) {\n // Logs an ERROR message.\n console.error(\"myFunction() yielded an error: \" + e);\n }\n console.timeEnd(label); // Stops the timer, logs execution duration.\n}\n\nfunction myFunction() {\n // ...\n}\n``` \n\n### Methods\n\n| Method | Return type | Brief description |\n|-----------------------------------------------------------|-------------|------------------------------------------------------------------------|\n| [error()](#error()) | `void` | Outputs a blank ERROR level message to Stackdriver Logging. |\n| [error(formatOrObject, values)](#error(Object,Object...)) | `void` | Outputs an ERROR level message to Stackdriver Logging. |\n| [info()](#info()) | `void` | Outputs blank INFO level message to Stackdriver Logging. |\n| [info(formatOrObject, values)](#info(Object,Object...)) | `void` | Outputs an INFO level message to Stackdriver Logging. |\n| [log()](#log()) | `void` | Outputs a blank DEBUG level message to Stackdriver Logging. |\n| [log(formatOrObject, values)](#log(Object,Object...)) | `void` | Outputs a DEBUG level message to Stackdriver Logging. |\n| [time(label)](#time(String)) | `void` | Starts a timer you can use to track how long an operation takes. |\n| [timeEnd(label)](#timeEnd(String)) | `void` | Stops a timer that was previously started by calling `console.time()`. |\n| [warn()](#warn()) | `void` | Outputs a blank WARNING level message to Stackdriver Logging. |\n| [warn(formatOrObject, values)](#warn(Object,Object...)) | `void` | Outputs a WARNING level message to Stackdriver Logging. |\n\nDetailed documentation\n----------------------\n\n### `error()`\n\nOutputs a blank ERROR level message to Stackdriver Logging.\n\n*** ** * ** ***\n\n### `error(formatOrObject, values)`\n\nOutputs an ERROR level message to Stackdriver Logging.\n\n#### Parameters\n\n| Name | Type | Description |\n|----------------------|-------------|-------------------------------------------------------------------------------------------------------------------------------------------|\n| `format``Or``Object` | `Object` | a string containing zero or more substitution strings, or a JavaScript object to be logged as a JavaScript object if no other parameters. |\n| `values` | `Object...` | objects with which to replace substitution strings within the message. This gives you additional control over the format of the output. |\n\n*** ** * ** ***\n\n### `info()`\n\nOutputs blank INFO level message to Stackdriver Logging.\n\n*** ** * ** ***\n\n### `info(formatOrObject, values)`\n\nOutputs an INFO level message to Stackdriver Logging.\n\n#### Parameters\n\n| Name | Type | Description |\n|----------------------|-------------|-------------------------------------------------------------------------------------------------------------------------------------------|\n| `format``Or``Object` | `Object` | a string containing zero or more substitution strings, or a JavaScript object to be logged as a JavaScript object if no other parameters. |\n| `values` | `Object...` | objects with which to replace substitution strings within the message. This gives you additional control over the format of the output. |\n\n*** ** * ** ***\n\n### `log()`\n\nOutputs a blank DEBUG level message to Stackdriver Logging.\n\n*** ** * ** ***\n\n### `log(formatOrObject, values)`\n\nOutputs a DEBUG level message to Stackdriver Logging.\n\n#### Parameters\n\n| Name | Type | Description |\n|----------------------|-------------|-------------------------------------------------------------------------------------------------------------------------------------------|\n| `format``Or``Object` | `Object` | a string containing zero or more substitution strings, or a JavaScript object to be logged as a JavaScript object if no other parameters. |\n| `values` | `Object...` | objects with which to replace substitution strings within the message. This gives you additional control over the format of the output. |\n\n*** ** * ** ***\n\n### `time(label)`\n\nStarts a timer you can use to track how long an operation takes.\n\n#### Parameters\n\n| Name | Type | Description |\n|---------|----------|---------------------------------|\n| `label` | `String` | The name to give the new timer. |\n\n*** ** * ** ***\n\n### `time``End(label)`\n\nStops a timer that was previously started by calling `console.time()`. The time duration\nis logged in Stackdriver.\n\n#### Parameters\n\n| Name | Type | Description |\n|---------|----------|--------------------------------|\n| `label` | `String` | the name of the timer to stop. |\n\n*** ** * ** ***\n\n### `warn()`\n\nOutputs a blank WARNING level message to Stackdriver Logging.\n\n*** ** * ** ***\n\n### `warn(formatOrObject, values)`\n\nOutputs a WARNING level message to Stackdriver Logging.\n\n#### Parameters\n\n| Name | Type | Description |\n|----------------------|-------------|-------------------------------------------------------------------------------------------------------------------------------------------|\n| `format``Or``Object` | `Object` | a string containing zero or more substitution strings, or a JavaScript object to be logged as a JavaScript object if no other parameters. |\n| `values` | `Object...` | objects with which to replace substitution strings within the message. This gives you additional control over the format of the output. |"]]