-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error.message being appended to info.message #1660
Comments
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:
|
This was referenced Oct 31, 2019
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
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please tell us about your environment:
winston
version?winston@2
winston@3
node -v
outputs: v8.11.4What's the problem?
Errors should not be appended to the message property.
What do you expect to happen instead?
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:
Problem: error.message gets printed twice because its on the stack and the message
I would expect the error message to only get appended to the message if you use format.errors().
The text was updated successfully, but these errors were encountered: