Node.js v21.7.2 文件
- Node.js v21.7.2
-
► 目錄
- 主控台
- 類別:
主控台
new Console(stdout[, stderr][, ignoreErrors])
new Console(options)
console.assert(value[, ...message])
console.clear()
console.count([label])
console.countReset([label])
console.debug(data[, ...args])
console.dir(obj[, options])
console.dirxml(...data)
console.error([data][, ...args])
console.group([...label])
console.groupCollapsed()
console.groupEnd()
console.info([資料][, ...引數])
console.log([資料][, ...引數])
console.table(表格資料[, 屬性])
console.time([標籤])
console.timeEnd([標籤])
console.timeLog([標籤][, ...資料])
console.trace([訊息][, ...引數])
console.warn([資料][, ...引數])
- 僅限檢查器的方法
- 類別:
- 主控台
-
► 索引
- 斷言測試
- 非同步內容追蹤
- 非同步掛鉤
- 緩衝區
- C++ 外掛程式
- 使用 Node-API 的 C/C++ 外掛程式
- C++ 嵌入式 API
- 子程序
- 叢集
- 命令列選項
- 主控台
- Corepack
- 加密
- 偵錯器
- 已棄用的 API
- 診斷頻道
- DNS
- 網域
- 錯誤
- 事件
- 檔案系統
- 全域變數
- HTTP
- HTTP/2
- HTTPS
- 檢查器
- 國際化
- 模組:CommonJS 模組
- 模組:ECMAScript 模組
- 模組:
node:module
API - 模組:套件
- 網路
- 作業系統
- 路徑
- 效能掛鉤
- 權限
- 程序
- Punycode
- 查詢字串
- 讀取列
- REPL
- 報告
- 單一可執行應用程式
- 串流
- 字串解碼器
- 測試執行器
- 計時器
- TLS/SSL
- 追蹤事件
- TTY
- UDP/資料包
- URL
- 公用程式
- V8
- VM
- WASI
- Web Crypto API
- Web Streams API
- 工作執行緒
- Zlib
- ► 其他版本
- ► 選項
主控台#
原始碼: lib/console.js
node:console
模組提供了一個簡單的除錯主控台,類似於網路瀏覽器提供的 JavaScript 主控台機制。
此模組匯出兩個特定元件
- 一個
Console
類別,其中包含console.log()
、console.error()
和console.warn()
等方法,可用於寫入任何 Node.js 串流。 - 一個全域
console
實例,設定為寫入process.stdout
和process.stderr
。全域console
可在不呼叫require('node:console')
的情況下使用。
警告:全域主控台物件的方法既不像它們類似的瀏覽器 API 一樣始終同步,也不像所有其他 Node.js 串流一樣始終非同步。請參閱 關於 process I/O 的注意事項 以取得更多資訊。
使用全域 console
的範例
console.log('hello world');
// Prints: hello world, to stdout
console.log('hello %s', 'world');
// Prints: hello world, to stdout
console.error(new Error('Whoops, something bad happened'));
// Prints error message and stack trace to stderr:
// Error: Whoops, something bad happened
// at [eval]:5:15
// at Script.runInThisContext (node:vm:132:18)
// at Object.runInThisContext (node:vm:309:38)
// at node:internal/process/execution:77:19
// at [eval]-wrapper:6:22
// at evalScript (node:internal/process/execution:76:60)
// at node:internal/main/eval_string:23:3
const name = 'Will Robinson';
console.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to stderr
使用 Console
類別的範例
const out = getStreamSomehow();
const err = getStreamSomehow();
const myConsole = new console.Console(out, err);
myConsole.log('hello world');
// Prints: hello world, to out
myConsole.log('hello %s', 'world');
// Prints: hello world, to out
myConsole.error(new Error('Whoops, something bad happened'));
// Prints: [Error: Whoops, something bad happened], to err
const name = 'Will Robinson';
myConsole.warn(`Danger ${name}! Danger!`);
// Prints: Danger Will Robinson! Danger!, to err
類別:Console
#
Console
類別可用於建立一個具有可設定輸出串流的簡單記錄器,並且可以使用 require('node:console').Console
或 console.Console
(或其解構對應項)存取
const { Console } = require('node:console');
const { Console } = console;
new Console(stdout[, stderr][, ignoreErrors])
#
new Console(options)
#
options
<物件>stdout
<stream.Writable>stderr
<stream.Writable>ignoreErrors
<布林> 寫入基礎串流時忽略錯誤。預設:true
。colorMode
<布林> | <字串> 設定此Console
執行個體的色彩支援。設定為true
會在檢查值時啟用色彩。設定為false
會在檢查值時停用色彩。設定為'auto'
會使色彩支援依賴於isTTY
屬性的值和各自串流上getColorDepth()
傳回的值。如果同時設定了inspectOptions.colors
,則無法使用此選項。預設:'auto'
。inspectOptions
<Object> 指定傳遞給util.inspect()
的選項。groupIndentation
<number> 設定群組縮排。預設值:2
。
使用一個或兩個可寫入串流實例建立新的 Console
。stdout
是可寫入串流,用於列印記錄或資訊輸出。stderr
用於警告或錯誤輸出。如果未提供 stderr
,則 stdout
會用於 stderr
。
const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
// Custom simple logger
const logger = new Console({ stdout: output, stderr: errorOutput });
// use it like console
const count = 5;
logger.log('count: %d', count);
// In stdout.log: count 5
全域 console
是特殊 Console
,其輸出會傳送至 process.stdout
和 process.stderr
。它等於呼叫
new Console({ stdout: process.stdout, stderr: process.stderr });
console.assert(value[, ...message])
#
如果 value
為 假值 或省略,console.assert()
會寫入訊息。它只會寫入訊息,不會影響執行。輸出總是從 "Assertion failed"
開始。如果提供 message
,會使用 util.format()
格式化它。
如果 value
為 真值,則不會發生任何事。
console.assert(true, 'does nothing');
console.assert(false, 'Whoops %s work', 'didn\'t');
// Assertion failed: Whoops didn't work
console.assert();
// Assertion failed
console.clear()
#
當 stdout
為 TTY 時,呼叫 console.clear()
將嘗試清除 TTY。當 stdout
不是 TTY 時,此方法不會執行任何動作。
console.clear()
的特定操作會因作業系統和終端機類型而異。對於大多數 Linux 作業系統,console.clear()
的操作類似於 clear
shell 指令。在 Windows 上,console.clear()
將只清除 Node.js 二進位檔的目前終端機視窗中的輸出。
console.count([label])
#
label
<字串> 計數器的顯示標籤。預設值:'default'
。
維護特定於 label
的內部計數器,並將 console.count()
已使用指定 label
呼叫的次數輸出至 stdout
。
> console.count()
default: 1
undefined
> console.count('default')
default: 2
undefined
> console.count('abc')
abc: 1
undefined
> console.count('xyz')
xyz: 1
undefined
> console.count('abc')
abc: 2
undefined
> console.count()
default: 3
undefined
>
console.countReset([label])
#
label
<字串> 計數器的顯示標籤。預設值:'default'
。
重設特定於 label
的內部計數器。
> console.count('abc');
abc: 1
undefined
> console.countReset('abc');
undefined
> console.count('abc');
abc: 1
undefined
>
console.debug(data[, ...args])
#
console.debug()
函式為 console.log()
的別名。
console.dir(obj[, options])
#
obj
<任何>options
<物件>showHidden
<布林> 如果為true
,則也會顯示物件的不可列舉和符號屬性。預設值:false
。depth
<number> 告訴util.inspect()
在格式化物件時遞迴的次數。這對於檢查大型複雜物件很有用。若要讓它無限遞迴,請傳遞null
。預設:2
。colors
<boolean> 如果為true
,則輸出將以 ANSI 色碼樣式化。顏色可自訂;請參閱 自訂util.inspect()
顏色。預設:false
。
對 obj
使用 util.inspect()
,並將結果字串列印到 stdout
。此函式會略過在 obj
上定義的任何自訂 inspect()
函式。
console.dirxml(...data)
#
...data
<any>
此方法會呼叫 console.log()
,傳遞接收到的引數。此方法不會產生任何 XML 格式化。
console.error([data][, ...args])
#
列印到 stderr
,並換行。可以傳遞多個引數,第一個引數用作主要訊息,所有其他引數都用作替換值,類似於 printf(3)
(所有引數都傳遞給 util.format()
)。
const code = 5;
console.error('error #%d', code);
// Prints: error #5, to stderr
console.error('error', code);
// Prints: error 5, to stderr
如果在第一個字串中找不到格式化元素(例如 %d
),則會對每個參數呼叫 util.inspect()
,並串接產生的字串值。有關更多資訊,請參閱 util.format()
。
console.group([...標籤])
#
...標籤
<any>
將後續行縮排增加 groupIndentation
長度的空格。
如果提供一個或多個 標籤
,這些標籤會先列印,而不會額外縮排。
console.groupCollapsed()
#
console.group()
的別名。
console.groupEnd()
#
將後續行縮排減少 groupIndentation
長度的空格。
console.info([資料][, ...引數])
#
console.info()
函式是 console.log()
的別名。
console.log([資料][, ...引數])
#
列印到 stdout
並換行。可以傳遞多個引數,第一個引數用作主要訊息,所有其他引數都用作替換值,類似於 printf(3)
(所有引數都傳遞給 util.format()
)。
const count = 5;
console.log('count: %d', count);
// Prints: count: 5, to stdout
console.log('count:', count);
// Prints: count: 5, to stdout
有關更多資訊,請參閱 util.format()
。
console.table(tabularData[, properties])
#
tabularData
<any>properties
<string[]> 建立表格的備用屬性。
嘗試使用 tabularData
屬性的欄位(或使用 properties
)和 tabularData
的列來建立一個表格,並將其記錄下來。如果無法將其解析為表格,則會退回僅記錄引數。
// These can't be parsed as tabular data
console.table(Symbol());
// Symbol()
console.table(undefined);
// undefined
console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }]);
// ┌─────────┬─────┬─────┐
// │ (index) │ a │ b │
// ├─────────┼─────┼─────┤
// │ 0 │ 1 │ 'Y' │
// │ 1 │ 'Z' │ 2 │
// └─────────┴─────┴─────┘
console.table([{ a: 1, b: 'Y' }, { a: 'Z', b: 2 }], ['a']);
// ┌─────────┬─────┐
// │ (index) │ a │
// ├─────────┼─────┤
// │ 0 │ 1 │
// │ 1 │ 'Z' │
// └─────────┴─────┘
console.time([label])
#
label
<string> 預設值:'default'
啟動計時器,可使用此計時器來計算作業的持續時間。計時器由唯一的 label
識別。呼叫 console.timeEnd()
時使用相同的 label
來停止計時器,並將經過時間以適當的時間單位輸出至 stdout
。例如,如果經過時間為 3869ms,console.timeEnd()
會顯示「3.869s」。
console.timeEnd([label])
#
label
<string> 預設值:'default'
停止先前呼叫 console.time()
啟動的計時器,並將結果列印至 stdout
console.time('bunch-of-stuff');
// Do a bunch of stuff.
console.timeEnd('bunch-of-stuff');
// Prints: bunch-of-stuff: 225.438ms
console.timeLog([label][, ...data])
#
對於先前呼叫 console.time()
啟動的計時器,將經過時間和其他 data
引數列印至 stdout
console.time('process');
const value = expensiveProcess1(); // Returns 42
console.timeLog('process', value);
// Prints "process: 365.227ms 42".
doExpensiveProcess2(value);
console.timeEnd('process');
console.trace([訊息][, ...引數])
#
列印字串 'Trace: '
到 stderr
,接著是 util.format()
格式化的訊息和堆疊追蹤到程式碼中的目前位置。
console.trace('Show me');
// Prints: (stack trace will vary based on where trace is called)
// Trace: Show me
// at repl:2:9
// at REPLServer.defaultEval (repl.js:248:27)
// at bound (domain.js:287:14)
// at REPLServer.runBound [as eval] (domain.js:300:12)
// at REPLServer.<anonymous> (repl.js:412:12)
// at emitOne (events.js:82:20)
// at REPLServer.emit (events.js:169:7)
// at REPLServer.Interface._onLine (readline.js:210:10)
// at REPLServer.Interface._line (readline.js:549:8)
// at REPLServer.Interface._ttyWrite (readline.js:826:14)
console.warn([資料][, ...引數])
#
console.warn()
函式是 console.error()
的別名。
僅限檢查器的方法#
下列方法由 V8 引擎公開在一般 API 中,但除非與 檢查器 (--inspect
旗標) 一起使用,否則不會顯示任何內容。
console.profile([標籤])
#
標籤
<字串>
除非在檢查器中使用,否則此方法不會顯示任何內容。console.profile()
方法會啟動一個 JavaScript CPU 設定檔,並附帶一個選用標籤,直到 console.profileEnd()
被呼叫。然後,設定檔會加入檢查器的 設定檔 面板。
console.profile('MyLabel');
// Some code
console.profileEnd('MyLabel');
// Adds the profile 'MyLabel' to the Profiles panel of the inspector.
console.profileEnd([標籤])
#
標籤
<字串>
此方法不會顯示任何內容,除非在檢查器中使用。如果已開始,則停止目前的 JavaScript CPU 分析記錄,並將報告列印至檢查器的分析記錄面板。請參閱 console.profile()
以取得範例。
如果未標籤呼叫此方法,則會停止最近開始的分析記錄。
console.timeStamp([label])
#
標籤
<字串>
此方法不會顯示任何內容,除非在檢查器中使用。console.timeStamp()
方法會將標籤為 'label'
的事件新增至檢查器的時間軸面板。