How to append a unique_id to my express node logs with winston without refactoring everything?

馋奶兔 提交于 2019-11-29 21:08:51

问题


I've an Express app. I'd like to trace all the loggings that were made by a single request.

Apache has a module called mod_unique_id which injects in the request headers a special one containing a unique hash. I'd like to use such hash when logging (anything).

For example if an user is assigned by apache the unique_id valued "abcdefg" and somewhere in my code (controller, model, or anything) i generate a debug log i'd like to have "abcdefg" attached to every log entry.

The only idea that pops in my mind is to create a logger instance using app.use() and pass that logger instance to ALL the functions that are needed (models,libs etc) to generate the proper response.

Unfortunately this will cause me to rewrite a lot of the code to let old functions accept this new logger instance.

Does anyone know a better way of having a single "logger" instance per "request" which is accessible somehow without passing this "per-request-created-logger" to anything ?

PS: I'm currently using winston to log

来源:https://stackoverflow.com/questions/34534616/how-to-append-a-unique-id-to-my-express-node-logs-with-winston-without-refactori

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!