console: allow options
object as constructor arg
PR-URL: https://github.com/nodejs/node/pull/19372 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
parent
65765989d3
commit
ce58df58d0
@ -79,17 +79,22 @@ const { Console } = console;
|
|||||||
```
|
```
|
||||||
|
|
||||||
### new Console(stdout[, stderr][, ignoreErrors])
|
### new Console(stdout[, stderr][, ignoreErrors])
|
||||||
|
### new Console(options)
|
||||||
<!-- YAML
|
<!-- YAML
|
||||||
changes:
|
changes:
|
||||||
- version: v8.0.0
|
- version: v8.0.0
|
||||||
pr-url: https://github.com/nodejs/node/pull/9744
|
pr-url: https://github.com/nodejs/node/pull/9744
|
||||||
description: The `ignoreErrors` option was introduced.
|
description: The `ignoreErrors` option was introduced.
|
||||||
|
- version: REPLACEME
|
||||||
|
pr-url: https://github.com/nodejs/node/pull/19372
|
||||||
|
description: The `Console` constructor now supports an `options` argument.
|
||||||
-->
|
-->
|
||||||
|
|
||||||
* `stdout` {stream.Writable}
|
* `options` {Object}
|
||||||
* `stderr` {stream.Writable}
|
* `stdout` {stream.Writable}
|
||||||
* `ignoreErrors` {boolean} Ignore errors when writing to the underlying streams.
|
* `stderr` {stream.Writable}
|
||||||
Defaults to `true`.
|
* `ignoreErrors` {boolean} Ignore errors when writing to the underlying
|
||||||
|
streams. **Default:** `true`.
|
||||||
|
|
||||||
Creates a new `Console` with one or two writable stream instances. `stdout` is a
|
Creates a new `Console` with one or two writable stream instances. `stdout` is a
|
||||||
writable stream to print log or info output. `stderr` is used for warning or
|
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`.
|
|||||||
const output = fs.createWriteStream('./stdout.log');
|
const output = fs.createWriteStream('./stdout.log');
|
||||||
const errorOutput = fs.createWriteStream('./stderr.log');
|
const errorOutput = fs.createWriteStream('./stderr.log');
|
||||||
// custom simple logger
|
// custom simple logger
|
||||||
const logger = new Console(output, errorOutput);
|
const logger = new Console({ stdout: output, stderr: errorOutput });
|
||||||
// use it like console
|
// use it like console
|
||||||
const count = 5;
|
const count = 5;
|
||||||
logger.log('count: %d', count);
|
logger.log('count: %d', count);
|
||||||
@ -110,7 +115,7 @@ The global `console` is a special `Console` whose output is sent to
|
|||||||
[`process.stdout`][] and [`process.stderr`][]. It is equivalent to calling:
|
[`process.stdout`][] and [`process.stderr`][]. It is equivalent to calling:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
new Console(process.stdout, process.stderr);
|
new Console({ stdout: process.stdout, stderr: process.stderr });
|
||||||
```
|
```
|
||||||
|
|
||||||
### console.assert(value[, ...message])
|
### console.assert(value[, ...message])
|
||||||
|
@ -51,16 +51,28 @@ const {
|
|||||||
// Track amount of indentation required via `console.group()`.
|
// Track amount of indentation required via `console.group()`.
|
||||||
const kGroupIndent = Symbol('groupIndent');
|
const kGroupIndent = Symbol('groupIndent');
|
||||||
|
|
||||||
function Console(stdout, stderr, ignoreErrors = true) {
|
function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
|
||||||
if (!(this instanceof Console)) {
|
if (!(this instanceof Console)) {
|
||||||
return new Console(stdout, stderr, ignoreErrors);
|
return new Console(...arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let stdout, stderr, ignoreErrors;
|
||||||
|
if (options && typeof options.write !== 'function') {
|
||||||
|
({
|
||||||
|
stdout,
|
||||||
|
stderr = stdout,
|
||||||
|
ignoreErrors = true
|
||||||
|
} = options);
|
||||||
|
} else {
|
||||||
|
stdout = options;
|
||||||
|
stderr = arguments[1];
|
||||||
|
ignoreErrors = arguments[2] === undefined ? true : arguments[2];
|
||||||
|
}
|
||||||
|
|
||||||
if (!stdout || typeof stdout.write !== 'function') {
|
if (!stdout || typeof stdout.write !== 'function') {
|
||||||
throw new ERR_CONSOLE_WRITABLE_STREAM('stdout');
|
throw new ERR_CONSOLE_WRITABLE_STREAM('stdout');
|
||||||
}
|
}
|
||||||
if (!stderr) {
|
if (!stderr || typeof stderr.write !== 'function') {
|
||||||
stderr = stdout;
|
|
||||||
} else if (typeof stderr.write !== 'function') {
|
|
||||||
throw new ERR_CONSOLE_WRITABLE_STREAM('stderr');
|
throw new ERR_CONSOLE_WRITABLE_STREAM('stderr');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -369,7 +381,10 @@ Console.prototype.table = function(tabularData, properties) {
|
|||||||
return final(keys, values);
|
return final(keys, values);
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = new Console(process.stdout, process.stderr);
|
module.exports = new Console({
|
||||||
|
stdout: process.stdout,
|
||||||
|
stderr: process.stderr
|
||||||
|
});
|
||||||
module.exports.Console = Console;
|
module.exports.Console = Console;
|
||||||
|
|
||||||
function noop() {}
|
function noop() {}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user