Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 870
Ivan Longin Avatar asked Oct 30 '13 11:10

Ivan Longin


1 Answers

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. :-)

like image 26
whirlwin Avatar answered Oct 12 '22 07:10

whirlwin