winston自定义日志管理
2021-02-16 11:21
标签:col error moment gif export file function 分享 自定义日志 logger.js 使用: 查看: winston自定义日志管理 标签:col error moment gif export file function 分享 自定义日志 原文地址:https://www.cnblogs.com/maoriaty/p/8409989.htmlconst winston = require(‘winston‘);
const moment = require(‘moment‘);
const config = require(‘../conf/config‘);
let customLevels = {
levels: {
debug: 0,
info: 1,
warn: 2,
error: 3
},
colors: {
debug: ‘blue‘,
info: ‘green‘,
warn: ‘yellow‘,
error: ‘red‘
}
};
// create the main logger 等级高的包含等级低的输出日志
module.exports = logger = new (winston.Logger)({
level: ‘debug‘,
levels: customLevels.levels,
transports: [
new (winston.transports.Console)({
level: ‘error‘,
levels: customLevels.levels,
timestamp: function(){return moment().format(‘YYYY-MM-DD HH:mm:ss‘)},
colorize: true,
silent: config.logStdout //true关闭,false打开
}),
new (winston.transports.File)({
name: ‘info‘,
filename: ‘./logs/info.log‘, //项目根目录
maxsize: 1024 * 1024 * 50, //50M
level: ‘info‘,
levels: customLevels.levels,
timestamp: function(){return moment().format(‘YYYY-MM-DD HH:mm:ss‘)},
json: false
})
]
})
winston.addColors(customLevels.colors);
#引入
const logger = require(‘./util/logger‘);
#输出
logger.debug(‘事件消息:‘ + JSON.stringify(message));
logger.info(‘事件消息:‘ + JSON.stringify(message));
logger.warn(‘事件消息:‘ + JSON.stringify(message));
logger.error(‘事件消息:‘ + JSON.stringify(message));