最簡單的方法是更改日誌格式。這允許你創建幾乎相同的輸出,但是例如你可以將你的關卡名稱映射到數字。
您必須在傳輸中禁用json。在下面的示例中,我禁用了它,但我手動將其格式化爲json。
例如:
this.logger.add(winston.transports.File, {
name:'log.info',
level: 'info',
filename: path.join(logPath, 'info.log'),
json: false, // this is important
maxsize: 5242880,
maxFiles: 5,
colorize: false,
formatter: function(options) { //formatter function
var map = { //add rest of levels with values
info: 6,
notice: 5
}
//to check all options
console.log(options); //then you know what props you can use to create desired output
//because we turn off json we have to manually create it
return JSON.stringify({
level: map[options.level],//I map level
message: options.message
});
}
});
這將在日誌文件中返回:
{ 「級」:6, 「消息」: 「一些信息」}
你可以閱讀更多關於它在winston文檔中(頁面底部「自定義日誌格式」部分)link