Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log every http.request made in Node

Tags:

node.js

I want log every http request being made by a particular node app, and all of its modules. Wrapping requests in a function could work for all non-module code, the disadvantage would obviously be it doesn't include module code, and be cumbersome to do.

This is for apps already in production, only other option I thought of was tcpdump.

like image 257
Alister Avatar asked Feb 18 '17 18:02

Alister


People also ask

Which module helps to log all request in an express Server?

Express uses the debug module internally to log information about route matches, middleware functions that are in use, application mode, and the flow of the request-response cycle. debug is like an augmented version of console. log , but unlike console.

Which logger is best for NodeJS?

Some of the most popular logging libraries for Node are Winston, Pino, Bunyan, and Log4js. While you should almost always use a standard console. log, a logging library can be more functional and help avoid decreases in app performance.


1 Answers

Setting NODE_DEBUG=http will make node log out detailed HTTP request information to the console.

Examples:

NODE_DEBUG=http node index.js

NODE_DEBUG=http npm start

For more information see:

  • NODE_DEBUG documentation

  • This blog post: Debugging tools and practices in node.js.
    It includes:

List of the default available NODE_DEBUG attributes:

  • timer
  • http
  • net
  • fs
  • cluster
  • tls
  • stream
  • child_process
  • module
like image 160
Sly_cardinal Avatar answered Oct 05 '22 12:10

Sly_cardinal