add nolog operation

This commit is contained in:
muddydixon 2011-10-25 14:09:41 +09:00
parent 0aca64623e
commit c7d3ac4fe1

View File

@ -37,13 +37,17 @@ function getLogger(logger4js, options) {
var thislogger = logger4js var thislogger = logger4js
, level = levels.toLevel(options.level, levels.INFO) , 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) { return function (req, res, next) {
// mount safety // mount safety
if (req._logging) return next(); if (req._logging) return next();
// nologs
if(nolog.test(req.url)) return next();
if (thislogger.isLevelEnabled(level)) { if (thislogger.isLevelEnabled(level)) {
var start = +new Date 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; exports.connectLogger = getLogger;