【发布时间】:2015-08-05 11:58:35
【问题描述】:
我决定使用winston 登录我的node 项目。我正在 windows 平台上开发这个应用程序,但最终它会在 linux 平台上。
这是我用来配置记录器的代码。
/* Custom logging module
* to write appropriate logs into log files as well as console.
*/
var winston = require( 'winston' );
winston.setLevels({
info: 0,
error: 1,
warning: 2,
audit: 3
});
winston.addColors({
info: 'blue',
error: 'red',
warning: 'yellow',
audit: 'green'
});
var logger = new ( winston.Logger )({
levels: {
info: 0,
error: 1,
warning: 2,
audit: 3
},
transports : [
new (winston.transports.Console)({
level: 'info',
prettyPrint: true,
colorize: true,
silent: false,
timestamp: true
}),
new (winston.transports.File)({
name : 'infoLogger',
filename : './logs/info-log.log',
prettyPrint: false,
level: 'info',
silent: false,
colorize: true,
timestamp: true,
maxsize: 40000,
maxFiles: 10,
json: false,
tailable : true
}),
new (winston.transports.File)({
name : 'errorLogger',
filename : './logs/error-log.log',
prettyPrint : false,
level : 'error',
silent : false,
colorize : true,
timestamp : true,
maxsize : 40000,
maxFiles : 10,
json : false,
tailable : true
}),
new (winston.transports.File)({
name : 'warningLogger',
filename : './logs/warning-log.log',
prettyPrint : false,
level : 'warning',
silent : false,
colorize : true,
timestamp : true,
maxsize : 40000,
maxFiles : 10,
json : false,
tailable : true
}),
new (winston.transports.File)({
name : 'auditLog',
filename : './logs/audit-log.log',
prettyPrint : false,
level : 'audit',
silent : false,
colorize : true,
timestamp : true,
maxsize : 40000,
maxFiles : 10,
json : false,
tailable : true
})
],
colors: {
info: 'blue',
error: 'red',
warning: 'yellow',
audit: 'green'
}
});
module.exports = logger;
我正在使用闲散代码登录文件。
logger.info( 'Winstom Log Info' );
logger.error( 'Winstom Log Error' );
logger.warning( 'Winstom Log Waring' );
logger.audit( 'Winstom Log Audit' );
但是,记录的消息中没有使用颜色。并且记录的文件并没有完全记录它们自己的级别。例如,
error-log.log 包含闲散消息。
2015-05-23T13:34:32.479Z - error: Winstom Log Error
2015-05-23T13:34:32.480Z - warning: Winstom Log Waring
2015-05-23T13:34:32.481Z - audit: Winstom Log Audit
2015-05-23T13:45:33.433Z - error: Winstom Log Error
2015-05-23T13:45:33.436Z - warning: Winstom Log Waring
2015-05-23T13:45:33.436Z - audit: Winstom Log Audit
2015-05-23T13:56:50.660Z - error: Winstom Log Error
2015-05-23T13:56:50.661Z - warning: Winstom Log Waring
2015-05-23T13:56:50.661Z - audit: Winstom Log Audit
2015-05-23T14:00:04.319Z - error: Winstom Log Error
2015-05-23T14:00:04.319Z - warning: Winstom Log Waring
2015-05-23T14:00:04.319Z - audit: Winstom Log Audit
错误日志也正在赶上警告日志和审计日志。 警告日志也是如此,它也在追赶审计日志的日志。
2015-05-23T13:34:32.480Z - warning: Winstom Log Waring
2015-05-23T13:34:32.481Z - audit: Winstom Log Audit
2015-05-23T13:45:33.436Z - warning: Winstom Log Waring
2015-05-23T13:45:33.436Z - audit: Winstom Log Audit
2015-05-23T13:56:50.661Z - warning: Winstom Log Waring
2015-05-23T13:56:50.661Z - audit: Winstom Log Audit
2015-05-23T14:00:04.319Z - warning: Winstom Log Waring
2015-05-23T14:00:04.319Z - audit: Winstom Log Audit
目前,只有审核日志和信息日志按预期工作。审计只包含审计日志和包含每一个的信息日志。
是什么导致了这个错误? 还请告诉我有关日志记录的任何最佳做法。
【问题讨论】: