new Console(options)


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

使用一个或两个可写流实例创建新的 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 false disables coloring while inspecting values. Setting to 'auto' makes 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().
    • groupIndentation <number> Set group indentation. Default: 2.

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 });