[英]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.