Node.js 新的 Console() 方法
控制台模块提供了一个简单的调试控制台,由导出两个特定组件的 Web 浏览器提供:
- 一个控制台类,可用于写入任何 Node.js 流。示例: console.log() 、 console.error()等。
- 无需导入控制台即可使用的全局控制台。示例: process.stdout、process.stderr等。
新的 Console() (在 v8.0.0 中添加)方法是“console”模块的内置应用程序编程接口,它创建一个具有单个或多个可写流实例的新控制台,其中stdout是可写流, stderr用于警告或错误输出。如果未提供stderr ,则 stdout 用于stderr 。它是一个控制台,其输出被发送到process.stdout和process.stderr 。
注意:全局控制台方法既不是始终同步也不是始终异步。
句法:
new Console(options);
论据:
const options = {
stdout: writableStream,
stderr: writableStream,
ignoreErrors: true,
colorMode:true
};
为了使用这个方法,我们需要使用( new Console() )方法创建一个控制台,我们需要导入' console '和' fs '模块。
const console = require('console');
const fs = require('fs');
参数:此函数接受一个对象/参数列表,如上所述和如下所述:
options < Object > :它可能在选项对象中有以下元素。
- stdout < stream.Writable > :它接受从 fs 模块导入的写入流。
- stderr < stream.Writable > :它还接受从 fs 模块导入的写入流。
- ignoreErrors < boolean > :传递的默认值为true 。它在写入底层流时忽略错误。
- colorMode <布尔值> | < 字符串 > :传递的默认值是' auto '。它用于设置仅支持此控制台实例的颜色。它可以根据设置的颜色模式设置为真、假或“自动”。
- inspectOptions < Object > :它指定传递给util.inspect()的选项 方法。
- groupIndentation < number > :默认值设置为2。用于设置组缩进。
返回值:它的输出被发送到由 fs 模块通过
下面的示例说明了在 Node.js 中使用新的 Console(options)方法。
示例 1:文件名:index.js
javascript
// Node.js program to demonstrate the
// new Console() method
// Using require to access fs module
const fs = require('fs');
// Using require to access console module
const { Console } = require('console');
// Creating write Stream
const output = fs.createWriteStream('./out.log');
const errorOutput = fs.createWriteStream('./err.log');
//
const options = { stdout: output, stderr: errorOutput,
ignoreErrors: true, colorMode: false };
const logger = new Console(options);
const count = 5;
// Using like console
logger.log('count: %d', count);
console.log("Successfully created and logged via console...", )
javascript
// Node.js program to demonstrate the
// new Console() method
// Using require to access fs module
const fs = require('fs');
// Using require to access console module
const console = require('console');
const { Console } = console;
try {
// Creating write Stream
const output = fs.createWriteStream('./outputlog.txt');
const error = fs.createWriteStream('./errlog.txt');
// Creating new Console
const objLogger = new Console(
{ stdout: output, stderr: error,
ignoreErrors: true, colorMode: true }
);
// Custom write Stream
const outt = fs.createWriteStream('./output.log');
const err = fs.createWriteStream('./error.log');
// Another way to create console
// (default values are passed to options)
const logObject = new console.Console(outt, err);
// Creating family object
var family = {};
family.Head = 'Miss Sanno';
family.headDesignation = 'Teacher';
family.Member1 = 'Miss Sanchi';
family.member1Designation = 'Kid';
family.Member2 = 'Master Amit';
family.member2Designation = 'Student';
// Creating constant value count
const count = 25+75*5-5/2;
// Writing via console
objLogger.log('Family: %s', family);
// Printing Family Object to console
console.log('Family Stream Created: ', family);
// Writing via console
logObject.log('Count: %s', count);
// Printing count to console
console.log('Count Stream Created: ', count);
// console.log(logObject.family.error)
}
catch {
console.error(new Error(
'Oops, some error happened in family...'));
// Prints: [Error: Oops, some error
// happened in family...], to stderr
}
使用以下命令运行index.js文件:
node index.js
输出:
Note: The above node.js example will create log files (out & err) in the same folder where index.js file exists.
>> Successfully created and logged via console…
示例 2:文件名:index.js
javascript
// Node.js program to demonstrate the
// new Console() method
// Using require to access fs module
const fs = require('fs');
// Using require to access console module
const console = require('console');
const { Console } = console;
try {
// Creating write Stream
const output = fs.createWriteStream('./outputlog.txt');
const error = fs.createWriteStream('./errlog.txt');
// Creating new Console
const objLogger = new Console(
{ stdout: output, stderr: error,
ignoreErrors: true, colorMode: true }
);
// Custom write Stream
const outt = fs.createWriteStream('./output.log');
const err = fs.createWriteStream('./error.log');
// Another way to create console
// (default values are passed to options)
const logObject = new console.Console(outt, err);
// Creating family object
var family = {};
family.Head = 'Miss Sanno';
family.headDesignation = 'Teacher';
family.Member1 = 'Miss Sanchi';
family.member1Designation = 'Kid';
family.Member2 = 'Master Amit';
family.member2Designation = 'Student';
// Creating constant value count
const count = 25+75*5-5/2;
// Writing via console
objLogger.log('Family: %s', family);
// Printing Family Object to console
console.log('Family Stream Created: ', family);
// Writing via console
logObject.log('Count: %s', count);
// Printing count to console
console.log('Count Stream Created: ', count);
// console.log(logObject.family.error)
}
catch {
console.error(new Error(
'Oops, some error happened in family...'));
// Prints: [Error: Oops, some error
// happened in family...], to stderr
}
使用以下命令运行index.js文件:
node index.js
输出:
Note: The above node.js example will create log files (output & error) in the same folder where index.js file exists.
Family Stream Created: {
Head: ‘Miss Sanno’,
headDesignation: ‘Teacher’,
Member1: ‘Miss Sanchi’,
member1Designation: ‘Kid’,
Member2: ‘Master Amit’,
member2Designation: ‘Student’
}
Count Stream Created: 397.5
参考: https://nodejs.org/api/console.html#console_new_console_options