logger.js
const 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));
查看: