winston version?_winston@2winston@3 node -v outputs:_ v8.11.4Errors should not be appended to the message property.
const {createLogger, format, transports} = require('winston');
const logger = createLogger({
level: 'info',
format: format.printf(info => `${info.level}: ${info.message}`),
transports: [new transports.Console()],
exitOnError: false
});
logger.error('Error Message:', new Error('This is an Error Log'));
logger.info('Object Message:', {a: 'lamb', b: 'cow'});
// results in
// error: Error Message:This is an Error Log
// info: Object Message:
Errors should not be forced onto the message property, thats what format.errors() is for.
If I want to get a stack trace for errors formatted properly I would do:
const {createLogger, format, transports} = require('winston');
const logger = createLogger({
level: 'info',
format: format.printf(info => `${info.level}: ${info.message} ${info.stack || ''}`),
transports: [new transports.Console()],
exitOnError: false
});
logger.error('Error Message:', new Error('This is an Error Log'));
logger.info('Object Message:', {a: 'lamb', b: 'cow'});
// results in
// error: Error Message:This is an Error Log Error: This is an Error Log
// <stack trace>
// <stack trace>
// <stack trace>
// <stack trace>
// <stack trace>
// <stack trace>
// <stack trace>
// info: Object Message:
I would expect the error message to only get appended to the message if you use format.errors().
Looks like #1664 is trying to fix this. In the meantime, my solution is to use a custom formatter that "undos" that concatenation:
const {format} = require('winston');
const fixErrors = format(info => {
// Only modify the info it there was an error
if (info.stack === undefined) {
return info;
}
const {message} = info;
// Get the original error message
const errorMessage =
info[Symbol.for('splat')] &&
info[Symbol.for('splat')][0] &&
info[Symbol.for('splat')][0].message;
// Check that the original error message was concatenated to the message
if (
errorMessage === undefined ||
message.length <= errorMessage.length ||
!message.endsWith(errorMessage)
) {
return info;
}
// Slice off the original error message from the log message
info.message = message.slice(0, errorMessage.length * -1);
return info;
});
module.exports = fixErrors;
And finally, use it like so:
const winston = require('winston');
const fixErrors = require('./fix-errors');
const logger = winston.loggers.add('myLogger', {
format: winston.format.combine(winston.format.splat(), fixErrors(), winston.format.printf(info => `${info.level.toUpperCase()} ${info.message} ${info.stack === undefined ? '' : info.stack}`)),
transports: coreTransports,
});
const error = new Error('something bad happened');
logger.error('was doing this and', error);
Resulting in:
ERROR was doing this and Error: something bad happened <rest of the stack>
I ran into this problem today, and created the following formatter to un-concatenate the messages:
const messageFormat = format(info => {
if (info.level == "error") {
const errorMessage = info[Symbol.for("splat")][0].message;
info.message = info.message.replace(errorMessage, "");
}
return info
})
Most helpful comment
Looks like #1664 is trying to fix this. In the meantime, my solution is to use a custom formatter that "undos" that concatenation:
And finally, use it like so:
Resulting in: