lib/connect-logger.js - allow options.tokens

This commit is contained in:
Christo Fogelberg 2015-03-10 06:18:02 +00:00
parent 24268422cf
commit 25c543f8ae

View File

@ -89,12 +89,12 @@ function getLogger(logger4js, options) {
if(res.statusCode >= 400) level = levels.ERROR;
}
if (thislogger.isLevelEnabled(level)) {
var default_tokens = assemble_tokens(req, res);
var combined_tokens = assemble_tokens(req, res, options.tokens || []);
if (typeof fmt === 'function') {
var line = fmt(req, res, function(str){ return format(str, default_tokens); });
var line = fmt(req, res, function(str){ return format(str, combined_tokens); });
if (line) thislogger.log(level, line);
} else {
thislogger.log(level, format(fmt, default_tokens));
thislogger.log(level, format(fmt, combined_tokens));
}
}
});
@ -113,7 +113,19 @@ function getLogger(logger4js, options) {
* @param {Array} custom_tokens [{ token: string-or-regexp, replacement: string-or-replace-function }]
* @return {Array}
*/
function assemble_tokens(req, res) {
function assemble_tokens(req, res, custom_tokens) {
var array_unique_tokens = function(array) {
var a = array.concat();
for(var i=0; i<a.length; ++i) {
for(var j=i+1; j<a.length; ++j) {
if(a[i].token == a[j].token) { // not === because token can be regexp object
a.splice(j--, 1);
}
}
}
return a;
};
var default_tokens = [];
default_tokens.push({ token: ':url', replacement: req.originalUrl });
default_tokens.push({ token: ':method', replacement: req.method });
@ -136,7 +148,7 @@ function assemble_tokens(req, res) {
: (res.__headers && res.__headers[field]);
} });
return default_tokens;
return array_unique_tokens(custom_tokens.concat(default_tokens));
};
/**