Request logging with Morgan

The Morgan package has two general areas for configuration:

  • Log format
  • Log location

As it stands, Notes uses the dev format, which is described as a concise status output meant for developers. This can be used to log web requests as a way to measure website activity and popularity. The Apache log format already has a large ecosystem of reporting tools and, sure enough, Morgan can produce log files in this format. 

To change the format, simply change this line in app.js:

app.use(logger(process.env.REQUEST_LOG_FORMAT || 'dev')); 

Then run Notes as follows:

$ REQUEST_LOG_FORMAT=common npm start > notes@0.0.0 start /Users/david/chap07/notes > node ./bin/www ::1 - - [12/Feb/2016:05:51:21 +0000] "GET / HTTP/1.1" 304 ...

Get Node.js Web Development - Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.