Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Refactor

  • Loading branch information...
commit ee4708ca32a94600f97d23dba01981479d92c558 1 parent f0d42e5
@carlos8f authored
View
78 index.js
@@ -1,6 +1,4 @@
-var addr = require('addr')
- , dateable = require('dateable')
- ;
+var compile = require('./lib/compile');
exports = module.exports = function accesslog(options) {
options || (options = {});
@@ -16,7 +14,7 @@ exports = module.exports = function accesslog(options) {
stream = process.stdout;
}
options.format || (options.format = "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\"");
- var render = compile(options.format, {options: options});
+ var render = compile(options.format, {options: options, stream: stream});
return function accessLogger(req, res, next) {
var end = res.end;
@@ -30,74 +28,4 @@ exports = module.exports = function accesslog(options) {
};
};
-function compile(format, context) {
- format = format.replace(/"/g, '\\"');
- var js = ' return "' + format.replace(/%(>?\w|{[\w-]+}i)/g, function(_, name) {
- return '"\n + (tokens["' + name + '"].call(this, req, res) || "-") + "';
- }) + '";';
- return new Function('tokens, req, res', js).bind(context);
-};
-
-exports.tokens = {};
-
-exports.tokens['h'] = exports.tokens['a'] = function(req) {
- return addr(req, this.options.proxies);
-};
-
-exports.tokens['l'] = function(req) {
- return null;
-};
-
-exports.tokens['u'] = function(req) {
- return null;
-};
-
-exports.tokens['t'] = function(req) {
- var d = new Date();
- var offset = d.getTimezoneOffset();
- var tz = zerofill(Math.abs(offset) / 60, 2) + zerofill(Math.abs(offset) % 60, 2);
- tz = (offset > 0 ? '-' : '+') + tz;
- return '[' + dateable.format(d, 'DD/MMM/YYYY:HH:MM:ss') + ' ' + tz + ']';
-};
-
-exports.tokens['r'] = function(req) {
- return req.method + ' ' + req.url.replace('"', '\\"') + ' HTTP/' + req.httpVersionMajor + '.' + req.httpVersionMinor;
-};
-
-exports.tokens['>s'] = exports.tokens['s'] = function(req, res) {
- return res.statusCode;
-};
-
-exports.tokens['b'] = function(req, res) {
- var length = parseInt(getHeader(res, 'Content-Length'), 10);
- return isNaN(length) ? null : length;
-};
-
-exports.tokens['{Referer}i'] = function(req) {
- return req.headers['referer'] || req.headers['referrer'];
-};
-
-exports.tokens['{User-agent}i'] = function(req) {
- return req.headers['user-agent'];
-};
-
-function zerofill(num, length) {
- num += '';
- while (num.length < length) {
- num = '0' + num;
- }
- return num;
-}
-
-function getHeader(res, name) {
- if (res._header && res._headerSent && !res._sentHeaders) {
- res._sentHeaders = {};
- res._header.split('\r\n').forEach(function(line) {
- var header = line.split(': ');
- if (header.length > 1) {
- res._sentHeaders[header[0]] = header[1];
- }
- });
- }
- return (res._sentHeaders && res._sentHeaders[name]) || null;
-}
+exports.tokens = require('./lib/tokens');
View
7 lib/compile.js
@@ -0,0 +1,7 @@
+module.exports = function compile(format, context) {
+ format = format.replace(/"/g, '\\"');
+ var js = ' return "' + format.replace(/%(>?\w|{[\w-]+}i)/g, function(_, name) {
+ return '"\n + (tokens["' + name + '"].call(this, req, res) || "-") + "';
+ }) + '";';
+ return new Function('tokens, req, res', js).bind(context);
+};
View
12 lib/getHeader.js
@@ -0,0 +1,12 @@
+module.exports = function getHeader(res, name) {
+ if (res._header && res._headerSent && !res._sentHeaders) {
+ res._sentHeaders = {};
+ res._header.split('\r\n').forEach(function(line) {
+ var header = line.split(': ');
+ if (header.length > 1) {
+ res._sentHeaders[header[0]] = header[1];
+ }
+ });
+ }
+ return (res._sentHeaders && res._sentHeaders[name]) || null;
+};
View
46 lib/tokens.js
@@ -0,0 +1,46 @@
+var addr = require('addr')
+ , dateable = require('dateable')
+ , getHeader = require('./getHeader')
+ , zerofill = require('./zerofill')
+ ;
+
+exports['h'] = exports['a'] = function(req) {
+ return addr(req, this.options.proxies);
+};
+
+exports['l'] = function(req) {
+ return null;
+};
+
+exports['u'] = function(req) {
+ return null;
+};
+
+exports['t'] = function(req) {
+ var d = new Date();
+ var offset = d.getTimezoneOffset();
+ var tz = zerofill(Math.abs(offset) / 60, 2) + zerofill(Math.abs(offset) % 60, 2);
+ tz = (offset > 0 ? '-' : '+') + tz;
+ return '[' + dateable.format(d, 'DD/MMM/YYYY:HH:MM:ss') + ' ' + tz + ']';
+};
+
+exports['r'] = function(req) {
+ return req.method + ' ' + req.url.replace('"', '\\"') + ' HTTP/' + req.httpVersionMajor + '.' + req.httpVersionMinor;
+};
+
+exports['>s'] = exports['s'] = function(req, res) {
+ return res.statusCode;
+};
+
+exports['b'] = function(req, res) {
+ var length = parseInt(getHeader(res, 'Content-Length'), 10);
+ return isNaN(length) ? null : length;
+};
+
+exports['{Referer}i'] = function(req) {
+ return req.headers['referer'] || req.headers['referrer'];
+};
+
+exports['{User-agent}i'] = function(req) {
+ return req.headers['user-agent'];
+};
View
7 lib/zerofill.js
@@ -0,0 +1,7 @@
+module.exports = function zerofill(num, length) {
+ num += '';
+ while (num.length < length) {
+ num = '0' + num;
+ }
+ return num;
+};
Please sign in to comment.
Something went wrong with that request. Please try again.