Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: attach custom data to middleware logger #695

Open
rubenvereecken opened this issue Apr 16, 2022 · 5 comments
Open

Feature request: attach custom data to middleware logger #695

rubenvereecken opened this issue Apr 16, 2022 · 5 comments
Labels
api: logging Issues related to the googleapis/nodejs-logging-winston API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@rubenvereecken
Copy link

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.

@product-auto-label product-auto-label bot added the api: logging Issues related to the googleapis/nodejs-logging-winston API. label Apr 16, 2022
@yoshi-automation yoshi-automation added the triage me I really want to be triaged. label Apr 17, 2022
@minherz minherz assigned losalex and unassigned minherz Apr 18, 2022
@minherz minherz added type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. and removed triage me I really want to be triaged. labels Apr 18, 2022
@hbiarge
Copy link

hbiarge commented May 18, 2022

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

@mbrevda
Copy link

mbrevda commented May 18, 2022

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}

@hbiarge
Copy link

hbiarge commented May 19, 2022

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;
        });

@mbrevda
Copy link

mbrevda commented May 19, 2022

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

@minherz minherz added priority: p3 Desirable enhancement or fix. May not be included in next release. priority: p2 Moderately-important priority. Fix may not be included in next release. and removed priority: p3 Desirable enhancement or fix. May not be included in next release. labels Jul 13, 2022
@Wazbat
Copy link

Wazbat commented Sep 8, 2022

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

@losalex losalex removed their assignment Jul 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: logging Issues related to the googleapis/nodejs-logging-winston API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

7 participants