Reputation: 2844
I would like to log messages from a nodejs service into the Windows Event Log.
I found winston-winlog and winston-winlog2 modules, but they all use old winston version.
How to make a custom Transport for Winston3 to log in the Windows Event Log ?
Upvotes: 1
Views: 924
Reputation: 2844
Here is how I made thanks to node-windows module :
const Transport = require('winston-transport');
const WinEventLogger = require('node-windows').EventLogger;
class WinstonWinlogTransport extends Transport {
constructor(opts) {
super(opts);
this.logger = new WinEventLogger(opts);
}
log(info, callback) {
setImmediate(() => {
this.emit('logged', info);
});
let {message, level} = info ;
if ( level==='error' ) this.logger.error(message) ;
else if ( level==='warn' ) this.logger.warn(message) ;
else this.logger.info(message) ;
callback();
}
};
Then you can create transports and add them to your winston logger :
let myTransport = new WinstonWinlogTransport({ source: 'Label1' }) ; // Go to "Applications" logs
let myOtherTransport = new WinstonWinlogTransport({ source: 'Label2', eventLog: 'SYSTEM' }) ; // Go to "System" logs
Note that your nodejs service have to be installed with administrator privilleges. (To install nodejs code as a windows service, I used WinSW)
Upvotes: 2