Reputation: 3377
In express 4, the package morgan is used for logging. Strangely, none of the built in modes seem to log successful requests - they only log error cases. I'm using it as middleware, like so:
app.use(morgan('dev'));
How can I get it to print all activity going through the server, including successful requests?
Upvotes: 0
Views: 914
Reputation: 1136
You need to be aware which middleware doesn't contain the next() invocation of the 'next' middleware.
For example, the 'serve-static' middleware does not invoke next() on success. It only invokes next() on certain error conditions. Therefore, in order to log success requests, you need to use() 'morgan' before 'serveStatic'
Upvotes: 0
Reputation: 3377
Turns out, I had to setup morgan before setting up my routes. Changing the order made everything work as expected
Upvotes: 1
Reputation: 163602
There is plenty of middleware available for logging. At a minimum, you can do something like this:
app.use(function log (req, res, next) {
console.log([req.method, req.url].join(' '));
next();
});
It's entirely up to you to decide how you want to handle this.
I usually don't bother as I typically run Nginx out in front of my application servers. If I need access logs, I just use its logs. If I need application logs for debugging, then that's what I get out of my Node.js servers.
Upvotes: 1