Created
October 25, 2012 14:45
-
-
Save bromanko/3952984 to your computer and use it in GitHub Desktop.
Express Middleware for Bunyan logging
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var bunyan = require('bunyan'); | |
| module.exports.logger = function logger(log) { | |
| if (typeof (log) !== 'object') | |
| throw new TypeError('log (Object) required'); | |
| this.log = log; | |
| var self = this; | |
| return function logger(req, res, next) { | |
| req.log = log; | |
| self.log.info({req: bunyan.stdSerializers.req(req)}, 'start'); | |
| next(); | |
| }; | |
| }; | |
| module.exports.errorLogger = function(log) { | |
| if (typeof (log) !== 'object') | |
| throw new TypeError('log (Object) required'); | |
| this.log = log; | |
| var self = this; | |
| return function logger(err, req, res, next) { | |
| if (err) | |
| self.log.trace({err: err}, 'error'); | |
| next(); | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The use of
thislooks dangerous. These are not constructors, sothisrefers to the scope of the module, allowing strange behaviour with collisions. There doesn't appear to be any need to usethis, as the scope should take care of itself: