Tuesday 29 November 2016

Need help on logging HTTP header with swagger-express-mw

Hi,I have this project that uses swagger-express-mw. It was coded by someone else and it is fairly large (ie: re-architecting the whole thing is not really an option).As it is expected swagger-express-mw, I have a swagger.yaml file that reference a bunch of controllers and methods.For instance, I may haveexports.myMethod123 = function (req, res) { // code... } exports.myMethod456 = function (req, res) { // some other code } Of course, each of these controller then call a service layer which use various DTO and DAO, etc.My question is: at various point in these services, DTOs, DAOs and other modules I want to log and trace a variable set by the Nginx front-end (X-RequestID)... how to do so?Usually, in the services or DAOs, the "req." object is long gone. We're several async calls deep and the initial context is totally lost. It might be worth noting that for logging we wrapped Winston. Therefore I would like, as much as possible, to use the Rewriter feature where I would dynamically inspect a "context" object and add info to the log if the "context" object contains something I want to log (X-RequestID).I've looked quite a lot on the Internet. Something called "domain" seemed promising, until I found it was deprecated. request-context seems nice too, but I would have to go through every controllers's methods (+200) and set the request-context everywhere.Anyone aware of a simpler method? I'm thinking of something like a HTTP interceptor: on every HTTP request, execute a function (which would perhaps set the request-context) and then the logger would be able to inspect this context.Any help on how to approach this problem is appreciated.

Submitted November 30, 2016 at 03:28AM by thisIsExactly20Chars

No comments:

Post a Comment