mdv mdv - 4 years ago 134
Node.js Question

ExpressJS morgan log only route request

Im using NodeJS web framework Expressjs and one middleware to log requests to a file (morgan).

My conf:

// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(__dirname + '/logs/access.log', {flags: 'a'})

// setup the logger
app.use(logger('short', {stream: accessLogStream}))

And my log looks like: - GET /signup HTTP/1.1 304 - - 19.194 ms - GET /assets/css/admin/module.admin.stylesheet-complete.sidebar_type.collapse.no_min2.css HTTP/1.1 304 - - 15.500 ms - GET /assets/components/library/jquery/jquery.min.js?v=v1.0.3-rc2&amb;sv=v0.0.1.1 HTTP/1.1 304 - - 14.244 ms

Is there some way to ONLY log route request? for example if user enters /signup/: - GET /signup HTTP/1.1 304 - - 19.194 ms

and not the assets required for the route.

Thanks in advance.

Answer Source

I had the same problem, I've found simple solution, maybe it will help somebody. If you use express.static you can move this call above morgan call, for example:

app.use(express.static(__dirname + '/public'));

Static assets won't be logged. Notice: your assets files must exist, otherwise they will be still logged (tested in express 4.12).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download