lib/connect-logger.js - format takes tokens array instead of req, res
This commit is contained in:
parent
ba80dc1588
commit
24268422cf
@ -1,5 +1,6 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
var levels = require("./levels");
|
var levels = require("./levels");
|
||||||
|
var _ = require('underscore');
|
||||||
var DEFAULT_FORMAT = ':remote-addr - -' +
|
var DEFAULT_FORMAT = ':remote-addr - -' +
|
||||||
' ":method :url HTTP/:http-version"' +
|
' ":method :url HTTP/:http-version"' +
|
||||||
' :status :content-length ":referrer"' +
|
' :status :content-length ":referrer"' +
|
||||||
@ -88,11 +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);
|
||||||
if (typeof fmt === 'function') {
|
if (typeof fmt === 'function') {
|
||||||
var line = fmt(req, res, function(str){ return format(str, req, res); });
|
var line = fmt(req, res, function(str){ return format(str, default_tokens); });
|
||||||
if (line) thislogger.log(level, line);
|
if (line) thislogger.log(level, line);
|
||||||
} else {
|
} else {
|
||||||
thislogger.log(level, format(fmt, req, res));
|
thislogger.log(level, format(fmt, default_tokens));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -103,6 +105,40 @@ function getLogger(logger4js, options) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds custom {token, replacement} objects to defaults, overwriting the defaults if any tokens clash
|
||||||
|
*
|
||||||
|
* @param {IncomingMessage} req
|
||||||
|
* @param {ServerResponse} res
|
||||||
|
* @param {Array} custom_tokens [{ token: string-or-regexp, replacement: string-or-replace-function }]
|
||||||
|
* @return {Array}
|
||||||
|
*/
|
||||||
|
function assemble_tokens(req, res) {
|
||||||
|
var default_tokens = [];
|
||||||
|
default_tokens.push({ token: ':url', replacement: req.originalUrl });
|
||||||
|
default_tokens.push({ token: ':method', replacement: req.method });
|
||||||
|
default_tokens.push({ token: ':status', replacement: res.__statusCode || res.statusCode });
|
||||||
|
default_tokens.push({ token: ':response-time', replacement: res.responseTime });
|
||||||
|
default_tokens.push({ token: ':date', replacement: new Date().toUTCString() });
|
||||||
|
default_tokens.push({ token: ':referrer', replacement: req.headers.referer || req.headers.referrer || '' });
|
||||||
|
default_tokens.push({ token: ':http-version', replacement: req.httpVersionMajor + '.' + req.httpVersionMinor });
|
||||||
|
default_tokens.push({ token: ':remote-addr', replacement: req.ip || req._remoteAddress ||
|
||||||
|
(req.socket && (req.socket.remoteAddress || (req.socket.socket && req.socket.socket.remoteAddress))) });
|
||||||
|
default_tokens.push({ token: ':user-agent', replacement: req.headers['user-agent'] });
|
||||||
|
default_tokens.push({ token: ':content-length', replacement: (res._headers && res._headers['content-length']) ||
|
||||||
|
(res.__headers && res.__headers['Content-Length']) || '-' });
|
||||||
|
default_tokens.push({ token: /:req\[([^\]]+)\]/g, replacement: function(_, field) {
|
||||||
|
return req.headers[field.toLowerCase()];
|
||||||
|
} });
|
||||||
|
default_tokens.push({ token: /:res\[([^\]]+)\]/g, replacement: function(_, field) {
|
||||||
|
return res._headers ?
|
||||||
|
(res._headers[field.toLowerCase()] || res.__headers[field])
|
||||||
|
: (res.__headers && res.__headers[field]);
|
||||||
|
} });
|
||||||
|
|
||||||
|
return default_tokens;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return formatted log line.
|
* Return formatted log line.
|
||||||
*
|
*
|
||||||
@ -113,33 +149,10 @@ function getLogger(logger4js, options) {
|
|||||||
* @api private
|
* @api private
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function format(str, req, res) {
|
function format(str, tokens) {
|
||||||
return str
|
return _.reduce(tokens, function(current_string, token) {
|
||||||
.replace(':url', req.originalUrl)
|
return current_string.replace(token.token, token.replacement);
|
||||||
.replace(':method', req.method)
|
}, str);
|
||||||
.replace(':status', res.__statusCode || res.statusCode)
|
|
||||||
.replace(':response-time', res.responseTime)
|
|
||||||
.replace(':date', new Date().toUTCString())
|
|
||||||
.replace(':referrer', req.headers.referer || req.headers.referrer || '')
|
|
||||||
.replace(':http-version', req.httpVersionMajor + '.' + req.httpVersionMinor)
|
|
||||||
.replace(
|
|
||||||
':remote-addr', req.ip || req._remoteAddress || (
|
|
||||||
req.socket &&
|
|
||||||
(req.socket.remoteAddress || (req.socket.socket && req.socket.socket.remoteAddress))
|
|
||||||
))
|
|
||||||
.replace(':user-agent', req.headers['user-agent'] || '')
|
|
||||||
.replace(
|
|
||||||
':content-length',
|
|
||||||
(res._headers && res._headers['content-length']) ||
|
|
||||||
(res.__headers && res.__headers['Content-Length']) ||
|
|
||||||
'-'
|
|
||||||
)
|
|
||||||
.replace(/:req\[([^\]]+)\]/g, function(_, field){ return req.headers[field.toLowerCase()]; })
|
|
||||||
.replace(/:res\[([^\]]+)\]/g, function(_, field){
|
|
||||||
return res._headers ?
|
|
||||||
(res._headers[field.toLowerCase()] || res.__headers[field])
|
|
||||||
: (res.__headers && res.__headers[field]);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -31,7 +31,8 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"async": "~0.2.0",
|
"async": "~0.2.0",
|
||||||
"readable-stream": "~1.0.2",
|
"readable-stream": "~1.0.2",
|
||||||
"semver": "~1.1.4"
|
"semver": "~1.1.4",
|
||||||
|
"underscore": "1.8.2"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"vows": "0.7.0",
|
"vows": "0.7.0",
|
||||||
|
Loading…
Reference in New Issue
Block a user