new Console(options)


  • options <Object>

    • stdout <stream.Writable>

    • stderr <stream.Writable>

    • ignoreErrors <boolean> 写入底层流时忽略错误。默认值:true

      ¥ignoreErrors <boolean> Ignore errors when writing to the underlying streams. Default: true.

    • colorMode <boolean> | <string> 为此 Console 实例设置颜色支持。设置为 true 可在检查值时进行着色。设置为 false 会在检查值时禁用着色。设置为 'auto' 使颜色支持取决于 isTTY 属性的值和 getColorDepth() 在相应流上返回的值。如果同时设置了 inspectOptions.colors,则无法使用此选项。默认值:'auto'

      ¥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> 指定传给 util.inspect() 的选项。

      ¥inspectOptions <Object> Specifies options that are passed along to util.inspect().

    • groupIndentation <number> 设置组缩进。默认值:2

      ¥groupIndentation <number> Set group indentation. Default: 2.

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

¥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 

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

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