node-logger-plus
v0.2.7
Published
node console 有趣的打印 log print 打印颜色 style log 美化 带风格的日志
Downloads
45
Maintainers
Readme
打印日志
支持来 Commonjs 和 Esmodule
每次打印会返回一个Promise then 会把组装的结构已数组的结构返回
可以自行处理,或者写入日志文件
通过拓展运算符可以再次打印出带样式的log
暴露的函数
import { logger, Color, Logger } from 'node-logger-plus';
使用
import { logger } from 'node-logger-plus';
// const logger = require('node-logger-plus').logger;
logger.log('log', 'logger1')
logger.error('log', 'logger1')
logger.warn('log', 'logger1')
logger.info('log', 'logger1')
logger.debug('debug', 'logger1')
logger.success('log', 'logger1')
logger.table([{
id: 1,
name: 'logger1',
age: 30
}]);
控制打印时机
/**
* @desc 默认为true
* log: true,
debug: true,
info: true,
warn: true,
error: true,
table: true,
*/
// 设置对于字段false 或者true 控制是否打印日志
logger.setConfig({
debug: false, // 比如debug默认设置这个false 减少不必然的打印,在特定阶段再开启打印
});
Data
- 如果你需要统一捕获打印的数据,你可以通过 on 来实现 内部通过Bus class 实现
logger.on('data', function (data) {
console.log(...data.result, 'bus on data')
});
Color
- 比如希望打印一个有颜色的字体你可以这样:
import { logger, Color } from 'node-logger-plus';
/**
* @desc 默认内置以下颜色
red
green
yellow
blue
magenta
cyan
white
*/
console.log(Color.green(msg));
// 或者
logger.log(Color.green(msg));
- 如果你希望预设一些颜色,你可以这样
import { colors } from 'node-logger-plus';
// 例子
colors['red'] = "\x1b[31m";
colors['green'] = "\x1b[32m";
BusPlus
- 用法和mitter方向保持一致
- logger 内部通过继承 BusPlus 拥有 bus能力
- https://www.npmjs.com/package/bus-plus
import { BusPlus } from 'bus-plus';
const mitter = new BusPlus();
mitter.on('message', function(data) {
console.log(data); // test data
});
mitter.once('test', function(data) {
});
// 支持链式调用
mitter.emit('message', 'test data')
.off('message');