📜  Node.js 自定义控制台类

📅  最后修改于: 2022-05-13 01:56:20.682000             🧑  作者: Mango

Node.js 自定义控制台类

Console 类可用于创建具有可配置输出流的记录器,基本用法在 Node.js 的新 Console() 方法中进行了描述。但正如您可能观察到的,某些选项只允许布尔值作为输入。

例如,当您有各种输出数据时,启用colorMode很有用。 Node.js 提供了一个非常方便的设置来显示控制台日志。但是,在某些情况下,您可能仍希望在不安装任何其他软件包的情况下对其进行修改以满足您的需要。

在本文中,我们将首先介绍如何使用inspectOptions进行Console类的其他一些高级设置,然后解释如何修改colorMode细节。

示例 1:使用默认的 Console 类。它展示数据的方式与全局控制台相同,即console.log

index.js
const { Console } = require('console');
  
const logger = new Console({
    stdout: process.stdout,
    stderr: process.stderr,
});
  
logger.log('log: object', {attr: 
    'string content a b c d e f g h i j k'});
logger.log('log: array', ['array_value1', 'array_value2', 
'array_value3', 'array_value4', 'array_value5']);
logger.log('log: set', new Set([3, 1, 2, 5, 4]));


index.js
const { Console } = require('console');
  
const logger = new Console({
    stdout: process.stdout,
    stderr: process.stderr,
    inspectOptions: {
  
        // Maximum number of Array elements 
        // to include when formatting.
        maxArrayLength: 3, 
  
        // Maximum number of characters to 
        // include when formatting.
        maxStringLength: 10, 
  
        // If properties of an object are sorted
        sorted: true,
    }
});
  
logger.log('log: object', {attr: 
        'string content a b c d e f g h i j k'});
logger.log('log: array', ['array_value1', 'array_value2', 
'array_value3', 'array_value4', 'array_value5']);
logger.log('log: set', new Set([3, 1, 2, 5, 4]));


index.js
const { Console } = require('console');
const util = require('util');
  
// This can be declared after the 
// logger is created, too.
util.inspect.styles.number = 'red';
  
const logger = new Console({
    stdout: process.stdout,
    stderr: process.stderr,
    inspectOptions: {
  
        // Remember to enable the color mode.
        // Default is false.
        colors: true
    }
});
  
// Now the number in the set are all shown in red.
logger.log('log: set', new Set([3, 1, 2, 5, 4]));


使用以下命令运行index.js文件:

node index.js

输出:

log: object { attr: 'string content a b c d e f g h i j k' }
log: array [
  'array_value1',
  'array_value2',
  'array_value3',
  'array_value4',
  'array_value5'
]
log: set Set(5) { 3, 1, 2, 5, 4 }

示例 2:如果我们没有足够的空间来展示所有内容并希望它更短且组织良好。我们将在inspectOptions对象中指定以下属性并将它们附加到控制台选项。

  • maxArrayLength:用于指定格式化时要包含的 Array 元素的最大数量。默认值:100。
  • maxStringLength:用于指定格式化时包含的最大字符数。默认值:10000。
  • sorted:如果设置为 true,则使用默认排序,如果设置为函数,则将其用作比较函数。

index.js

const { Console } = require('console');
  
const logger = new Console({
    stdout: process.stdout,
    stderr: process.stderr,
    inspectOptions: {
  
        // Maximum number of Array elements 
        // to include when formatting.
        maxArrayLength: 3, 
  
        // Maximum number of characters to 
        // include when formatting.
        maxStringLength: 10, 
  
        // If properties of an object are sorted
        sorted: true,
    }
});
  
logger.log('log: object', {attr: 
        'string content a b c d e f g h i j k'});
logger.log('log: array', ['array_value1', 'array_value2', 
'array_value3', 'array_value4', 'array_value5']);
logger.log('log: set', new Set([3, 1, 2, 5, 4]));

使用以下命令运行index.js文件:

node index.js

输出:

说明:如果你看一下inspectOptions接受的参数,有一个叫做colors 。此参数的优先级高于Console 类中的 colorMode 。如果设置了inspectOptions.colors ,则忽略colorMode 。同样,它只允许布尔值。也就是说,它接受真或假。这可能令人失望,但如果一次只能处理 1 个记录器,这是一种解决方法。

示例 3:我们可以将util.inspect.styles中的默认设置修改为下面的示例代码。 (这是每种数据类型可接受的所有值的列表)。请注意,这是一种修改全局配色方案的方法。应用更改后,启用颜色模式并遵循此配色方案的所有其他组件都会受到影响。

index.js

const { Console } = require('console');
const util = require('util');
  
// This can be declared after the 
// logger is created, too.
util.inspect.styles.number = 'red';
  
const logger = new Console({
    stdout: process.stdout,
    stderr: process.stderr,
    inspectOptions: {
  
        // Remember to enable the color mode.
        // Default is false.
        colors: true
    }
});
  
// Now the number in the set are all shown in red.
logger.log('log: set', new Set([3, 1, 2, 5, 4]));

使用以下命令运行index.js文件:

node index.js

输出:

log: set Set { 3, 1, 2, 5, 4 }

参考: https://nodejs.org/api/util.html#util_customizing_util_inspect_colors