nodejs-logging-winston icon indicating copy to clipboard operation
nodejs-logging-winston copied to clipboard

Feature request: attach custom data to middleware logger

Open rubenvereecken opened this issue 4 years ago • 5 comments

The use case is quite simple. I'd like to attach some custom data to request logs, such as which user is associated with this request. This data is available on the req object, so all I'd need is an optional makeData parameter for makeMiddleware. This function would be invoked for each request so that custom data could be set for each request.

rubenvereecken avatar Apr 16 '22 13:04 rubenvereecken

Same here. I need to include custom data (userId) to the log. Any advance on this feature request? Or any alternative? Thanks in advance!

hbiarge avatar May 18 '22 12:05 hbiarge

Here's an example of adding data dynamically:

let UserData

const winstonConfig = {
  level: 'info',
  format: winston.format.combine(
    winston.format.errors({stack: true}),
    winston.format.json()
  )
}

// UserData is not set yet, it will be set later
Object.defineProperty(winstonConfig.defaultMeta, 'user', {
  enumerable: true,
  get() {
    return UserData
  }
})
const log = winston.createLogger(winstonConfig)

later in the same file:

const myMiddleware = async (req, res, next) => {
  UserData = req.user
}

export {myMiddleware}

mbrevda avatar May 18 '22 16:05 mbrevda

Hi @mbrevda! Thanks for the quick response. But I was looking for something similar but with the express middleware that this library provides:

GoogleWinston.express.makeMiddleware(logger, options);

I don't want to create my own middleware for this. Something like morgan do with custom tokens or so:

morgan.token("userId", function (req, res) {
            const userId = req["userId"] || "N/A";
            return userId;
        });

hbiarge avatar May 19 '22 07:05 hbiarge

So was I. It doesn't seem that winston supports appending metadata post initialization.

mbrevda avatar May 19 '22 07:05 mbrevda

~~I don't think this is a good fix, but wouldn't you in theory be able to create a child logger from the req.log at the start of your request handler wherever this data is available and just use that?~~

EDIT: Ah I think I'm mistaken. That'd work for the logs in your application, but the final log sent when the request is complete would not have that metadata. Rewriting req.log with your logger might work, but even then that feels like a hacky fix

Wazbat avatar Sep 08 '22 13:09 Wazbat