Установка winston
npm i winston~3.8.2
Расширение стандартного транспорта
const winston = require('winston');
const Transport = require('winston-transport');
export class MyTransport extends Transport {
log(info, callback) {
const { foo, bar } = info.message;
this.emit(
'pre-logged',
{
foo,
bar,
},
{
sendImmediately: false,
},
);
try {
callback();
setImmediate(() => {
this.emit(
'logged',
{
foo,
bar,
},
{
sendImmediately: false,
},
);
});
} catch (error) {
callback(error.message, {
sendImmediately: false,
});
}
}
};
Пример использования расширенного транспорта
const myTransport = new MyTransport();
myTransport.on('pre-logged', async (data) => {
console.log('pre-logged', data);
});
myTransport.on('logged', async (data) => {
console.log('logged', data);
});
const myLogger = winston.createLogger({
transports: [myTransport],
});
myLogger.on('error', async (message) => {
console.error(message);
});
myLogger.info({
foo: 'Foo',
bar: 'Bar',
});