Skip to content

Commit 4fe5175

Browse files
addaleaxjasnell
authored andcommitted
console: allow options object as constructor arg
PR-URL: #19372 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 63565e1 commit 4fe5175

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

doc/api/console.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,17 +79,22 @@ const { Console } = console;
7979
```
8080

8181
### new Console(stdout[, stderr][, ignoreErrors])
82+
### new Console(options)
8283
<!-- YAML
8384
changes:
8485
- version: v8.0.0
8586
pr-url: https://github.com/nodejs/node/pull/9744
8687
description: The `ignoreErrors` option was introduced.
88+
- version: REPLACEME
89+
pr-url: https://github.com/nodejs/node/pull/19372
90+
description: The `Console` constructor now supports an `options` argument.
8791
-->
8892

89-
* `stdout` {stream.Writable}
90-
* `stderr` {stream.Writable}
91-
* `ignoreErrors` {boolean} Ignore errors when writing to the underlying streams.
92-
Defaults to `true`.
93+
* `options` {Object}
94+
* `stdout` {stream.Writable}
95+
* `stderr` {stream.Writable}
96+
* `ignoreErrors` {boolean} Ignore errors when writing to the underlying
97+
streams. **Default:** `true`.
9398

9499
Creates a new `Console` with one or two writable stream instances. `stdout` is a
95100
writable stream to print log or info output. `stderr` is used for warning or
@@ -99,7 +104,7 @@ error output. If `stderr` is not provided, `stdout` is used for `stderr`.
99104
const output = fs.createWriteStream('./stdout.log');
100105
const errorOutput = fs.createWriteStream('./stderr.log');
101106
// custom simple logger
102-
const logger = new Console(output, errorOutput);
107+
const logger = new Console({ stdout: output, stderr: errorOutput });
103108
// use it like console
104109
const count = 5;
105110
logger.log('count: %d', count);
@@ -110,7 +115,7 @@ The global `console` is a special `Console` whose output is sent to
110115
[`process.stdout`][] and [`process.stderr`][]. It is equivalent to calling:
111116

112117
```js
113-
new Console(process.stdout, process.stderr);
118+
new Console({ stdout: process.stdout, stderr: process.stderr });
114119
```
115120

116121
### console.assert(value[, ...message])

lib/console.js

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,28 @@ const {
5151
// Track amount of indentation required via `console.group()`.
5252
const kGroupIndent = Symbol('groupIndent');
5353

54-
function Console(stdout, stderr, ignoreErrors = true) {
54+
function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
5555
if (!(this instanceof Console)) {
56-
return new Console(stdout, stderr, ignoreErrors);
56+
return new Console(...arguments);
5757
}
58+
59+
let stdout, stderr, ignoreErrors;
60+
if (options && typeof options.write !== 'function') {
61+
({
62+
stdout,
63+
stderr = stdout,
64+
ignoreErrors = true
65+
} = options);
66+
} else {
67+
stdout = options;
68+
stderr = arguments[1];
69+
ignoreErrors = arguments[2] === undefined ? true : arguments[2];
70+
}
71+
5872
if (!stdout || typeof stdout.write !== 'function') {
5973
throw new ERR_CONSOLE_WRITABLE_STREAM('stdout');
6074
}
61-
if (!stderr) {
62-
stderr = stdout;
63-
} else if (typeof stderr.write !== 'function') {
75+
if (!stderr || typeof stderr.write !== 'function') {
6476
throw new ERR_CONSOLE_WRITABLE_STREAM('stderr');
6577
}
6678

@@ -369,7 +381,10 @@ Console.prototype.table = function(tabularData, properties) {
369381
return final(keys, values);
370382
};
371383

372-
module.exports = new Console(process.stdout, process.stderr);
384+
module.exports = new Console({
385+
stdout: process.stdout,
386+
stderr: process.stderr
387+
});
373388
module.exports.Console = Console;
374389

375390
function noop() {}

0 commit comments

Comments
 (0)