Skip to content

multiple transports loosing SPLAT/meta #1430

@WhiteTomX

Description

@WhiteTomX

Please 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:

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions