diff --git a/doc/api/console.md b/doc/api/console.md index 9ccdad99983017..ea95d119d3674b 100644 --- a/doc/api/console.md +++ b/doc/api/console.md @@ -417,6 +417,26 @@ added: v0.1.100 The `console.warn()` function is an alias for [`console.error()`][]. +### console.inspector + + +V8 contexts provide a `console` global object, but by default it is only useful +for sending console messages to attached inspectors. This is provided as the +`inspector` property of the global `console` object (but not of other instances +of `Console`). + +For example, to send a log message, `'hello'` to an attached inspector console: + +```js +console.inspector.log('hello'); +// 'hello' appears in inspector console, but not in node's stdout +``` + +When an inspector is connected, logging methods on the global `console` object +will also send messages to the inspector console. + [`console.error()`]: #console_console_error_data_args [`console.group()`]: #console_console_group_label [`console.log()`]: #console_console_log_data_args diff --git a/lib/internal/bootstrap_node.js b/lib/internal/bootstrap_node.js index 3ce558611e46e1..266d189f6b3f86 100644 --- a/lib/internal/bootstrap_node.js +++ b/lib/internal/bootstrap_node.js @@ -344,11 +344,7 @@ wrappedConsole[key], config); } - for (const key of Object.keys(originalConsole)) { - if (wrappedConsole.hasOwnProperty(key)) - continue; - wrappedConsole[key] = originalConsole[key]; - } + wrappedConsole.inspector = originalConsole; } function setupProcessFatal() { diff --git a/test/parallel/test-console.js b/test/parallel/test-console.js index cc1d3fb83412d1..8534aea7ead6f8 100644 --- a/test/parallel/test-console.js +++ b/test/parallel/test-console.js @@ -22,6 +22,7 @@ 'use strict'; const common = require('../common'); const assert = require('assert'); +const vm = require('vm'); assert.ok(process.stdout.writable); assert.ok(process.stderr.writable); @@ -187,3 +188,16 @@ common.hijackStderr(common.mustCall(function(data) { assert.strictEqual(data.includes('no such label'), true); }); })); + +const pristineConsole = vm.runInNewContext('this.console'); +for (const name in console.inspector) { + + // No inspector-only functions are available on the global console + if (name in console) { + assert.notStrictEqual(console[name], console.inspector[name]); + } + + // console.inspector should be the same as a pristine console object from a v8 + // context. + assert(name in pristineConsole); +}