Plain file logging in winston

只谈情不闲聊 提交于 2019-12-11 03:58:21

问题


I want to log just the data and not log level, timestamp etc. to a file.

var logger = new (winston.Logger)({                                                                                     
  transports: [                                                                                                     
    new (winston.transports.File)({                                                                                 
        filename: '/tmp/data.log',                                                                                  
        json : false,                                                                                               
        timestamp : function() {                                                                                    
            return '';                                                                                              
        }                                                                                                           
    })                                                                                                              
  ]                                                                                                                 
}); 

logger.log('info', "a")

It removes the timestamp from the line but log level still appears. Currently, file contains "info: a". I want it to log just "a". Is it possible to specify output format in winston?


回答1:


Unfortunately, that formatting is sort of hardcoded into winston; you can see the logic for it in the log function of common.js, which is used by most of the default transports.

The way around this would be to write your own custom transport which doesn't rely on common.log().

An aside: you can just provide a timestamp: false option to disable timestamp logging in the default transports.




回答2:


You can define the custom log format like this

var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)({
timestamp: function() {
return Date.now();
},
formatter: function(options) {
// Return string will be passed to logger.
return options.timestamp() +' '+ options.level.toUpperCase() +' '+ (options.message ? options.message : '') + (options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );
}
})
]
});
logger.info('Data to log.');


来源:https://stackoverflow.com/questions/23257723/plain-file-logging-in-winston

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!