new Console(options)


  • options <Object>

    • stdout <stream.Writable>
    • stderr <stream.Writable>
    • ignoreErrors <boolean> 在写入底层流时忽略错误。默认值: true
    • colorMode <boolean> | <string>Console 实例设置颜色支持。 设置为 true 会在检查值时启用着色,设置为 'auto' 将使颜色支持取决 isTTY 属性的值和 getColorDepth() 在相应流上返回的值。 如果设置了 inspectOptions.colors,则不能使用此选项。 默认值: 'auto'
    • inspectOptions <Object> 指定传给 util.inspect() 的选项。

创建具有一个或两个可写流实例的新 Consolestdout 是一个可写流,用于打印日志或信息输出。 stderr 用于警告或错误输出。 如果未提供 stderr,则 stdout 用于 stderr

const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
// 自定义的简单记录器。
const logger = new Console({ stdout: output, stderr: errorOutput });
// 像控制台一样使用它。
const count = 5;
logger.log('count: %d', count);
// 在 stdout.log 中: count 5

全局的 console 是一个特殊的 Console,其输出发送到 process.stdoutprocess.stderr。 相当于调用:

new Console({ stdout: process.stdout, stderr: process.stderr });
  • options <Object>

    • stdout <stream.Writable>
    • stderr <stream.Writable>
    • ignoreErrors <boolean> Ignore errors when writing to the underlying streams. Default: true.
    • colorMode <boolean> | <string> Set color support for this Console instance. Setting to true enables coloring while inspecting values, setting to 'auto' will make color support depend on the value of the isTTY property and the value returned by getColorDepth() on the respective stream. This option can not be used, if inspectOptions.colors is set as well. Default: 'auto'.
    • inspectOptions <Object> Specifies options that are passed along to util.inspect().

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 error output. If stderr is not provided, stdout is used for stderr.

const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
// Custom simple logger
const logger = new Console({ stdout: output, stderr: errorOutput });
// use it like console
const count = 5;
logger.log('count: %d', count);
// In stdout.log: count 5

The global console is a special Console whose output is sent to process.stdout and process.stderr. It is equivalent to calling:

new Console({ stdout: process.stdout, stderr: process.stderr });