forked from bahamas10/node-access-log
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
100 lines (84 loc) · 2.83 KB
/
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
var strftime = require('strftime');
var defaultformat = ':ip - :userID [:clfDate] ":method :url :protocol/:httpVersion" :statusCode :contentLength ":referer" ":userAgent"';
module.exports = accesslog;
function accesslog(req, res, format, cb) {
if (typeof format === 'function') {
cb = format;
format = null;
}
var remoteAddress = req.connection.remoteAddress;
var contentLength;
format = format || defaultformat;
cb = cb || console.log.bind(console);
var uriDecoded;
try {
uriDecoded = decodeURIComponent(req.url);
} catch (e) {
uriDecoded = e.message || 'error decoding URI';
}
var start = new Date();
// override res.writeHead to track contentLength
var resWriteHead = res.writeHead.bind(res);
res.writeHead = function(statusCode, reason, headers) {
var ret = resWriteHead.apply(res, arguments);
if (typeof reason === 'object' && !headers) {
headers = reason;
reason = null;
}
if (headers) {
Object.keys(headers).forEach(function(key) {
if (key.toLowerCase() === 'content-length')
contentLength = headers[key];
});
}
return ret;
};
// override res.end to capture all responses
var resend = res.end.bind(res);
res.end = function() {
// call the original
var ret = resend.apply(res, arguments);
var end = new Date();
var delta = end - start;
var userID;
try {
userID = new Buffer(req.headers.authorization.split(' ')[1], 'base64').toString().split(':')[0];
} catch(e) {}
var data = {
':clfDate': strftime('%d/%b/%Y:%H:%M:%S %z', end),
':contentLength': res.getHeader('content-length') || contentLength || '-',
':delta': delta,
':endDate': end.toISOString(),
':endTime': end.getTime(),
':host': encode(req.headers.host || '-'),
':httpVersion': req.httpVersion,
':ip': remoteAddress || '-',
':Xip': encode(req.headers['x-forwarded-for'] || remoteAddress || '-'),
':method': req.method,
':protocol': req.connection.encrypted ? 'HTTPS' : 'HTTP',
':referer': encode(req.headers.referer || '-'),
':startDate': start.toISOString(),
':startTime': start.getTime(),
':statusCode': res.statusCode,
':url': encode(req.url),
':urlDecoded': encode(uriDecoded),
':userID': encode(userID || '-'),
':userAgent': encode(req.headers['user-agent'] || '-')
};
cb(template(format, data));
return ret;
};
}
// replace :variable and :{variable} in `s` with what's in `d`
function template(s, d) {
s = s.replace(/(:[a-zA-Z]+)/g, function(match, key) {
return d[key] || '';
});
return s.replace(/:{([a-zA-Z]+)}/g, function(match, key) {
return d[':' + key] || '';
});
}
// make a string safe to put in double quotes in CLF
function encode(s) {
return s.replace(/\\/g, '\\x5C').replace(/"/, '\\x22');
}