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的内容。
14 个解决方案
#1
878
You need to use util.inspect()
:
你需要使用util.inspect():
const util = require('util')console.log(util.inspect(myObject, {showHidden: false, depth: null}))// alternative shortcutconsole.log(util.inspect(myObject, false, null))
Outputs
输出
{ a: 'a', b: { c: 'c', d: { e: 'e', f: { g: 'g', h: { i: 'i' } } } } }
See util.inspect()
docs.
看到util.inspect()文档。
#2
420
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/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
#3
130
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).
来自(至少)节点的许多有用答案的汇编。js v0.10.33(稳定)/ v0.11.14(不稳定)可能通过(至少)v7.7.4(此答案的最新更新的当前版本)。
tl;dr
博士tl;
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.
inspect()是诊断输出的核心:console.log()和console.dir()以及节点。js REPL隐式地使用util.inspect(),因此通常不需要直接要求('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()
):log()(及其别名console.info():
-
If the 1st argument is NOT a format string:
util.inspect()
is automatically applied to every argument:o = { one: 1, two: 'deux', foo: function(){} }; console.log(o, [1,2,3]) // -> '{ one: 1, two: 'deux', foo: [Function] } [ 1, 2, 3 ]'
- o = {1, 2: 'deux', foo: function(){};控制台。日志(o,[1,2,3])/ / - >“{:1,二:“两个”,foo:[功能]}[1,2,3]”
- Note that you cannot pass options through
util.inspect()
in this case, which implies 2 notable limitations:- Structural depth of the output is limited to 2 levels (the default).
- Since you cannot change this with
console.log()
, you must instead useconsole.dir()
:console.dir(myObject, { depth: null }
prints with unlimited depth; see below. - 由于不能使用console.log()更改这一点,所以必须使用console.dir(): console。dir(myObject, {depth: null}打印,深度无限;见下文。
- Since you cannot change this with
- 输出的结构深度被限制为2级(默认)。由于不能使用console.log()更改这一点,所以必须使用console.dir(): console。dir(myObject, {depth: null}打印,深度无限;见下文。
- You can't turn syntax coloring on.
- 不能打开语法着色。
- Structural depth of the output is limited to 2 levels (the default).
- 注意,在这种情况下,不能通过util.inspect()传递选项,这意味着两个明显的限制:输出的结构深度限制为两个级别(默认值)。由于不能使用console.log()更改这一点,所以必须使用console.dir(): console。dir(myObject, {depth: null}打印,深度无限;见下文。不能打开语法着色。
- 如果第一个参数不是格式字符串:util.inspect()将自动应用到每个参数:o = {1: 1, 2: 'deux', foo: function(){};控制台。log(o, [1,2,3]) // -> '{1,2: 'deux', foo: [Function]}[1,2,3] '注意,在这个例子中,你不能通过util.inspect()来传递选项,这意味着2个显著的限制:输出的结构深度限制在2级(默认值)。由于不能使用console.log()更改这一点,所以必须使用console.dir(): console。dir(myObject, {depth: null}打印,深度无限;见下文。不能打开语法着色。
-
If the 1st argument IS a format string (see below): uses
util.format()
to print the remaining arguments based on the format string (see below); e.g.:o = { one: 1, two: 'deux', foo: function(){} }; console.log('o as JSON: %j', o) // -> 'o as JSON: {"one":1,"two":"deux"}'
- o = {1, 2: 'deux', foo: function(){};控制台。日志(o为JSON:% j,o)/ / - >“o为JSON:{“一”:1、“两个”:“两个”}”
- Note:
- There is NO placeholder for representing objects
util.inspect()
-style. - 没有用于表示对象的占位符。
- JSON generated with
%j
is NOT pretty-printed. - 使用%j生成的JSON不是漂亮打印的。
- There is NO placeholder for representing objects
- 注意:没有用于表示util.inspect()样式的对象的占位符。使用%j生成的JSON不是漂亮打印的。
- 如果第一个参数是一个格式字符串(参见下面):使用util.format()来打印基于格式字符串的剩余参数(见下面);例如:o = {:1,二:“两个”,foo:函数(){ } };控制台。日志('o as JSON: %j', o) // -> 'o as JSON: {" 1 ":1," 2 ":"deux"}'注意:没有占位符来表示util.inspect()样式的对象。使用%j生成的JSON不是漂亮打印的。
-
If the 1st argument is NOT a format string:
-
console.dir()
:console.dir():
-
Accepts only 1 argument to inspect, and always applies
util.inspect()
- essentially, a wrapper forutil.inspect()
without options by default; e.g.:o = { one: 1, two: 'deux', foo: function(){} }; console.dir(o); // Effectively the same as console.log(o) in this case.
- o = {1, 2: 'deux', foo: function(){};console.dir(o);//在这种情况下与console.log(o)一样有效。
- 只接受一个参数进行检查,并且始终应用util.inspect()——本质上,是util.inspect()的包装器,默认情况下没有选项;例如:o = {:1,二:“两个”,foo:函数(){ } };console.dir(o);//在这种情况下与console.log(o)一样有效。
-
node.js v0.11.14+: The optional 2nd argument specifies options for
util.inspect()
- see below; e.g.:console.dir({ one: 1, two: 'deux'}, { colors: true }); // node 0.11+: Prints object representation with syntax coloring.
- 控制台。dir({1: 1, 2: 'deux'}, {color: true});// node 0.11+:打印带有语法着色的对象表示。
- 节点。js v0.11.14+:可选的第2个参数指定util.inspect()的选项—见下面;例如:控制台。dir({1: 1, 2: 'deux'}, {color: true});// node 0.11+:打印带有语法着色的对象表示。
-
Accepts only 1 argument to inspect, and always applies
-
The REPL: implicitly prints any expression's return value with
util.inspect()
with syntax coloring;
i.e., just typing a variable's name and hitting Enter will print an inspected version of its value; e.g.:o = { one: 1, two: 'deux', foo: function(){} } // echoes the object definition with syntax coloring.
- o = {1: 1, 2: 'deux', foo: function(){} //用语法着色来回应对象定义。
- REPL:使用util.inspect()使用语法着色隐式地打印任何表达式的返回值;即。,只需输入变量的名称并按Enter键,就会打印出其值的检查版本;例如:o = {1: 1, 2: 'deux', foo: function(){} // /用语法着色来回应对象定义。
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. In practice, since line breaks only happen at property boundaries, you will often end up with shorter lines, but they can also be longer (e.g., with long property values).
默认情况下,无论输出是发送到文件还是终端,输出都包装在大约60个字符内。在实践中,由于换行只发生在属性边界处,您通常会得到更短的行,但是它们也可以更长(例如,具有长属性值)。
-
In v6.3.0+ you can use the
breakLength
option to override the 60-character limit; if you set it toInfinity
, everything is output on a single line.在v6.3.0+中,您可以使用breakLength选项来覆盖60个字符的限制;如果你把它设为无穷大,所有的东西都输出在一条直线上。
If you want more control over pretty-printing, consider using JSON.stringify()
with a 3rd argument, but note the following:
如果您想要对漂亮打印进行更多的控制,可以考虑使用JSON.stringify()作为第三个参数,但是请注意以下内容:
-
Fails with objects that have circular references, such as
module
in the global context. - 对具有循环引用的对象失败,例如全局上下文中的模块。
- Methods (functions) will by design NOT be included.
- 方法(功能)在设计上不包括在内。
- You can't opt into showing hidden (non-enumerable) properties.
- 您不能选择显示隐藏的(非可枚举的)属性。
- Example call:
JSON.stringify({ one: 1, two: 'deux', three: true}, undefined, 2); // creates a pretty-printed multiline JSON representation indented with 2 spaces
- JSON。stringify({1: 1, 2: 'deux', 3: true}, undefined, 2);//创建一个漂亮打印的多行JSON表示,缩进两个空格
- 示例调用:JSON。stringify({1: 1, 2: 'deux', 3: true}, undefined, 2);//创建一个漂亮打印的多行JSON表示,缩进两个空格
util.inspect()
options object (2nd argument):
视察()选项对象(第二个参数):
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:
可以传递一个可选选项对象来改变格式化字符串的某些方面:
-
showHidden
- if
true
, then the object's non-enumerable properties [those designated not to show up when you usefor keys in obj
orObject.keys(obj)
] will be shown too. Defaults tofalse
. - 如果为真,那么对象的非可枚举属性[当您在obj或object .keys中使用键时指定不显示的属性]也将显示出来。默认值为false。
- if
- 如果为真,那么对象的非可枚举属性(当您在obj或object .keys(obj)中使用键时指定不显示的属性)也将显示出来。默认值为false。
-
depth
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
null
. - 告诉检查在格式化对象时要递归多少次。这对于检查大型复杂对象非常有用。默认为2。要使它无限递归,传递null。
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
- 深度告诉检查在格式化对象时要递归多少次。这对于检查大型复杂对象非常有用。默认为2。要使它无限递归,传递null。
-
colors
- if true, then the output will be styled with ANSI color codes. Defaults to
false
. Colors are customizable [... - see link]. - 如果为真,则输出将使用ANSI颜色代码进行样式化。默认值为false。颜色可定制的[…(见链接)。
- if true, then the output will be styled with ANSI color codes. Defaults to
- 如果颜色为真,则输出将使用ANSI颜色代码进行样式化。默认值为false。颜色可定制的[…(见链接)。
-
customInspect
- if
false
, then custominspect()
functions defined on the objects being inspected won't be called. Defaults totrue
. - 如果为false,则不会调用被检查对象上定义的自定义inspect()函数。默认值为true。
- if
- 如果customInspect if为false,则不会调用被检查对象上定义的custom inspect()函数。默认值为true。
util.format()
format-string placeholders (1st argument)
format() format-string占位符(第一个参数)
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. - % -单个百分号('%')。这不会消耗一个论点。
#4
41
Another simple method is to convert it to json
另一个简单的方法是将它转换为json
console.log('connection : %j', myObject);
#5
21
Try this:
试试这个:
console.dir(myObject,{depth:null})
#6
16
perhaps console.dir
is all you need.
也许控制台。你所需要的就是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并将结果字符串打印到stdout。
use util option if you need more control.
如果需要更多的控制,请使用util选项。
#7
16
You can also do
你也可以做
console.log(JSON.stringify(myObject, null, 3));
#8
12
Since Node.js 6.4.0, this can be elegantly solved with util.inspect.defaultOptions
:
由于节点。js 6.4.0可以用util. inspection优雅地解决。
require("util").inspect.defaultOptions.depth = null;console.log(myObject);
#9
10
A good way to inspect objects is to use node --inspect option with Chrome DevTools for Node.
检查对象的一个好方法是使用node -inspect选项与Chrome DevTools for node。
node.exe --inspect www.js
Open chrome://inspect/#devices
in chrome and click Open dedicated DevTools for Node
打开chrome://检查/#设备,点击为节点打开专用的DevTools
Now every logged object is available in inspector like regular JS running in chrome.
现在,每个被记录的对象都可以在检查器中使用,比如在chrome中运行的普通JS。
There is no need to reopen inspector, it connects to node automatically as soon as node starts or restarts. Both --inspect and Chrome DevTools for Node may not be available in older versions of Node and Chrome.
无需重新打开检查器,当节点启动或重新启动时,它将自动连接到节点。查看和Chrome开发工具在旧版本的Node和Chrome中可能都不可用。
#10
5
Both of these usages can be applied
这两种用法都可以应用
// more compact and colour can be applied (better for process managers logging)console.dir(queryArgs, { depth: null, colors: true });// clear list of actual valuesconsole.log(JSON.stringify(queryArgs, undefined, 2));
#11
3
You can simply add an inspect()
method to your object which will override the representation of object in console.log
messages
您只需向对象添加一个inspect()方法,该方法将覆盖控制台中的对象表示。日志消息
eg:
例如:
var myObject = { "a":"a", "b":{ "c":"c", "d":{ "e":"e", "f":{ "g":"g", "h":{ "i":"i" } } } }};myObject.inspect = function(){ return JSON.stringify( this, null, ' ' ); }
then, your object will be represented as required in both console.log and node shell
然后,您的对象将在两个控制台中表示为必需的。日志和节点壳
#12
2
A simple trick would be use debug
module to add DEBUG_DEPTH=null
as environment variable when running the script
一个简单的技巧是在运行脚本时使用debug模块将DEBUG_DEPTH=null添加为环境变量
Ex.
前女友。
DEBUG=* DEBUG_DEPTH=null node index.js
调试index.js = * DEBUG_DEPTH =空节点
In you code
在你的代码
const debug = require('debug');debug("%O", myObject);
#13
1
The node REPL has a built-in solution for overriding how objects are displayed, see here.
node REPL有一个内置的解决方案,用于重写对象的显示方式,请参见这里。
The REPL module internally uses
util.inspect()
, when printing values. However,util.inspect
delegates the call to the object'sinspect()
function, if it has one.在打印值时,REPL模块在内部使用util. inspection()。然而,实效。如果对象有一个inspect()函数,那么将调用委托给它。
#14
-1
Try this helper function
试试这个helper函数
const l = (...params) => console.log(...params.map(param => JSON.stringify(param, null, 4)))
Usage:
用法:
l(obj1,obj2...)
You can also use it in other javascript environments like PhantomJs for example
您还可以在其他javascript环境中使用它,比如PhantomJs。
#1
878
You need to use util.inspect()
:
你需要使用util.inspect():
const util = require('util')console.log(util.inspect(myObject, {showHidden: false, depth: null}))// alternative shortcutconsole.log(util.inspect(myObject, false, null))
Outputs
输出
{ a: 'a', b: { c: 'c', d: { e: 'e', f: { g: 'g', h: { i: 'i' } } } } }
See util.inspect()
docs.
看到util.inspect()文档。
#2
420
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/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
#3
130
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).
来自(至少)节点的许多有用答案的汇编。js v0.10.33(稳定)/ v0.11.14(不稳定)可能通过(至少)v7.7.4(此答案的最新更新的当前版本)。
tl;dr
博士tl;
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.
inspect()是诊断输出的核心:console.log()和console.dir()以及节点。js REPL隐式地使用util.inspect(),因此通常不需要直接要求('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()
):log()(及其别名console.info():
-
If the 1st argument is NOT a format string:
util.inspect()
is automatically applied to every argument:o = { one: 1, two: 'deux', foo: function(){} }; console.log(o, [1,2,3]) // -> '{ one: 1, two: 'deux', foo: [Function] } [ 1, 2, 3 ]'
- o = {1, 2: 'deux', foo: function(){};控制台。日志(o,[1,2,3])/ / - >“{:1,二:“两个”,foo:[功能]}[1,2,3]”
- Note that you cannot pass options through
util.inspect()
in this case, which implies 2 notable limitations:- Structural depth of the output is limited to 2 levels (the default).
- Since you cannot change this with
console.log()
, you must instead useconsole.dir()
:console.dir(myObject, { depth: null }
prints with unlimited depth; see below. - 由于不能使用console.log()更改这一点,所以必须使用console.dir(): console。dir(myObject, {depth: null}打印,深度无限;见下文。
- Since you cannot change this with
- 输出的结构深度被限制为2级(默认)。由于不能使用console.log()更改这一点,所以必须使用console.dir(): console。dir(myObject, {depth: null}打印,深度无限;见下文。
- You can't turn syntax coloring on.
- 不能打开语法着色。
- Structural depth of the output is limited to 2 levels (the default).
- 注意,在这种情况下,不能通过util.inspect()传递选项,这意味着两个明显的限制:输出的结构深度限制为两个级别(默认值)。由于不能使用console.log()更改这一点,所以必须使用console.dir(): console。dir(myObject, {depth: null}打印,深度无限;见下文。不能打开语法着色。
- 如果第一个参数不是格式字符串:util.inspect()将自动应用到每个参数:o = {1: 1, 2: 'deux', foo: function(){};控制台。log(o, [1,2,3]) // -> '{1,2: 'deux', foo: [Function]}[1,2,3] '注意,在这个例子中,你不能通过util.inspect()来传递选项,这意味着2个显著的限制:输出的结构深度限制在2级(默认值)。由于不能使用console.log()更改这一点,所以必须使用console.dir(): console。dir(myObject, {depth: null}打印,深度无限;见下文。不能打开语法着色。
-
If the 1st argument IS a format string (see below): uses
util.format()
to print the remaining arguments based on the format string (see below); e.g.:o = { one: 1, two: 'deux', foo: function(){} }; console.log('o as JSON: %j', o) // -> 'o as JSON: {"one":1,"two":"deux"}'
- o = {1, 2: 'deux', foo: function(){};控制台。日志(o为JSON:% j,o)/ / - >“o为JSON:{“一”:1、“两个”:“两个”}”
- Note:
- There is NO placeholder for representing objects
util.inspect()
-style. - 没有用于表示对象的占位符。
- JSON generated with
%j
is NOT pretty-printed. - 使用%j生成的JSON不是漂亮打印的。
- There is NO placeholder for representing objects
- 注意:没有用于表示util.inspect()样式的对象的占位符。使用%j生成的JSON不是漂亮打印的。
- 如果第一个参数是一个格式字符串(参见下面):使用util.format()来打印基于格式字符串的剩余参数(见下面);例如:o = {:1,二:“两个”,foo:函数(){ } };控制台。日志('o as JSON: %j', o) // -> 'o as JSON: {" 1 ":1," 2 ":"deux"}'注意:没有占位符来表示util.inspect()样式的对象。使用%j生成的JSON不是漂亮打印的。
-
If the 1st argument is NOT a format string:
-
console.dir()
:console.dir():
-
Accepts only 1 argument to inspect, and always applies
util.inspect()
- essentially, a wrapper forutil.inspect()
without options by default; e.g.:o = { one: 1, two: 'deux', foo: function(){} }; console.dir(o); // Effectively the same as console.log(o) in this case.
- o = {1, 2: 'deux', foo: function(){};console.dir(o);//在这种情况下与console.log(o)一样有效。
- 只接受一个参数进行检查,并且始终应用util.inspect()——本质上,是util.inspect()的包装器,默认情况下没有选项;例如:o = {:1,二:“两个”,foo:函数(){ } };console.dir(o);//在这种情况下与console.log(o)一样有效。
-
node.js v0.11.14+: The optional 2nd argument specifies options for
util.inspect()
- see below; e.g.:console.dir({ one: 1, two: 'deux'}, { colors: true }); // node 0.11+: Prints object representation with syntax coloring.
- 控制台。dir({1: 1, 2: 'deux'}, {color: true});// node 0.11+:打印带有语法着色的对象表示。
- 节点。js v0.11.14+:可选的第2个参数指定util.inspect()的选项—见下面;例如:控制台。dir({1: 1, 2: 'deux'}, {color: true});// node 0.11+:打印带有语法着色的对象表示。
-
Accepts only 1 argument to inspect, and always applies
-
The REPL: implicitly prints any expression's return value with
util.inspect()
with syntax coloring;
i.e., just typing a variable's name and hitting Enter will print an inspected version of its value; e.g.:o = { one: 1, two: 'deux', foo: function(){} } // echoes the object definition with syntax coloring.
- o = {1: 1, 2: 'deux', foo: function(){} //用语法着色来回应对象定义。
- REPL:使用util.inspect()使用语法着色隐式地打印任何表达式的返回值;即。,只需输入变量的名称并按Enter键,就会打印出其值的检查版本;例如:o = {1: 1, 2: 'deux', foo: function(){} // /用语法着色来回应对象定义。
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. In practice, since line breaks only happen at property boundaries, you will often end up with shorter lines, but they can also be longer (e.g., with long property values).
默认情况下,无论输出是发送到文件还是终端,输出都包装在大约60个字符内。在实践中,由于换行只发生在属性边界处,您通常会得到更短的行,但是它们也可以更长(例如,具有长属性值)。
-
In v6.3.0+ you can use the
breakLength
option to override the 60-character limit; if you set it toInfinity
, everything is output on a single line.在v6.3.0+中,您可以使用breakLength选项来覆盖60个字符的限制;如果你把它设为无穷大,所有的东西都输出在一条直线上。
If you want more control over pretty-printing, consider using JSON.stringify()
with a 3rd argument, but note the following:
如果您想要对漂亮打印进行更多的控制,可以考虑使用JSON.stringify()作为第三个参数,但是请注意以下内容:
-
Fails with objects that have circular references, such as
module
in the global context. - 对具有循环引用的对象失败,例如全局上下文中的模块。
- Methods (functions) will by design NOT be included.
- 方法(功能)在设计上不包括在内。
- You can't opt into showing hidden (non-enumerable) properties.
- 您不能选择显示隐藏的(非可枚举的)属性。
- Example call:
JSON.stringify({ one: 1, two: 'deux', three: true}, undefined, 2); // creates a pretty-printed multiline JSON representation indented with 2 spaces
- JSON。stringify({1: 1, 2: 'deux', 3: true}, undefined, 2);//创建一个漂亮打印的多行JSON表示,缩进两个空格
- 示例调用:JSON。stringify({1: 1, 2: 'deux', 3: true}, undefined, 2);//创建一个漂亮打印的多行JSON表示,缩进两个空格
util.inspect()
options object (2nd argument):
视察()选项对象(第二个参数):
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:
可以传递一个可选选项对象来改变格式化字符串的某些方面:
-
showHidden
- if
true
, then the object's non-enumerable properties [those designated not to show up when you usefor keys in obj
orObject.keys(obj)
] will be shown too. Defaults tofalse
. - 如果为真,那么对象的非可枚举属性[当您在obj或object .keys中使用键时指定不显示的属性]也将显示出来。默认值为false。
- if
- 如果为真,那么对象的非可枚举属性(当您在obj或object .keys(obj)中使用键时指定不显示的属性)也将显示出来。默认值为false。
-
depth
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
null
. - 告诉检查在格式化对象时要递归多少次。这对于检查大型复杂对象非常有用。默认为2。要使它无限递归,传递null。
- tells inspect how many times to recurse while formatting the object. This is useful for inspecting large complicated objects. Defaults to 2. To make it recurse indefinitely, pass
- 深度告诉检查在格式化对象时要递归多少次。这对于检查大型复杂对象非常有用。默认为2。要使它无限递归,传递null。
-
colors
- if true, then the output will be styled with ANSI color codes. Defaults to
false
. Colors are customizable [... - see link]. - 如果为真,则输出将使用ANSI颜色代码进行样式化。默认值为false。颜色可定制的[…(见链接)。
- if true, then the output will be styled with ANSI color codes. Defaults to
- 如果颜色为真,则输出将使用ANSI颜色代码进行样式化。默认值为false。颜色可定制的[…(见链接)。
-
customInspect
- if
false
, then custominspect()
functions defined on the objects being inspected won't be called. Defaults totrue
. - 如果为false,则不会调用被检查对象上定义的自定义inspect()函数。默认值为true。
- if
- 如果customInspect if为false,则不会调用被检查对象上定义的custom inspect()函数。默认值为true。
util.format()
format-string placeholders (1st argument)
format() format-string占位符(第一个参数)
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. - % -单个百分号('%')。这不会消耗一个论点。
#4
41
Another simple method is to convert it to json
另一个简单的方法是将它转换为json
console.log('connection : %j', myObject);
#5
21
Try this:
试试这个:
console.dir(myObject,{depth:null})
#6
16
perhaps console.dir
is all you need.
也许控制台。你所需要的就是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并将结果字符串打印到stdout。
use util option if you need more control.
如果需要更多的控制,请使用util选项。
#7
16
You can also do
你也可以做
console.log(JSON.stringify(myObject, null, 3));
#8
12
Since Node.js 6.4.0, this can be elegantly solved with util.inspect.defaultOptions
:
由于节点。js 6.4.0可以用util. inspection优雅地解决。
require("util").inspect.defaultOptions.depth = null;console.log(myObject);
#9
10
A good way to inspect objects is to use node --inspect option with Chrome DevTools for Node.
检查对象的一个好方法是使用node -inspect选项与Chrome DevTools for node。
node.exe --inspect www.js
Open chrome://inspect/#devices
in chrome and click Open dedicated DevTools for Node
打开chrome://检查/#设备,点击为节点打开专用的DevTools
Now every logged object is available in inspector like regular JS running in chrome.
现在,每个被记录的对象都可以在检查器中使用,比如在chrome中运行的普通JS。
There is no need to reopen inspector, it connects to node automatically as soon as node starts or restarts. Both --inspect and Chrome DevTools for Node may not be available in older versions of Node and Chrome.
无需重新打开检查器,当节点启动或重新启动时,它将自动连接到节点。查看和Chrome开发工具在旧版本的Node和Chrome中可能都不可用。
#10
5
Both of these usages can be applied
这两种用法都可以应用
// more compact and colour can be applied (better for process managers logging)console.dir(queryArgs, { depth: null, colors: true });// clear list of actual valuesconsole.log(JSON.stringify(queryArgs, undefined, 2));
#11
3
You can simply add an inspect()
method to your object which will override the representation of object in console.log
messages
您只需向对象添加一个inspect()方法,该方法将覆盖控制台中的对象表示。日志消息
eg:
例如:
var myObject = { "a":"a", "b":{ "c":"c", "d":{ "e":"e", "f":{ "g":"g", "h":{ "i":"i" } } } }};myObject.inspect = function(){ return JSON.stringify( this, null, ' ' ); }
then, your object will be represented as required in both console.log and node shell
然后,您的对象将在两个控制台中表示为必需的。日志和节点壳
#12
2
A simple trick would be use debug
module to add DEBUG_DEPTH=null
as environment variable when running the script
一个简单的技巧是在运行脚本时使用debug模块将DEBUG_DEPTH=null添加为环境变量
Ex.
前女友。
DEBUG=* DEBUG_DEPTH=null node index.js
调试index.js = * DEBUG_DEPTH =空节点
In you code
在你的代码
const debug = require('debug');debug("%O", myObject);
#13
1
The node REPL has a built-in solution for overriding how objects are displayed, see here.
node REPL有一个内置的解决方案,用于重写对象的显示方式,请参见这里。
The REPL module internally uses
util.inspect()
, when printing values. However,util.inspect
delegates the call to the object'sinspect()
function, if it has one.在打印值时,REPL模块在内部使用util. inspection()。然而,实效。如果对象有一个inspect()函数,那么将调用委托给它。
#14
-1
Try this helper function
试试这个helper函数
const l = (...params) => console.log(...params.map(param => JSON.stringify(param, null, 4)))
Usage:
用法:
l(obj1,obj2...)
You can also use it in other javascript environments like PhantomJs for example
您还可以在其他javascript环境中使用它,比如PhantomJs。