DEV Community

HTTP request logging in Node.JS

bright inventions on February 01, 2018

One of the most basic kind of logging every backend application should have is a trace logging of all incoming HTTP requests. Yet it's not easy to ...
Collapse
 
dangolant profile image
Daniel Golant

I normally read posts and have the response “hrm that’s an interesting concept/idea, maybe I’ll have to try it sometime or learn more”, but here my reaction is that this is super practical, ups the DX a ton, and isn’t that hard to implement. I want to try this immediately :)

Collapse
 
davicente profile image
David Vicente Fuentes

I was struggling with this same problem time ago. The problem I found with your approach is that you need to pass the request object all over your code, which gets it dirty.
Finally, I made a wrap of Winston logger library, and used another libraries for sharing variables withoug passing them through function calls, and it works pretty well. The library generate and manage a unique request Id and print it with every log, so you can abstract from this problem.
Hope it helps:
github.com/davicente/express-logge...