我想创建自定义日志级别。创建是正确的,我可以在将来使用它们,但当我使用自定义关卡时,消息不会着色。正如我所看到的-颜色是在关卡之前添加的,这就是为什么我不能在自定义关卡中使用颜色。我的代码如下,当我使用warn或custom时-它崩溃并返回错误:
TypeError: colors[Colorizer.allColors[lookup]] is not a function还有调用的代码:
const Winston = require('./logger');
Winston.error('1 This is a info statement');
Winston.data('2 This is a warning statement');
Winston.info('3 This is a warning statement');
Winston.debug('4 This is a error statement');
Winston.verbose('6 This is a warning statement');
Winston.silly('7 This is a error statement');
Winston.warn('5 This is a debug statement');
Winston.custom('8 This is a error statement');和logger.js
const winston = require('winston');
const colorizer = winston.format.colorize();
const {
combine, timestamp, printf, simple,
} = winston.format;
const myCustomLevels = {
levels: {
error: 0,
warn: 1,
data: 2,
info: 3,
debug: 4,
verbose: 5,
silly: 6,
custom: 7,
},
colors: {
error: 'red',
warn: 'orange',
data: 'grey',
info: 'green',
debug: 'yellow',
verbose: 'cyan',
silly: 'magenta',
custom: 'blue',
},
};
colorizer.addColors(myCustomLevels.colors);
const logger = winston.createLogger({
colorize: true,
prettyPrint: true,
level: 'custom',
levels: myCustomLevels.levels,
format: combine(
simple(),
printf(
(msg) => {
return colorizer.colorize(
msg.level,
`${msg.level} - ${msg.message}`,
);
},
),
timestamp(),
),
transports: [
new winston.transports.Console(),
],
});
module.exports = logger;如何在自定义关卡中使用colorize?
发布于 2019-12-18 20:37:45
Usnig colorize:true会破坏你的自定义格式,如果你想给你所有的日志文本上色,你可以像这样手动完成:
const { combine, timestamp, label, printf } = winston.format;
const color = {
'info': "\x1b[36m",
'error': "\x1b[31m",
'warn': "\x1b[33m"
.
.
.
};
const myFormat = printf(({ level, message, label, timestamp }) => {
return `${level}: ${color[level] || ''} ${label} || ${timestamp} || ${message}\x1b[0m `;
});然后在createLogger函数中使用:
levels: myLevels,
format: combine(
winston.format.prettyPrint(),
winston.format.metadata(),
winston.format.json(),
label({ label }),
timestamp(),
myFormat
),
.
.
.https://stackoverflow.com/questions/59391618
复制相似问题