multiple transports loosing SPLAT/meta
See original GitHub issuePlease tell us about your environment:
winston
version?-
winston@2
-
winston@3
-
- _
node -v
outputs: v8.11.3 - _Operating System? Windows
- _Language? Typescript
What is the problem?
Adding multiple transports to a logger causes the info-object to loose meta field on all but one transport.:
const Winston = require("winston");
Winston.add(new Winston.transports.Console({
format: Winston.format.combine(Winston.format(info => {
info.message = info.message + "Console";
return info;
})(), Winston.format.splat(), Winston.format.simple(), Winston.format(info => {
// console.log(info); // meta is there!
return info;
})()),
handleExceptions: true
}));
Winston.add(new Winston.transports.File({
filename: "test.log",
format: Winston.format.combine(Winston.format(info => {
// console.log(info); // meta is gone :(! and so is SPLAT only a array of length 1
return info;
})(), Winston.format.splat(), Winston.format.simple())
}));
for (let index = 0; index < 10; index++) {
Winston.error("test %s" + index, "blub", "metainfo");
}
Output Console:
error: test blub0Console {"meta":"metainfo"}
error: test blub1Console {"meta":"metainfo"}
error: test blub2Console {"meta":"metainfo"}
error: test blub3Console {"meta":"metainfo"}
error: test blub4Console {"meta":"metainfo"}
error: test blub5Console {"meta":"metainfo"}
error: test blub6Console {"meta":"metainfo"}
error: test blub7Console {"meta":"metainfo"}
error: test blub8Console {"meta":"metainfo"}
error: test blub9Console {"meta":"metainfo"}
Output File:
error: test blub0
error: test blub1
error: test blub2
error: test blub3
error: test blub4
error: test blub5
error: test blub6
error: test blub7
error: test blub8
error: test blub9
Sometimes one transport gets the meta and the other doesn’t and for the next logentry its the other way around.
What do you expect to happen instead?
Each transport should get a clean “new” info object. At least with a full SPLAT. So {“meta”:“metainfo”} is included in File too.
Other information
I tried for a moment to find the issue in debugger, but got lost in the streams. So please excuse me, if this issue belongs more to winston-transports. I try to investigate this further.
It seems the info[SPLAT] is preserved from one Transport to the other, but due to the splat() formatter th metadata is removed. Maybe its more related to the splat() altering the array than i thought…
As a workaround i will push the metadata back to SPLAT after splat():
const splatWorkaround = Winston.format.combine(
Winston.format.splat(),
Winston.format(info => {
if (info.meta && info.meta instanceof Array) info[SPLAT].push(...info.meta);
else if (info.meta) info[SPLAT].push(info.meta);
return info;
})()
);
To get this working i will use this workaround:
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:8 (2 by maintainers)
Top GitHub Comments
I was about to open a new issue regarding this exact same issue, and then I found this one. I’ll post my description as well. 😃
Please tell us about your environment:
winston
version?winston@3.1.0
node -v
outputs:v8.12.0
What is the problem?
The custom data added by the
splat
formatter as the fieldmeta
in the log message is dropped when all the following conditions are met:%s
)format
is applied on each transportTo replicate the bug, see the code:
https://gist.github.com/avaly/527eee1e061accb7e1f446ca105c3094#file-run-with-bug-js
Which outputs:
What do you expect to happen instead?
Expected output:
Other information
If we move the custom
format
to the top level logger, then the bug does not present itself:https://gist.github.com/avaly/527eee1e061accb7e1f446ca105c3094#file-run-without-bug-js
But, most of the time, different transports do require different formatting.
i think i figured out a workaround for now. You can try adding this to your scripts to make the transport use a “deepclone” instead of making shallow clone
you can try this as a workaround for now: