我碰巧使用bunyan来记录数据。我希望日志打印出适当的颜色,如红色错误,调试黄色等等;不幸的是我无论如何都找不到。现在我想知道它是否可能与温斯顿。我可以在winston中更改日志数据的颜色吗?
这是我执行的代码。
var logger = require("winston-color");
var winston = require('winston');
var util = require('util');
var logFilename = __dirname + '/logfile.log';
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.File)({
filename: 'logfile.log',
timestamp:true
}),
new (winston.transports.File)({
name: 'error-log',
filename: 'error.log',
level: 'error'
}),
new (winston.transports.File)({
name: 'info-log',
filename: 'info.log',
level: 'info'
}),
]
});
logger.info('Hello Winston info!');
logger.debug('Hello Winston debug!');
logger.warn('Hello Winston warn!');
logger.info('Hello again distributed logs');
logger.error('error1');
logger.error('error2');
输出屏幕截图here
在此工作代码的输出here
答案 0 :(得分:2)
如果您要寻找自定义的颜色架构,则可以这样编写自己的转运蛋白
import winston from "winston";
import Transport from "winston-transport";
const Colors = {
info: "\x1b[36m",
error: "\x1b[31m",
warn: "\x1b[33m",
verbose: "\x1b[43m",
};
class SimpleConsoleTransport extends Transport {
constructor() {
super();
}
log = (info, callback) => {
const { level, message, stack } = info;
console.log(
`${Colors[level]}${level}\t${message}\x1b[0m`,
stack ? "\n" + stack : ""
)
if (callback) {
callback();
}
};
}
然后以这种方式配置Winston实例:
winston.configure({
transports: [new SimpleConsoleTransport()],
});
这些是您可以使用的全部颜色列表:
Reset = "\x1b[0m"
Bright = "\x1b[1m"
Dim = "\x1b[2m"
Underscore = "\x1b[4m"
Blink = "\x1b[5m"
Reverse = "\x1b[7m"
Hidden = "\x1b[8m"
FgBlack = "\x1b[30m"
FgRed = "\x1b[31m"
FgGreen = "\x1b[32m"
FgYellow = "\x1b[33m"
FgBlue = "\x1b[34m"
FgMagenta = "\x1b[35m"
FgCyan = "\x1b[36m"
FgWhite = "\x1b[37m"
BgBlack = "\x1b[40m"
BgRed = "\x1b[41m"
BgGreen = "\x1b[42m"
BgYellow = "\x1b[43m"
BgBlue = "\x1b[44m"
BgMagenta = "\x1b[45m"
BgCyan = "\x1b[46m"
BgWhite = "\x1b[47m"
答案 1 :(得分:1)
我遵循了this的答案,并使其适用于颜色
new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
)
});
答案 2 :(得分:1)
您可以像其他答案中提到的那样为文本使用颜色。
但是您可以改用表情符号!例如,您可以将⚠️
用于警告消息,将?
用于错误消息。 (即使没有记录器!但是您也可以将其包装在记录器中,以进行访问控制)
或者只是将这些笔记本用作颜色:
console.log('?: error message');
console.log('?: warning message');
console.log('?: ok status message');
console.log('?: action message');
console.log('?: canceled status message');
console.log('?: Or anything you like and want to recognize immediately by color');
此方法还可以帮助您快速直接在源代码中直接扫描并查找日志。
而且,它不依赖于任何框架,您可以在任何地方自由使用它。
但是Linux默认的emoji字体默认情况下不是彩色的,您可能首先要使其彩色。
答案 3 :(得分:0)
我没试过这个。但根据这个你使用颜色的日志。 enter link description here
答案 4 :(得分:0)
是的,你可以。您可以使用我在项目中使用的以下代码。
<强>记录器/ WinstonPlugin.js 强>
/* jslint node: true */
/* jshint esversion: 6 */
'use strict';
const Winston = require('winston');
const logLevel = 'debug';
var logger;
(function createLogger() {
logger = new(Winston.Logger)({
transports: [
new(Winston.transports.Console)({
level: logLevel,
colorize: true,
timestamp: function () {
return (new Date()).toLocaleTimeString();
},
prettyPrint: true
})
]
});
Winston.addColors({
error: 'red',
warn: 'yellow',
info: 'cyan',
debug: 'green'
});
})();
module.exports = logger;
任何时候你需要任何代码文件中的Winston。您可以访问如下:
const Winston = require('logger/WinstonPlugin');
Winston.info('This is a info statement');
Winston.debug('This is a debug statement');
Winston.debug('This is a warning statement');
Winston.error('This is a error statement');
您将在控制台中看到彩色输出。
答案 5 :(得分:0)
试过你的修复;温斯顿不注意任何颜色设置。
答案 6 :(得分:-3)
为什么不使用log4js呢?这是一个完整的例子:
/* logger.js (tested with log4js 2.5.3) */
const log4js = require('log4js')
const logger = function(topic) {
const f = `logs/${topic}-${new Date().toISOString().substr(0, 10)}.log`
log4js.configure({
appenders: {
out: { type: 'stdout'},
[topic]: {
type: 'file', filename: f, maxLogSize: 128*1024*1024, backups: 10, compress: false
}
},
categories: {
default: { appenders: ['out', topic], level: 'debug' }
}
})
return log4js.getLogger(topic)
}
module.exports=logger
/* app.js */
const logger = require('./logger')('some-topic')
logger.info('loaded')
logger.warn('no such command')
logger.error('oops some problem')