winstonjs / winston

A logger for just about everything.
http://github.com/winstonjs/winston
MIT License
22.81k stars 1.81k forks source link

Feature request - Unique identifier for logs in formatter #1030

Closed roydondsouza closed 6 years ago

roydondsouza commented 7 years ago

Feature request: Can we add unique identifier for logs using formatters/processors? This would be a good addition Kindly point me to the relevant documentation if this feature is already present.

Objective: Is to identify a log set of one full flow of request, helps us query all logs for a session of each user of each API endpoint.

Example:

All Logs:

{ "level": "info", "uid": 6sdf867, message: "transaction start" }
{ "level": "info", "uid": 3298080, message: "transaction start" }
{ "level": "info", "uid": 6sdf867, message: "transaction with card number 123456****2345 successful." }
{ "level": "info", "uid": 6sdf867, message: "transaction completed, TDR values calculation started" }
{ "level": "info", "uid": 3298080, message: "transaction  with card number 999999***99999 failed" }
{ "level": "info", "uid": 6sdf867, message: "transaction TDR values calculation complete" }

Query uid 6sdf867:

{ "level": "info", "uid": 6sdf867, message: "transaction start" }
{ "level": "info", "uid": 6sdf867, message: "transaction with card number 123456****2345 successful." }
{ "level": "info", "uid": 6sdf867, message: "transaction completed, TDR values calculation started" }
{ "level": "info", "uid": 6sdf867, message: "transaction TDR values calculation complete" }

Query uid 3298080:

{ "level": "info", "uid": 3298080, message: "transaction start" }
{ "level": "info", "uid": 3298080, message: "transaction  with card number 999999***99999 failed" }

Reference: I have used this feature in a PHP logger - Monolog ... using processors using the UidProcessor

roydondsouza commented 7 years ago

@indexzero, @FotoVerite, @Marak can you help me out here?

ne00346536 commented 7 years ago

Hi, Anyone has implemented this for finding differentiate/ unique identifier logs of each user.?

roydondsouza commented 7 years ago

@ne00346536 we can add unique identifiers using middleware in php Monolog by doing something like this

Looking for a similar functionality here, I was not able to locate it in the documentation.

indexzero commented 6 years ago

This can be accomplished with using custom formats in winston@3.0.0. Please consider upgrading.

Specifically you would define a format to set a uid on each info object and then plumb through that uid for all other services your logs might end up at.

makstaks commented 5 years ago

@indexzero I wanted to clarify your comment. I added this custom format

const requestId = format((info, opts) => {
    if (opts.requestId) {
        info.uid = opts.requestId;
    }
    return info;
});

What do you mean by this statement, can you provide an example?

then plumb through that uid for all other services your logs might end up at.