samedi 21 février 2015

Nodejs Winston Emit to socket

I have implemented Winston to log my nodejs app. Currently I am writing the log to log files using the "DailyRotateFile" transport of Winston, and outputting the log data to the console.


I want to add an additional transport that emits the log message to a socket so that I can output it to the web interface I have created using Express, through socket.io.


This is the current way I have set up Winston:



var logger = new winston.Logger({
transports: [
new winston.transports.DailyRotateFile({
level: 'info',
filename: './Logs/server',
datePattern: '.yyyy-MM-dd.log',
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true
})
],
exitOnError: false
})


This is the code the I currently use to emit the log message:



var logStream = logger.stream({
start: -1
})

logStream.on('log', function(log){
console.log('LOG STREAM')
//console.log(log)
io.emit('logger', log);
})


This however continuously outputs "LOG STREAM" to the nodejs console when I start the server, and doesn't emit anything when I use the logger.info command.


EDIT So it looks like the reason the it continues to output "LOG STREAM" is because it is sending all the messages that have been saved in the log file, even though I set "start -1"


I would really appreciate any suggestions as how to correctly achieve this. TIA!


Aucun commentaire:

Enregistrer un commentaire