790 javascript debugging node.js console.log
使用调试时console.log(),如何获取完整对象?
const myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
console.log(myObject);
Run Code Online (Sandbox Code Playgroud)
输出:
{ a: 'a', b: { c: 'c', d: { e: 'e', f: [Object] } } }
Run Code Online (Sandbox Code Playgroud)
但我也希望看到财产的内容f.
250*_*50R 1319
你需要使用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 */))
Run Code Online (Sandbox Code Playgroud)
输出
{ a: 'a', b: { c: 'c', d: { e: 'e', f: { g: 'g', h: { i: 'i' } } } } }
Run Code Online (Sandbox Code Playgroud)
小智 583
你可以使用JSON.stringify,并获得一些很好的缩进,也许更容易记住语法.
console.log(JSON.stringify(myObject, null, 4));
Run Code Online (Sandbox Code Playgroud)
{
"a": "a",
"b": {
"c": "c",
"d": {
"e": "e",
"f": {
"g": "g",
"h": {
"i": "i"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
第三个参数设置缩进级别,因此您可以根据需要进行调整.
如果需要,请在此处提
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
mkl*_*nt0 268
来自(至少)Node.js v0.10.33(稳定)/ v0.11.14(不稳定)的许多有用答案的汇编,大概是通过(至少)v7.7.4(该答案的最新更新的版本当前).
TL;博士
util.inspect()在诊断输出的心脏:console.log()和console.dir()以及Node.js的REPL使用util.inspect() 隐式,因此一般没有必要 require('util')和调用util.inspect()直接.
要获得问题中示例的所需输出:
console.dir(myObject, { depth: null }); // `depth: null` ensures unlimited recursion
Run Code Online (Sandbox Code Playgroud)
详情如下.
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不是很漂亮的.console.dir():
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(){} } // echoes the object definition with syntax coloring.util.inspect()自动(并且总是)漂亮地打印对象和数组表示,但仅在需要时生成多行输出 - 如果所有内容都适合一行,则只打印一行.
Shrey ,无论输出是发送到文件还是终端,默认情况下输出都会包含大约60个字符.实际上,由于换行仅发生在属性边界,因此通常会以较短的行结束,但它们也可能更长(例如,具有较长的属性值).
在v6.3.0 +中,您可以使用该breakLength选项覆盖60个字符的限制; 如果你将它设置为Infinity,一切都在输出单一线.
如果您想要更好地控制漂亮打印,请考虑使用JSON.stringify()第3个参数,但请注意以下事项:
module在全局上下文中.JSON.stringify({ one: 1, two: 'deux', three: true}, undefined, 2); // creates a pretty-printed multiline JSON representation indented with 2 spacesutil.inspect()选项对象(第二个参数):
来源:http://nodejs.org/api/util.html#util_util_format_format
可以传递一个可选的选项对象,它可以改变格式化字符串的某些方面:
showHidden
true,那么对象的非枚举的属性[指定那些不显示当您使用for keys in obj或者Object.keys(obj)]也将被显示.默认为false.depth
null.colors
false.颜色可定制[... - 见链接].customInspect
false,则inspect()不会调用在被检查对象上定义的自定义函数.默认为true.util.format()format-string占位符(第一个参数)
来源:http://nodejs.org/api/util.html#util_util_format_format
%s - 字符串.%d - 数字(整数和浮点数).%j - JSON.% - 单个百分号('%').这不会消耗参数.nik*_*mac 52
另一个简单的方法是将其转换为json
console.log('connection : %j', myObject);
Run Code Online (Sandbox Code Playgroud)
sil*_*ind 27
从Node.js 6.4.0开始,可以通过以下方式优雅地解决这个问题util.inspect.defaultOptions:
require("util").inspect.defaultOptions.depth = null;
console.log(myObject);
Run Code Online (Sandbox Code Playgroud)
Luk*_*e W 23
也许console.dir就是你所需要的一切.
http://nodejs.org/api/console.html#console_console_dir_obj
在obj上使用util.inspect并将结果字符串输出到stdout.
如果需要更多控制,请使用util选项.
Ees*_*esa 18
你也可以
console.log(JSON.stringify(myObject, null, 3));
Run Code Online (Sandbox Code Playgroud)
Ali*_*Ali 14
检查对象的一种好方法是在Chrome DevTools for Node中使用node --inspect选项.
node.exe --inspect www.js
Run Code Online (Sandbox Code Playgroud)
chrome://inspect/#devices在chrome中打开,然后单击打开专用的DevTools for Node
现在每个记录的对象都可以在检查器中使用,例如在chrome中运行的常规JS.
无需重新打开检查器,它会在节点启动或重新启动后自动连接到节点.无论--inspect并为节点的Chrome DevTools可能无法在旧版本的节点和Chrome浏览器使用.
Erc*_*rce 10
这两种用法都可以应用
// more compact and colour can be applied (better for process managers logging)
console.dir(queryArgs, { depth: null, colors: true });
// clear list of actual values
console.log(JSON.stringify(queryArgs, undefined, 2));
Run Code Online (Sandbox Code Playgroud)
不要尝试重新发明轮子
util.inspect(),JSON.stringify()并且console.dir()是在浏览器控制台中播放时记录对象的有用工具。
如果您认真从事 Node.js 开发,那么您绝对应该使用记录器。使用它,您可以添加调试和监视应用程序所需的所有日志。然后只需更改记录器的日志记录级别即可仅保留生产日志可见。
此外,他们已经解决了与日志记录相关的所有烦人的问题,例如:循环对象、格式、日志级别、多个输出和 性能。
pino是一个快速且现代的 Node.js 记录器,具有合理的默认值来处理循环对象/引用,例如depthLimit和edgeLimit。它支持子记录器、传输和漂亮的打印输出。
此外,它有 8 个默认日志记录级别,您可以使用以下customLevels选项进行自定义:
fatalerrorwarninfodebugtracesilentnpm install pino
Run Code Online (Sandbox Code Playgroud)
const logger = require('pino')()
logger.info('hello world')
Run Code Online (Sandbox Code Playgroud)
const logger = pino({
depthLimit: 10,
edgeLimit: 200,
customLevels: {
foo: 35
}
});
logger.foo('hi')
Run Code Online (Sandbox Code Playgroud)
let myVar = {a: {b: {c: 1}}};
console.log(JSON.stringify( myVar, null, 4 ))
Run Code Online (Sandbox Code Playgroud)
非常适合深度检查数据对象。此方法适用于嵌套数组和带有数组的嵌套对象。
小智 7
我认为这可能对您有用。
const myObject = {
"a":"a",
"b":{
"c":"c",
"d":{
"e":"e",
"f":{
"g":"g",
"h":{
"i":"i"
}
}
}
}
};
console.log(JSON.stringify(myObject, null, '\t'));Run Code Online (Sandbox Code Playgroud)
就像在这个答案中提到的:
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, ' ' ); }
Run Code Online (Sandbox Code Playgroud)
然后,您的对象将在 console.log 和 node 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))
Run Code Online (Sandbox Code Playgroud)
如果您正在寻找一种方法来显示数组中的隐藏项目,那么您必须通过maxArrayLength: Infinity
console.log(util.inspect(value, { maxArrayLength: Infinity }));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
422156 次 |
| 最近记录: |