I have my API built with express.
Out of the box it uses morgan, which can be used as logging middleware. From what I can see it binds to the request and listens for events and is able to collect data when the connection is complete. This allows it to record timings and size of data transferred.
I would prefer to use something like winston which has really good transport options that suit me, although I am looking to capture more data about the I/O with my server.
I have my own custom generic response functions that I am able to log extra information when responding from my API endpoints.
I cannot do the same for logging requests for static assets.
So I very much need to be able to have a function that binds to the request and logs on completion so I can track response time, response size and other details across all requests.
How can I achieve this while still using winston?
I did solve this one a long time ago...
Ended up using this package to help detect when the response headers were written (indicating an end of the response) - https://github.com/jshttp/on-headers
This is the code I implemented as middleware to achieve logging at the end of each response:
// Log Request
app.use(function (req, res, next) {
// Listen for response event and log
onHeaders(res, function () {
logger.info({
_id: req.id,
client: {
ip: function () {
return req.ip
|| req._remoteAddress
|| (req.connection && req.connection.remoteAddress)
|| undefined
}()
},
method: req.method,
url: req.url,
statusCode: res.statusCode,
time: (Date.now() - req.start),
'res-length': res._headers['content-length'] || 0,
'req-length': req.headers['content-length'] || 0
});
});
next();
});