I'm trying to log stacktrace along with error message using Winston. My logger is configured with custom formatter:
this.errorLogger = winston.createLogger({
levels: this.levels,
level: 'error',
transports: [
new WinstonFileRotator({
filename: '%DATE%.log',
dirname: 'logs/error',
zippedArchive: true,
maxSize: '20m',
maxFiles: '14d',
handleExceptions: true,
json: false,
format: winston.format.combine(
winston.format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
winston.format.printf(info => {
return '[${info.timestamp}] -> ${info.message}';
}),
),
})
]
});
I log the error along with stacktrace:
this.errorLogger.error('My message', ex.Stack);
In my log I have a line:
[2018-09-03 23:41:14] -> My message
How can I access in my custom formatter the metadata that I passed to error
function along with the message?
I have been working on a similar issue. In the end, I did:
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.label({ label: 'MY-SILLY-APP' }),
winston.format.timestamp(),
winston.format.metadata({ fillExcept: ['message', 'level', 'timestamp', 'label'] }),
winston.format.colorize(),
winston.format.printf(info => {
let out = `${info.timestamp} [${info.label}] ${info.level}: ${info.message}`;
if (info.metadata.error) {
out = out + ' ' + info.metadata.error;
if (info.metadata.error.stack) {
out = out + ' ' + info.metadata.error.stack;
}
}
return out;
}),
),
transports: [
new winston.transports.Console()
]
});
logger.info('running');
try {
throw new Error('failed');
} catch (err) {
logger.error('failing', { error: err });
}
logger.info('stopping');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With