[英]How can I get the full object in Node.js's console.log(), rather than '[Object]'?
我有這個 object:
const myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
但是當我嘗試使用console.log(myObject)
顯示它時,我收到了這個 output:
{ a: 'a', b: { c: 'c', d: { e: 'e', f: [Object] } } }
如何獲得完整的 object,包括屬性f
的內容?
您需要使用util.inspect()
:
const util = require('util')
console.log(util.inspect(myObject, {showHidden: false, depth: null, colors: true}))
// alternative shortcut
console.log(util.inspect(myObject, false, null, true /* enable colors */))
輸出
{ a: 'a', b: { c: 'c', d: { e: 'e', f: { g: 'g', h: { i: 'i' } } } } }
您可以使用JSON.stringify
,並獲得一些不錯的縮進,並且可能更容易記住語法。
console.log(JSON.stringify(myObject, null, 4));
{
"a": "a",
"b": {
"c": "c",
"d": {
"e": "e",
"f": {
"g": "g",
"h": {
"i": "i"
}
}
}
}
}
第三個參數設置縮進級別,因此您可以根據需要進行調整。
如果需要,請參閱 JSON stringify MDN 文檔中的更多詳細信息。
來自(至少)Node.js v0.10.33
(穩定) v0.11.14
(不穩定)的許多有用答案的匯編,大概到(至少) v7.7.4
(截至此答案的最新更新的最新版本) . 向Rory O'Kane 致敬,感謝他的幫助。
tl;博士
要獲得問題中示例的所需輸出,請使用console.dir()
:
console.dir(myObject, { depth: null }); // `depth: null` ensures unlimited recursion
為什么不util.inspect()
? 因為它已經是診斷輸出的核心: console.log()
和console.dir()
以及 Node.js REPL隱式使用util.inspect()
。 通常不需要require('util')
並直接調用util.inspect()
。
詳情如下。
console.log()
(及其別名console.info()
):
util.inspect()
會自動應用於每個參數:
o = { one: 1, two: 'deux', foo: function(){} }; console.log(o, [1,2,3]) // -> '{ one: 1, two: 'deux', foo: [Function] } [ 1, 2, 3 ]'
util.inspect()
傳遞選項,這意味着 2 個顯着限制:
console.log()
更改此設置,因此您必須改用console.dir()
: console.dir(myObject, { depth: null }
以無限深度打印;見下文。util.format()
根據格式字符串打印剩余的參數(見下文); 例如:
o = { one: 1, two: 'deux', foo: function(){} }; console.log('o as JSON: %j', o) // -> 'o as JSON: {"one":1,"two":"deux"}'
util.inspect()
樣式的占位符。%j
生成的 JSON 打印不漂亮。util.inspect()
- 本質上是util.inspect()
的包裝器,默認情況下沒有選項; 例如:
o = { one: 1, two: 'deux', foo: function(){} }; console.dir(o); // Effectively the same as console.log(o) in this case.
util.inspect()
的選項——見下文; 例如:
console.dir({ one: 1, two: 'deux'}, { colors: true }); // Node 0.11+: Prints object representation with syntax coloring.
util.inspect()
隱式打印任何表達式的返回值;o = { one: 1, two: 'deux', foo: function(){} } // The REPL echoes the object definition with syntax coloring.
util.inspect()
自動漂亮地打印對象和數組表示,但僅在需要時產生多行輸出。
漂亮的打印行為可以通過可選options
參數中的compact
屬性來控制; false
無條件使用多行輸出,而true
完全禁用漂亮打印; 它也可以設置為一個數字(默認值為3
)來控制有條件的多行行為 - 請參閱文檔。
默認情況下,無論輸出是發送到文件還是終端,輸出都包含大約 60 個字符,謝謝, Shrey 。 實際上,由於換行只發生在屬性邊界處,因此您通常會得到較短的行,但它們也可以更長(例如,具有較長的屬性值)。
在 v6.3.0+ 中,您可以使用breakLength
選項覆蓋 60 個字符的限制; 如果將其設置為Infinity
,則所有內容都將在一行上輸出。
如果您想更好地控制漂亮打印,請考慮將JSON.stringify()
與第三個參數一起使用,但請注意以下幾點:
module
。JSON.stringify({ one: 1, two: 'deux', three: true}, undefined, 2); // creates a pretty-printed multiline JSON representation indented with 2 spaces
util.inspect()
選項對象(第二個參數):
可以傳遞一個可選的選項對象來改變格式化字符串的某些方面; 支持的一些屬性是:
有關當前完整列表,請參閱最新的 Node.js 文檔。
showHidden
true
,則對象的不可枚舉屬性 [那些在您使用 obj 或Object.keys(obj)
for keys in obj
時指定不顯示的屬性] 也將顯示。 默認為false
。 depth
null
。 colors
false
。 顏色是可定制的 [… – 見鏈接]。 customInspect
false
,則不會調用在被檢查對象上定義的自定義inspect()
函數。 默認為true
。 util.format()
格式字符串占位符(第一個參數)
一些受支持的占位符是:
有關當前完整列表,請參閱 最新的 Node.js 文檔。
%s
字符串。%d
– 數字(整數和浮點數)。%j
– JSON。%%
– 單個百分號 ('%')。 這不會消耗一個論點。另一個簡單的方法是將其轉換為 json
console.log('connection : %j', myObject);
嘗試這個:
console.dir(myObject,{depth:null})
從 Node.js 6.4.0 開始,這可以通過util.inspect.defaultOptions
優雅地解決:
require("util").inspect.defaultOptions.depth = null;
console.log(myObject);
也許console.dir
就是你所需要的。
http://nodejs.org/api/console.html#console_console_dir_obj
在 obj 上使用 util.inspect 並將結果字符串打印到標准輸出。
如果您需要更多控制,請使用 util 選項。
這兩種用法都可以應用:
// more compact, and colour can be applied (better for process managers logging)
console.dir(queryArgs, { depth: null, colors: true });
// get a clear list of actual values
console.log(JSON.stringify(queryArgs, undefined, 2));
你也可以做
console.log(JSON.stringify(myObject, null, 3));
我認為這可能對你有用。
const myObject = { "a":"a", "b":{ "c":"c", "d":{ "e":"e", "f":{ "g":"g", "h":{ "i":"i" } } } } }; console.log(JSON.stringify(myObject, null, '\t'));
如本答案所述:
JSON.stringify
的第三個參數定義了用於漂亮打印的空白插入。 它可以是字符串或數字(空格數)。
您可以簡單地向您的對象添加一個inspect()
方法,該方法將覆蓋對象在console.log
消息中的表示
例如:
var myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
myObject.inspect = function(){ return JSON.stringify( this, null, ' ' ); }
然后,您的對象將根據需要在 console.log 和節點 shell 中表示
更新:
object.inspect 已被棄用( https://github.com/nodejs/node/issues/15549 )。 使用 myObject[util.inspect.custom] 代替:
const util = require('util')
var myObject = {
/* nested properties not shown */
}
myObject[util.inspect.custom] = function(){ return JSON.stringify( this, null, 4 ); }
console.log(util.inspect(myObject))
let myVar = {a: {b: {c: 1}}};
console.log(JSON.stringify( myVar, null, 4 ))
非常適合深入檢查數據對象。 這種方法適用於嵌套數組和帶有數組的嵌套對象。
一個簡單的技巧是在運行腳本時使用debug
模塊添加DEBUG_DEPTH=null
作為環境變量
前任。
DEBUG=* DEBUG_DEPTH=null 節點 index.js
在你的代碼中
const debug = require('debug');
debug("%O", myObject);
不要試圖重新發明輪子
util.inspect()
、 JSON.stringify()
和console.dir()
是在瀏覽器控制台中播放時記錄對象的有用工具。
如果您對 Node.js 開發很認真,那么您絕對應該使用記錄器。 使用它,您可以添加調試和監控應用程序所需的所有日志。 然后只需更改記錄器的日志記錄級別以僅保持生產日志可見。
此外,他們已經解決了與日志記錄相關的所有煩人問題,例如:循環對象、格式、日志級別、多輸出和性能。
pino
是用於 Node.js 的快速且現代的記錄器,它具有合理的默認值來處理循環對象/引用,例如depthLimit
和edgeLimit
。 它支持子記錄器、傳輸器和漂亮的打印輸出。
此外,它有 8 個默認日志級別,您可以使用customLevels
選項自定義:
fatal
error
warn
info
debug
trace
silent
npm install pino
const logger = require('pino')()
logger.info('hello world')
const logger = pino({
depthLimit: 10,
edgeLimit: 200,
customLevels: {
foo: 35
}
});
logger.foo('hi')
節點 REPL 具有用於覆蓋對象顯示方式的內置解決方案,請參見此處。
REPL 模塊在打印值時在內部使用
util.inspect()
。 但是,util.inspect
將調用委托給對象的inspect()
函數(如果有的話)。
最簡單的選擇:
console.log('%O', myObject);
如果您正在尋找一種方法來顯示數組中的隱藏項,則必須通過maxArrayLength: Infinity
console.log(util.inspect(value, { maxArrayLength: Infinity }));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.