lib/connect-logger.js - allow options.tokens
This commit is contained in:
parent
24268422cf
commit
25c543f8ae
@ -89,12 +89,12 @@ function getLogger(logger4js, options) {
|
|||||||
if(res.statusCode >= 400) level = levels.ERROR;
|
if(res.statusCode >= 400) level = levels.ERROR;
|
||||||
}
|
}
|
||||||
if (thislogger.isLevelEnabled(level)) {
|
if (thislogger.isLevelEnabled(level)) {
|
||||||
var default_tokens = assemble_tokens(req, res);
|
var combined_tokens = assemble_tokens(req, res, options.tokens || []);
|
||||||
if (typeof fmt === 'function') {
|
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);
|
if (line) thislogger.log(level, line);
|
||||||
} else {
|
} 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 }]
|
* @param {Array} custom_tokens [{ token: string-or-regexp, replacement: string-or-replace-function }]
|
||||||
* @return {Array}
|
* @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 = [];
|
var default_tokens = [];
|
||||||
default_tokens.push({ token: ':url', replacement: req.originalUrl });
|
default_tokens.push({ token: ':url', replacement: req.originalUrl });
|
||||||
default_tokens.push({ token: ':method', replacement: req.method });
|
default_tokens.push({ token: ':method', replacement: req.method });
|
||||||
@ -136,7 +148,7 @@ function assemble_tokens(req, res) {
|
|||||||
: (res.__headers && res.__headers[field]);
|
: (res.__headers && res.__headers[field]);
|
||||||
} });
|
} });
|
||||||
|
|
||||||
return default_tokens;
|
return array_unique_tokens(custom_tokens.concat(default_tokens));
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user