-
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
logger.info({message:...}) modifies original object #1775
Comments
I have the same question. |
I have the same issue using Given: class CustomError extends Error {
level;
constructor(message, level) {
super(message);
this.level = level
}
}
const win = winston.createLogger({
transports: [
new winston.transports.Console(),
]
});
const error = new CustomError("Wrong level", "fatal");
win.error(error); // {"level":"error"}
console.log(error.level); // error If |
As the original poster guessed, this caused a very hard-to-find bug for myself. I could never have imagined that the logger I'm using would modify the object I've passed to it, so it was the last place I looked to resolve an issue. Winston should not be doing this! |
I also just discovered this bug. We rely on the |
I have a fix at #1938 ...but it needs to be updated to handle deep cloning of Error objects and other types. JS is not my primary language, so I am open to ideas here. The quick solution is to use Lodash, or another library, but I'm not sure how open the maintainers are to adding such a dependency. |
I also came across this defect as I get optional "message" property from external API. When "message" returned is empty, this issue disappears so it was nasty to track where my problem is from. To me, response (res.data) from axios service call modified by winston logger should be fixed ASAP. I see @clintonb PR fixes the issue of data object mutated at least, (the object logged still has the issue though as I use json format). Having this workaround is bad, please help by reviewing the PR and merging it.
|
Experience the same thing that the logger.error method modifies the message of the error object directly to pad 3 spaces in front. const error = new Error('No!');
// before
console.log(error.message.length); // 3 - 'No!'
// after
logger.error(error);
console.log(error.message.length); // 6 - ' No!' |
Wow - this does seem like something that should be fixed, though it also seems like a fair amount of work to do it well without having a pretty large impact on performance (which is important for a logging library). I think the best strategy here is go through an analysis of the code to find places where that error object getting logged is mutated, and try to split the mutated thing from the original and pass them alongside each other, copying only where really necessary e.g. for passing into other functions that can't take side-by-side parameters. I don't personally have any time budget to do that right now, but am pinning this issue so that folks can be more aware of it, as lack of awareness/expectation of this behavior is a key reason why it's so frustrating. |
I am still facing the same issue on winston version 3.8.2
|
Still having shallow copy issue. |
best succinct example! This was my expectation, and spent a bunch of time figuring out the actual behavior, and finding this issue. Given that changing established behavior might be a significant breaking change, if there were an option in format.json() like ignoreMessage: true, or some such - that would solve the problem - given the docs mentioned it. |
I'm facing a similar issue where the log output seems to append a second JSON object to my log message. It adds an extra The interesting thing is that I have this implemented in two apps with identical implementations and package versions. One app outputs correctly to grafana and I can prettify the logs for better readability. The other app outputs the message with that extra JSON object and it breaks the prettify feature in Grafana and makes the logs really unreadable.
|
Problem description:
.info()
, and probably other methods modify original object when the object contains 'message' field.This is unfortunate because a user might be unaware of such behaviour, and can easily overlook this moment, which would lead to hard-to-find problems.
Possible fix:
Leave everything as it is (as user expect to pass config object to the logging method), but clone incoming object.
The text was updated successfully, but these errors were encountered: