I’ve pontificated previously about the benefits of combining bunyan with logstash, but if your app is using express it can be a little more complicated.
I wanted to add the user id & IP address of a request to all log output involved in that request. Bunyan makes it simple to create a child logger, and add the necessary properties; but due to the way express is architected (and how node works) the only way to access it is to pass it to the handler, and all it’s dependencies:
app.post("/foo", function(req, res) {
var childLogger = logger.child({
userId: req.userId, // taken from a header by middleware
ip: req.ip
}),
barService = new BarService(childLogger),
handler = new Handler(barService, childLogger);
handler.handle(req, res);
});
Which makes it possible to search the logs for all information relating to a specific user, or IP address.