本文翻译自:How can I get the full object in Node.js's console.log(), rather than '[Object]'?
When debugging using console.log()
, how can I get the full object? 使用console.log()
调试时,如何获取完整的对象?
const myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
console.log(myObject);
Outputs: 输出:
{ a: 'a', b: { c: 'c', d: { e: 'e', f: [Object] } } }
But I want to also see the content of property f
. 但我也想看看属性f
的内容。
参考:https://stackoom.com/question/j1Aq/如何在Node-js的console-log-中获得完整的对象-而不是-Object
You need to use util.inspect()
: 您需要使用util.inspect()
:
const util = require('util')
console.log(util.inspect(myObject, {showHidden: false, depth: null}))
// alternative shortcut
console.log(util.inspect(myObject, false, null, true /* enable colors */))
Outputs 产出
{ a: 'a', b: { c: 'c', d: { e: 'e', f: { g: 'g', h: { i: 'i' } } } } }
See util.inspect()
docs . 请参阅util.inspect()
docs 。
You can use JSON.stringify
, and get some nice indentation as well as perhaps easier to remember syntax. 您可以使用JSON.stringify
,获得一些不错的缩进以及可能更容易记住的语法。
console.log(JSON.stringify(myObject, null, 4));
{
"a": "a",
"b": {
"c": "c",
"d": {
"e": "e",
"f": {
"g": "g",
"h": {
"i": "i"
}
}
}
}
}
The third argument sets the indentation level, so you can adjust that as desired. 第三个参数设置缩进级别,因此您可以根据需要进行调整。
More detail here if needed: 如果需要,请在此处提供更多详细信息:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
另一个简单的方法是将其转换为json
console.log('connection : %j', myObject);
perhaps console.dir
is all you need. 也许console.dir
就是您所需要的。
http://nodejs.org/api/console.html#console_console_dir_obj http://nodejs.org/api/console.html#console_console_dir_obj
Uses util.inspect on obj and prints resulting string to stdout. 在obj上使用util.inspect并将结果字符串输出到stdout。
use util option if you need more control. 如果需要更多控制,请使用util选项。
A compilation of the many useful answers from (at least) Node.js v0.10.33
(stable) / v0.11.14
(unstable) presumably through (at least) v7.7.4
(the version current as of the latest update to this answer). 至少(至少)通过v7.7.4
(至少是最新版本)的(至少)Node.js v0.10.33
(稳定)/ v0.11.14
(不稳定)的许多有用答案的汇编。
tl;dr tl; dr
util.inspect()
is at the heart of diagnostic output: console.log()
and console.dir()
as well as the Node.js REPL use util.inspect()
implicitly , so it's generally not necessary to require('util')
and call util.inspect()
directly . util.inspect()
是在诊断输出的心脏: console.log()
和console.dir()
以及Node.js的REPL使用util.inspect()
暗示 ,因此一般没有必要 require('util')
然后直接调用util.inspect()
。
To get the desired output for the example in the question: 为问题中的示例获取所需的输出:
console.dir(myObject, { depth: null }); // `depth: null` ensures unlimited recursion
Details below. 详细信息如下。
console.log()
(and its alias, console.info()
): console.log()
(及其别名console.info()
):
util.inspect()
is automatically applied to every argument: 如果第一个参数不是格式字符串 : 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()
in this case, which implies 2 notable limitations: 请注意,在这种情况下,您无法通过util.inspect()
传递选项 ,这意味着两个明显的限制:
console.log()
, you must instead use console.dir()
: console.dir(myObject, { depth: null }
prints with unlimited depth ; see below. 由于无法使用console.log()
更改此设置,因此必须改为使用console.dir()
: console.dir(myObject, { depth: null }
打印深度不受限制 ;请参见下文。 util.format()
to print the remaining arguments based on the format string (see below); 如果第一个参数是格式字符串 (请参见下文):使用util.format()
根据格式字符串打印其余参数(请参见下文); eg: 例如:
o = { one: 1, two: 'deux', foo: function(){} }; console.log('o as JSON: %j', o) // -> 'o as JSON: {"one":1,"two":"deux"}'
util.inspect()
-style. 没有代表对象 util.inspect()
-style的占位符。 %j
is NOT pretty-printed. 用%j
生成的JSON印刷得不好。 console.dir()
: console.dir()
:
util.inspect()
- essentially, a wrapper for util.inspect()
without options by default; 仅接受1个参数来检查 ,并始终应用util.inspect()
-本质上,默认情况下为util.inspect()
的包装,不带选项; eg: 例如:
o = { one: 1, two: 'deux', foo: function(){} }; console.dir(o); // Effectively the same as console.log(o) in this case.
util.inspect()
- see below; node.js v0.11.14 + :可选的第二个参数指定util.inspect()
选项-参见下文; eg: 例如:
console.dir({ one: 1, two: 'deux'}, { colors: true }); // node 0.11+: Prints object representation with syntax coloring.
util.inspect()
with syntax coloring ; REPL : 使用带有语法颜色的util.inspect()
隐式打印任何表达式的返回值 ; o = { one: 1, two: 'deux', foo: function(){} } // echoes the object definition with syntax coloring.
util.inspect()
automatically (and invariably) pretty-prints object and array representations , but produces multiline output only when needed - if everything fits on one line, only 1 line is printed. util.inspect()
自动(并总是)漂亮地打印对象和数组表示形式 ,但仅在需要时才产生多行输出 -如果所有内容都适合一行,则仅打印一行。
By default, output is wrapped at around 60 characters thanks, Shrey , regardless of whether the output is sent to a file or a terminal. 默认情况下,无论输出是发送到文件还是终端,输出都会用大约60个字符包装, 感谢Shrey 。 In practice, since line breaks only happen at property boundaries , you will often end up with shorter lines, but they can also be longer (eg, with long property values). 实际上,由于换行符仅发生在属性边界处 ,所以您通常会得到较短的行,但它们也可能更长(例如,具有较长的属性值)。
In v6.3.0+ you can use the breakLength
option to override the 60-character limit; 在v6.3.0 +中,您可以使用breakLength
选项覆盖60个字符的限制; if you set it to Infinity
, everything is output on a single line. 如果你将其设置为Infinity
,一切都在一个线路输出。
If you want more control over pretty-printing, consider using JSON.stringify()
with a 3rd argument , but note the following: 如果要对JSON.stringify()
打印进行更多控制,请考虑将JSON.stringify()
与第三个参数一起使用 ,但请注意以下几点:
module
in the global context. 带有循环引用的对象失败 ,例如全局上下文中的module
。 JSON.stringify({ one: 1, two: 'deux', three: true}, undefined, 2); // creates a pretty-printed multiline JSON representation indented with 2 spaces
util.inspect()
options object (2nd argument): util.inspect()
选项对象 (第二个参数):
source: http://nodejs.org/api/util.html#util_util_format_format 来源: http : //nodejs.org/api/util.html#util_util_format_format
An optional options object may be passed that alters certain aspects of the formatted string: 可以传递一个可选的options对象,以更改格式化字符串的某些方面:
showHidden
true
, then the object's non-enumerable properties [those designated not to show up when you use for keys in obj
or Object.keys(obj)
] will be shown too. 如果为true
,则还将显示对象的不可枚举的属性[当for keys in obj
或Object.keys(obj)
for keys in obj
时指定为不显示的属性]。 Defaults to false
. 默认为false
。 depth
null
. 默认值为2。要使其无限期递归,请传递null
。 colors
false
. 默认为false
。 Colors are customizable [... - see link]. 颜色是可自定义的[...-参见链接]。 customInspect
false
, then custom inspect()
functions defined on the objects being inspected won't be called. 如果为false
,则不会调用在被检查对象上定义的自定义inspect()
函数。 Defaults to true
. 默认为true
。 util.format()
format-string placeholders (1st argument) util.format()
格式字符串占位符 (第一个参数)
source: http://nodejs.org/api/util.html#util_util_format_format 来源: http : //nodejs.org/api/util.html#util_util_format_format
%s
- String. %s
字符串。 %d
- Number (both integer and float). %d
数字(整数和浮点数)。 %j
- JSON. %j
-JSON。 %
- single percent sign ('%'). %
-单个百分号('%')。 This does not consume an argument. 这不消耗参数。