var winston = require('winston'); var moment = require('moment'); var stackTrace = require('stack-trace'); var dateFormat=function() { return moment().format('YYYY-MM-DD HH:mm:ss:SSS'); }; var customFileFormatter = function (options) { var trace = stackTrace.get()[9]; var tmp = trace.getFileName(); var lastpos = (tmp.lastIndexOf('/')+1) || (tmp.lastIndexOf('\\')+1); var fileName = 'null'; lastpos--; if (lastpos > 0){ fileName = tmp.substring(lastpos+1,tmp.length); } var str = options.timestamp() +'|['+ options.level.toUpperCase() +'|file:'+fileName+'|fun:'+trace.getFunctionName() +'|line:'+trace.getLineNumber() +']|['+ (undefined !== options.message ? options.message : '') +']'+ (options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' ); return str; } var selfconfig = { init:false, path:'', level:'warn', }; module.exports = function(config){ var project=config.project||''; if (config.hasOwnProperty("path") || config.hasOwnProperty("level")) { selfconfig.init = true; selfconfig.path = config.path|| undefined; selfconfig.level = config.level || undefined; } if (selfconfig.init){ return new (winston.Logger)({ levels: { 'trace': 5, 'debug': 4, 'info': 3, 'warn': 2, 'error': 1, 'fatal': 0 }, transports: [ new (winston.transports.File)({ name: 'info-file', filename: selfconfig.path +'/'+project+'_'+ moment().format('YYYY-MM-DD') + '.log', json: false, level: selfconfig.level, maxsize: 1024 * 1024 * 100, maxFiles: 3000, tailable:true, colorize: true, timestamp: dateFormat, formatter: customFileFormatter }), ], exceptionHandlers: [ new winston.transports.File({filename: 'exceptions.log'}) ], exitOnError: false }); } else { return new (winston.Logger)({ levels: { 'trace': 5, 'debug': 4, 'info': 3, 'warn': 2, 'error': 1, 'fatal': 0 }, transports: [ new (winston.transports.File)({ name: 'info-file', filename: "./" +project+ moment().format('YYYY-MM-DD') + '.log', json: false, level: selfconfig.level, maxsize: 1024 * 1024 * 10, tailable:true, maxFiles: 3000, colorize: true, timestamp: dateFormat, formatter: customFileFormatter }), ], exceptionHandlers: [ new winston.transports.File({filename: 'exceptions.log'}) ], exitOnError: false }); } }
你打算打赏多少钱呢?
(微信扫一扫)