Built in access logs in node.js (express framework)

Ivan Longin picture Ivan Longin · Oct 30, 2013 · Viewed 35k times · Source

I was wondering if node.js (or express framework) has any kind of built in access logging like grails has for example?

I have grails application that runs on tomcat and it automatically generates /apache-tomcat-7.0.42/logs/localhost_access_log.2013.10.30.txt file in which are logs about request response like this one:

[30/Oct/2013:00:00:01 +0000] [my-ip-address] [http-bio-8080-exec-18] "GET /my-service/check HTTP/1.0" [200] [took: 1 milis]  

This logs are written automatically by system and I don't have to worry about that.

So what about node.js?

Thanks for any help!

Ivan

Answer

whirlwin picture whirlwin · Apr 17, 2014

In newer versions of Express (4.0.0 at the time of writing), the logger is no longer part of Express, so you have to include it as a dependency manually. It is now called morgan.

So, in package.json, add morgan as a dependency:

"dependencies": {
  ...
  "morgan": "*"
  ...
}

And in your Express configuration, add:

app.use(require('morgan')('dev'));

Now logging should work more or less like before. :-)