winstonjs / winston

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

Adding http request headers information as part of winston logger #1088

Open 210178 opened 7 years ago

210178 commented 7 years ago

I am using Koa 2 framework and Winston logger for my development.

Question:

Whenever UI sends any request to back end (server), I will get the following information (along with other info) as part of my http request header.

  1. User Id,
  2. Correlation Id (which is a unique id for each URL request)

I would like to log the above information as part of my winston logger to track/analysis each of the URL request sent to my backend service. My tobe log should be,

Example log statement: info: "get Employee details", {userId: "test", correlationId: "123anbr343232"} info: "get Employee details", {userId: "dummy", correlationId: "453323anbr343232"}

FYI, this correlation id would be different for each URL request

Please let me know how can I achieve this...

stanleegoodspeed commented 6 years ago

@210178 did you every figure out how to do this?

edevil commented 6 years ago

I guess we need an equivalent to https://github.com/skonves/express-http-context for Koa where we can store request scoped information that can be retrieved in a Winston formatter.

jamesliupenn commented 4 years ago

@210178 @stanleegoodspeed @edevil were any of you able to get to the bottom of this? I have a koa context being passed from my frontend application to the backend whenever there's an interaction, but I'm in need of getting it to be added to my Winston logs.

Here's the detail:

const addHeader = Winston.format((info) => {
    info.headers = "Something from koa context";

    return info;
});

export const Logger: Winston.Logger = Winston.createLogger({
    levels: Winston.config.syslog.levels,
    format: Winston.format.combine(
        Winston.format.timestamp(),
        addHeader()
    ),
    transports: [
        new Winston.transports.Console({ 
            level: "info", 
            format: Winston.format.prettyPrint(),
        }),
        new Winston.transports.Console({ 
            level: "error",
            format: Winston.format.prettyPrint()
        }),
    ]
});

I want it set up that every time something logs, it will fetch that context value from koa and writes using the Winston logger.

edevil commented 4 years ago

@jamesliupenn I ended up using @emartech/cls-adapter, to store request specific information and later retrieve it when logging.

Themandunord commented 4 years ago

What about performance issue with cls ? Aync_hooks are pretty low.

I have push the feature on my server that have 3000req/15s, and my cpu are on fire :

image

You can see before and after the cls feature :/

edevil commented 4 years ago

@Themandunord What exactly did you change in the app to cause that spike?

Themandunord commented 4 years ago

@edevil I have figured it out yesterday evening. I was create a cls namespace for each request.

With only one (and it how it is done on @emartech/cls-adapter with the singleton instance for the namespace), the CPU just increase a little and the latency.

It's better.

Do you feel the same on your side?

I am dreaming about a winston's feature to do that 🤤

edevil commented 4 years ago

@Themandunord Yes, I didn't notice such a performance degradation as you were seeing. Glad you've worked it out.

TKul6 commented 4 years ago

Does anyone found a solution for this issue, it will really help me in my application.

Thanks