-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
31 lines (26 loc) · 826 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
var compile = require('./lib/compile');
exports = module.exports = function accesslog(options) {
options || (options = {});
var stream;
if (options.path) {
stream = require('fs').createWriteStream(options.path, {flags: 'a+'});
}
else if (options.stream) {
stream = options.stream;
}
else {
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});
return function accessLogger(req, res, next) {
var end = res.end;
res.end = function(chunk, encoding) {
res.end = end;
res.end(chunk, encoding);
stream.write(render(exports.tokens, req, res) + '\n', 'ascii');
};
next && next();
};
};
exports.tokens = require('./lib/tokens');