【发布时间】:2016-08-18 11:51:19
【问题描述】:
这是我设置的一个基本的 winston 记录器:
var winston = require('winston')
var systemLogger = new (winston.Logger)({
transports: [
new (winston.transports.File)({
name: 'system-file',
// log which stores all the errors
filename: './test.log',
json: true,
timestamp: true
})
]
})
systemLogger.log('info', 'hello')
systemLogger.log('error', '-----hello2')
这会创建一个这样的日志文件:
{"level":"info","message":"hello","timestamp":"2016-08-18T11:48:22.081Z"}
{"level":"error","message":"-----hello2","timestamp":"2016-08-18T11:48:22.083Z"}
但我想让它在文件中看起来像这样:
INFO: 2016-08-18T11:48:22.081Z hello
ERROR: 2016-08-18T11:48:22.083Z *** -----hello2 ***
这可能吗?
我读过这篇文章 - https://github.com/winstonjs/winston#custom-log-format 但我实际上无法找到如何做到这一点 - 如果可能的话。
我已尝试添加格式化程序:
formatter: function(options) {
// Return string will be passed to logger.
if (options.level === 'error') {
return options.timestamp() +' ******** '+ options.level.toUpperCase() +' ******** '+ (undefined !== options.message ? options.message : '')
}
else if (options.level === 'info') {
return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (undefined !== options.message ? options.message : '')
}
}
但这似乎只适用于控制台打印,不适用于文件。由于文件的保存方式与没有格式化程序的方式相同。
【问题讨论】:
-
我的答案有文件和控制台的例子。
标签: javascript node.js logging winston