-
Notifications
You must be signed in to change notification settings - Fork 50
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
Comments
Same here. I need to include custom data (userId) to the log. Any advance on this feature request? Or any alternative? |
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} |
Hi @mbrevda! GoogleWinston.express.makeMiddleware(logger, options); I don't want to create my own middleware for this. morgan.token("userId", function (req, res) {
const userId = req["userId"] || "N/A";
return userId;
}); |
So was I. It doesn't seem that winston supports appending metadata post initialization. |
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 |
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 optionalmakeData
parameter formakeMiddleware
. This function would be invoked for each request so that custom data could be set for each request.The text was updated successfully, but these errors were encountered: