From c7d3ac4fe1d7d77859082b7cc159c673a64b2e84 Mon Sep 17 00:00:00 2001 From: muddydixon Date: Tue, 25 Oct 2011 14:09:41 +0900 Subject: [PATCH] add nolog operation --- lib/connect-logger.js | 55 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-) diff --git a/lib/connect-logger.js b/lib/connect-logger.js index 395d881..b775775 100644 --- a/lib/connect-logger.js +++ b/lib/connect-logger.js @@ -37,13 +37,17 @@ function getLogger(logger4js, options) { var thislogger = logger4js , level = levels.toLevel(options.level, levels.INFO) - , fmt = options.format || ':remote-addr - - ":method :url HTTP/:http-version" :status :content-length ":referrer" ":user-agent"'; + , fmt = options.format || ':remote-addr - - ":method :url HTTP/:http-version" :status :content-length ":referrer" ":user-agent"' + , nolog = options.nolog ? createNoLogCondition(options.nolog) : null; return function (req, res, next) { // mount safety if (req._logging) return next(); + // nologs + if(nolog.test(req.url)) return next(); + if (thislogger.isLevelEnabled(level)) { var start = +new Date @@ -111,4 +115,53 @@ function format(str, req, res) { }); } +/** + * Return RegExp Object about nolog + * + * @param {String} nolog + * @return {RegExp} + * @api private + */ + +/** + * syntax + * 1. String + * 1.1 "\\.gif" + * NOT LOGGING http://example.com/hoge.gif and http://example.com/hoge.gif?fuga + * LOGGING http://example.com/hoge.agif + * 1.2 in "\\.gif|\\.jpg$" + * NOT LOGGING http://example.com/hoge.gif and http://example.com/hoge.gif?fuga and http://example.com/hoge.jpg?fuga + * LOGGING http://example.com/hoge.agif, http://example.com/hoge.ajpg and http://example.com/hoge.jpg?hoge + * 1.3 in "\\.(gif|jpe?g|png)$" + * NOT LOGGING http://example.com/hoge.gif and http://example.com/hoge.jpeg + * LOGGING http://example.com/hoge.gif?uid=2 and http://example.com/hoge.jpg?pid=3 + * 2. RegExp + * 2.1 in /\.(gif|jpe?g|png)$/ + * SAME AS 1.3 + * 3. Array + * 3.1 ["\\.jpg$", "\\.png", "\\.gif"] + * SAME AS "\\.jpg|\\.png|\\.gif" + */ +function createNoLogCondition(nolog, type) { + if(!nolog) return null; + type = type || ''; + + if(nolog instanceof RegExp){ + if(type === 'string') + return nolog.source; + return nolog; + } else if(typeof nolog === 'string'){ + if(type === 'string') + return nolog; + try{ + return new RegExp(nolog); + } catch (ex) { + return null; + } + } else if(nolog instanceof Array){ + var regexps = nolog.map(function(o){ return createNoLogCondition(o, 'string')}); + return new RegExp(regexps.join('|')); + } +} + exports.connectLogger = getLogger; \ No newline at end of file