util: change util.inspect depth default
The current default is not ideal in most use cases. Therefore it is changed to showing unlimited depth in case util.inspect is called directly. The default is kept as before for console.log and similar. Using console.dir will now show a depth of up to five and console.assert / console.trace will show a unlimited depth. PR-URL: https://github.com/nodejs/node/pull/17907 Refs: https://github.com/nodejs/node/issues/12693 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
parent
9d3958102e
commit
b994b8eff6
@ -327,6 +327,9 @@ stream.write('With ES6');
|
|||||||
<!-- YAML
|
<!-- YAML
|
||||||
added: v0.3.0
|
added: v0.3.0
|
||||||
changes:
|
changes:
|
||||||
|
- version: REPLACEME
|
||||||
|
pr-url: https://github.com/nodejs/node/pull/17907
|
||||||
|
description: The `depth` default changed to Infinity.
|
||||||
- version: REPLACEME
|
- version: REPLACEME
|
||||||
pr-url: https://github.com/nodejs/node/pull/REPLACEME
|
pr-url: https://github.com/nodejs/node/pull/REPLACEME
|
||||||
description: The `compact` option is supported now.
|
description: The `compact` option is supported now.
|
||||||
@ -349,9 +352,6 @@ changes:
|
|||||||
* `options` {Object}
|
* `options` {Object}
|
||||||
* `showHidden` {boolean} If `true`, the `object`'s non-enumerable symbols and
|
* `showHidden` {boolean} If `true`, the `object`'s non-enumerable symbols and
|
||||||
properties will be included in the formatted result. Defaults to `false`.
|
properties will be included in the formatted result. Defaults to `false`.
|
||||||
* `depth` {number} Specifies the number of 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`.
|
|
||||||
* `colors` {boolean} If `true`, the output will be styled with ANSI color
|
* `colors` {boolean} If `true`, the output will be styled with ANSI color
|
||||||
codes. Defaults to `false`. Colors are customizable, see
|
codes. Defaults to `false`. Colors are customizable, see
|
||||||
[Customizing `util.inspect` colors][].
|
[Customizing `util.inspect` colors][].
|
||||||
@ -362,8 +362,8 @@ changes:
|
|||||||
objects. Defaults to `false`.
|
objects. Defaults to `false`.
|
||||||
* `maxArrayLength` {number} Specifies the maximum number of array and
|
* `maxArrayLength` {number} Specifies the maximum number of array and
|
||||||
`TypedArray` elements to include when formatting. Defaults to `100`. Set to
|
`TypedArray` elements to include when formatting. Defaults to `100`. Set to
|
||||||
`null` to show all array elements. Set to `0` or negative to show no array
|
`null` or `Infinity` to show all array elements. Set to `0` or negative to
|
||||||
elements.
|
show no array elements.
|
||||||
* `breakLength` {number} The length at which an object's keys are split
|
* `breakLength` {number} The length at which an object's keys are split
|
||||||
across multiple lines. Set to `Infinity` to format an object as a single
|
across multiple lines. Set to `Infinity` to format an object as a single
|
||||||
line. Defaults to 60 for legacy compatibility.
|
line. Defaults to 60 for legacy compatibility.
|
||||||
@ -374,6 +374,10 @@ changes:
|
|||||||
objects the same as arrays. Note that no text will be reduced below 16
|
objects the same as arrays. Note that no text will be reduced below 16
|
||||||
characters, no matter the `breakLength` size. For more information, see the
|
characters, no matter the `breakLength` size. For more information, see the
|
||||||
example below. Defaults to `true`.
|
example below. Defaults to `true`.
|
||||||
|
* `depth` {number} Specifies the number visible nested Objects in an `object`.
|
||||||
|
This is useful to minimize the inspection output for large complicated
|
||||||
|
objects. To make it recurse indefinitely pass `null` or `Infinity`. Defaults
|
||||||
|
to `Infinity`.
|
||||||
|
|
||||||
The `util.inspect()` method returns a string representation of `object` that is
|
The `util.inspect()` method returns a string representation of `object` that is
|
||||||
intended for debugging. The output of `util.inspect` may change at any time
|
intended for debugging. The output of `util.inspect` may change at any time
|
||||||
@ -398,12 +402,23 @@ util.inspect(new Bar()); // 'Bar {}'
|
|||||||
util.inspect(baz); // '[foo] {}'
|
util.inspect(baz); // '[foo] {}'
|
||||||
```
|
```
|
||||||
|
|
||||||
The following example inspects all properties of the `util` object:
|
The following example limits the inspected output of the `paths` property:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const util = require('util');
|
const util = require('util');
|
||||||
|
|
||||||
console.log(util.inspect(util, { showHidden: true, depth: null }));
|
console.log(util.inspect(module, { depth: 0 }));
|
||||||
|
// Instead of showing all entries in `paths` `[Array]` is used to limit the
|
||||||
|
// output for readability:
|
||||||
|
|
||||||
|
// Module {
|
||||||
|
// id: '<repl>',
|
||||||
|
// exports: {},
|
||||||
|
// parent: undefined,
|
||||||
|
// filename: null,
|
||||||
|
// loaded: false,
|
||||||
|
// children: [],
|
||||||
|
// paths: [Array] }
|
||||||
```
|
```
|
||||||
|
|
||||||
Values may supply their own custom `inspect(depth, opts)` functions, when
|
Values may supply their own custom `inspect(depth, opts)` functions, when
|
||||||
@ -423,7 +438,7 @@ const o = {
|
|||||||
'foo']], 4],
|
'foo']], 4],
|
||||||
b: new Map([['za', 1], ['zb', 'test']])
|
b: new Map([['za', 1], ['zb', 'test']])
|
||||||
};
|
};
|
||||||
console.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));
|
console.log(util.inspect(o, { compact: true, breakLength: 80 }));
|
||||||
|
|
||||||
// This will print
|
// This will print
|
||||||
|
|
||||||
@ -437,7 +452,7 @@ console.log(util.inspect(o, { compact: true, depth: 5, breakLength: 80 }));
|
|||||||
// b: Map { 'za' => 1, 'zb' => 'test' } }
|
// b: Map { 'za' => 1, 'zb' => 'test' } }
|
||||||
|
|
||||||
// Setting `compact` to false changes the output to be more reader friendly.
|
// Setting `compact` to false changes the output to be more reader friendly.
|
||||||
console.log(util.inspect(o, { compact: false, depth: 5, breakLength: 80 }));
|
console.log(util.inspect(o, { compact: false, breakLength: 80 }));
|
||||||
|
|
||||||
// {
|
// {
|
||||||
// a: [
|
// a: [
|
||||||
|
@ -103,8 +103,9 @@ function hasOwnProperty(obj, prop) {
|
|||||||
// Can overridden with custom print functions, such as `probe` or `eyes.js`.
|
// Can overridden with custom print functions, such as `probe` or `eyes.js`.
|
||||||
// This is the default "writer" value if none is passed in the REPL options.
|
// This is the default "writer" value if none is passed in the REPL options.
|
||||||
const writer = exports.writer = (obj) => util.inspect(obj, writer.options);
|
const writer = exports.writer = (obj) => util.inspect(obj, writer.options);
|
||||||
writer.options =
|
writer.options = Object.assign({},
|
||||||
Object.assign({}, util.inspect.defaultOptions, { showProxy: true });
|
util.inspect.defaultOptions,
|
||||||
|
{ showProxy: true, depth: 2 });
|
||||||
|
|
||||||
exports._builtinLibs = internalModule.builtinLibs;
|
exports._builtinLibs = internalModule.builtinLibs;
|
||||||
|
|
||||||
|
@ -64,7 +64,7 @@ const {
|
|||||||
|
|
||||||
const inspectDefaultOptions = Object.seal({
|
const inspectDefaultOptions = Object.seal({
|
||||||
showHidden: false,
|
showHidden: false,
|
||||||
depth: 2,
|
depth: null,
|
||||||
colors: false,
|
colors: false,
|
||||||
customInspect: true,
|
customInspect: true,
|
||||||
showProxy: false,
|
showProxy: false,
|
||||||
@ -317,8 +317,7 @@ Object.defineProperty(inspect, 'defaultOptions', {
|
|||||||
if (options === null || typeof options !== 'object') {
|
if (options === null || typeof options !== 'object') {
|
||||||
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
|
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'options', 'Object');
|
||||||
}
|
}
|
||||||
Object.assign(inspectDefaultOptions, options);
|
return _extend(inspectDefaultOptions, options);
|
||||||
return inspectDefaultOptions;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
require('../common');
|
require('../common');
|
||||||
const assert = require('assert');
|
const assert = require('assert');
|
||||||
const BufferList = require('internal/streams/BufferList');
|
const BufferList = require('internal/streams/BufferList');
|
||||||
|
const util = require('util');
|
||||||
|
|
||||||
// Test empty buffer list.
|
// Test empty buffer list.
|
||||||
const emptyList = new BufferList();
|
const emptyList = new BufferList();
|
||||||
@ -25,3 +26,10 @@ assert.strictEqual(list.join(','), 'foo');
|
|||||||
const shifted = list.shift();
|
const shifted = list.shift();
|
||||||
assert.strictEqual(shifted, 'foo');
|
assert.strictEqual(shifted, 'foo');
|
||||||
assert.deepStrictEqual(list, new BufferList());
|
assert.deepStrictEqual(list, new BufferList());
|
||||||
|
|
||||||
|
const tmp = util.inspect.defaultOptions.colors;
|
||||||
|
util.inspect.defaultOptions = { colors: true };
|
||||||
|
assert.strictEqual(
|
||||||
|
util.inspect(list),
|
||||||
|
'BufferList { length: \u001b[33m0\u001b[39m }');
|
||||||
|
util.inspect.defaultOptions = { colors: tmp };
|
||||||
|
@ -48,13 +48,17 @@ const expected1 = 'Proxy [ {}, {} ]';
|
|||||||
const expected2 = 'Proxy [ Proxy [ {}, {} ], {} ]';
|
const expected2 = 'Proxy [ Proxy [ {}, {} ], {} ]';
|
||||||
const expected3 = 'Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ]';
|
const expected3 = 'Proxy [ Proxy [ Proxy [ {}, {} ], {} ], Proxy [ {}, {} ] ]';
|
||||||
const expected4 = 'Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [ {}, {} ], {} ] ]';
|
const expected4 = 'Proxy [ Proxy [ {}, {} ], Proxy [ Proxy [ {}, {} ], {} ] ]';
|
||||||
const expected5 = 'Proxy [ Proxy [ Proxy [ Proxy [Array], {} ],' +
|
const expected5 = 'Proxy [ Proxy [ Proxy [ Proxy [ {}, {} ], {} ],' +
|
||||||
' Proxy [ {}, {} ] ],\n Proxy [ Proxy [ {}, {} ]' +
|
' Proxy [ {}, {} ] ],\n Proxy [ Proxy [ {}, {} ]' +
|
||||||
', Proxy [ Proxy [Array], {} ] ] ]';
|
', Proxy [ Proxy [ {}, {} ], {} ] ] ]';
|
||||||
const expected6 = 'Proxy [ Proxy [ Proxy [ Proxy [Array], Proxy [Array]' +
|
const expected6 = 'Proxy [ Proxy [ Proxy [ Proxy [ Proxy [ {}, {} ], {} ], ' +
|
||||||
' ],\n Proxy [ Proxy [Array], Proxy [Array] ] ],\n' +
|
'Proxy [ {}, {} ] ],\n' +
|
||||||
' Proxy [ Proxy [ Proxy [Array], Proxy [Array] ],\n' +
|
' Proxy [ Proxy [ {}, {} ], ' +
|
||||||
' Proxy [ Proxy [Array], Proxy [Array] ] ] ]';
|
'Proxy [ Proxy [ {}, {} ], {} ] ] ],\n' +
|
||||||
|
' Proxy [ Proxy [ Proxy [ Proxy [ {}, {} ], {} ], ' +
|
||||||
|
'Proxy [ {}, {} ] ],\n' +
|
||||||
|
' Proxy [ Proxy [ {}, {} ], ' +
|
||||||
|
'Proxy [ Proxy [ {}, {} ], {} ] ] ] ]';
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
util.inspect(proxy1, { showProxy: true, depth: null }),
|
util.inspect(proxy1, { showProxy: true, depth: null }),
|
||||||
expected1);
|
expected1);
|
||||||
|
@ -68,7 +68,7 @@ assert.strictEqual(util.inspect({ a: 1, b: 2 }), '{ a: 1, b: 2 }');
|
|||||||
assert.strictEqual(util.inspect({ 'a': {} }), '{ a: {} }');
|
assert.strictEqual(util.inspect({ 'a': {} }), '{ a: {} }');
|
||||||
assert.strictEqual(util.inspect({ 'a': { 'b': 2 } }), '{ a: { b: 2 } }');
|
assert.strictEqual(util.inspect({ 'a': { 'b': 2 } }), '{ a: { b: 2 } }');
|
||||||
assert.strictEqual(util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }),
|
assert.strictEqual(util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }),
|
||||||
'{ a: { b: { c: [Object] } } }');
|
'{ a: { b: { c: { d: 2 } } } }');
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }, false, null),
|
util.inspect({ 'a': { 'b': { 'c': { 'd': 2 } } } }, false, null),
|
||||||
'{ a: { b: { c: { d: 2 } } } }');
|
'{ a: { b: { c: { d: 2 } } } }');
|
||||||
@ -106,7 +106,7 @@ assert.strictEqual(util.inspect((new JSStream())._externalStream),
|
|||||||
assert.strictEqual(util.inspect({ a: regexp }, false, 0), '{ a: /regexp/ }');
|
assert.strictEqual(util.inspect({ a: regexp }, false, 0), '{ a: /regexp/ }');
|
||||||
}
|
}
|
||||||
|
|
||||||
assert(/Object/.test(
|
assert(!/Object/.test(
|
||||||
util.inspect({ a: { a: { a: { a: {} } } } }, undefined, undefined, true)
|
util.inspect({ a: { a: { a: { a: {} } } } }, undefined, undefined, true)
|
||||||
));
|
));
|
||||||
assert(!/Object/.test(
|
assert(!/Object/.test(
|
||||||
@ -1012,15 +1012,15 @@ if (typeof Symbol !== 'undefined') {
|
|||||||
// Empty and circular before depth
|
// Empty and circular before depth
|
||||||
{
|
{
|
||||||
const arr = [[[[]]]];
|
const arr = [[[[]]]];
|
||||||
assert.strictEqual(util.inspect(arr), '[ [ [ [] ] ] ]');
|
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [] ] ] ]');
|
||||||
arr[0][0][0][0] = [];
|
arr[0][0][0][0] = [];
|
||||||
assert.strictEqual(util.inspect(arr), '[ [ [ [Array] ] ] ]');
|
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [Array] ] ] ]');
|
||||||
arr[0][0][0] = {};
|
arr[0][0][0] = {};
|
||||||
assert.strictEqual(util.inspect(arr), '[ [ [ {} ] ] ]');
|
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ {} ] ] ]');
|
||||||
arr[0][0][0] = { a: 2 };
|
arr[0][0][0] = { a: 2 };
|
||||||
assert.strictEqual(util.inspect(arr), '[ [ [ [Object] ] ] ]');
|
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [Object] ] ] ]');
|
||||||
arr[0][0][0] = arr;
|
arr[0][0][0] = arr;
|
||||||
assert.strictEqual(util.inspect(arr), '[ [ [ [Circular] ] ] ]');
|
assert.strictEqual(util.inspect(arr, { depth: 2 }), '[ [ [ [Circular] ] ] ]');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Corner cases.
|
// Corner cases.
|
||||||
@ -1117,22 +1117,22 @@ if (typeof Symbol !== 'undefined') {
|
|||||||
assert(!/1 more item/.test(util.inspect(arr)));
|
assert(!/1 more item/.test(util.inspect(arr)));
|
||||||
util.inspect.defaultOptions.maxArrayLength = oldOptions.maxArrayLength;
|
util.inspect.defaultOptions.maxArrayLength = oldOptions.maxArrayLength;
|
||||||
assert(/1 more item/.test(util.inspect(arr)));
|
assert(/1 more item/.test(util.inspect(arr)));
|
||||||
util.inspect.defaultOptions.depth = null;
|
util.inspect.defaultOptions.depth = 2;
|
||||||
assert(!/Object/.test(util.inspect(obj)));
|
|
||||||
util.inspect.defaultOptions.depth = oldOptions.depth;
|
|
||||||
assert(/Object/.test(util.inspect(obj)));
|
assert(/Object/.test(util.inspect(obj)));
|
||||||
|
util.inspect.defaultOptions.depth = oldOptions.depth;
|
||||||
|
assert(!/Object/.test(util.inspect(obj)));
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
JSON.stringify(util.inspect.defaultOptions),
|
JSON.stringify(util.inspect.defaultOptions),
|
||||||
JSON.stringify(oldOptions)
|
JSON.stringify(oldOptions)
|
||||||
);
|
);
|
||||||
|
|
||||||
// Set multiple options through object assignment
|
// Set multiple options through object assignment
|
||||||
util.inspect.defaultOptions = { maxArrayLength: null, depth: null };
|
util.inspect.defaultOptions = { maxArrayLength: null, depth: 2 };
|
||||||
assert(!/1 more item/.test(util.inspect(arr)));
|
assert(!/1 more item/.test(util.inspect(arr)));
|
||||||
assert(!/Object/.test(util.inspect(obj)));
|
assert(/Object/.test(util.inspect(obj)));
|
||||||
util.inspect.defaultOptions = oldOptions;
|
util.inspect.defaultOptions = oldOptions;
|
||||||
assert(/1 more item/.test(util.inspect(arr)));
|
assert(/1 more item/.test(util.inspect(arr)));
|
||||||
assert(/Object/.test(util.inspect(obj)));
|
assert(!/Object/.test(util.inspect(obj)));
|
||||||
assert.strictEqual(
|
assert.strictEqual(
|
||||||
JSON.stringify(util.inspect.defaultOptions),
|
JSON.stringify(util.inspect.defaultOptions),
|
||||||
JSON.stringify(oldOptions)
|
JSON.stringify(oldOptions)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user