forked from browserify/wzrd.in
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request-logger.js
47 lines (41 loc) · 1.24 KB
/
request-logger.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
var util = require('util');
var log = require('minilog')('http');
module.exports = function requestLogger(req, res, next) {
//
// Pretty much copypasta from
// https://github.com/senchalabs/connect/blob/master/lib/middleware/logger.js#L135-L158
//
// Monkey punching res.end. It's dirty but, maan I wanna log my status
// codes!
//
var end = res.end;
res.end = function (chunk, encoding) {
res.end = end;
res.end(chunk, encoding);
var remoteAddr = (function () {
if (req.ip) return req.ip;
var sock = req.socket;
if (sock.socket) return sock.socket.remoteAddress;
if (sock.remoteAddress) return sock.remoteAddress;
return ' - ';
})(),
date = new Date().toUTCString(), // DEFINITELY not CLF-compatible.
method = req.method,
url = req.originalUrl || req.url,
httpVersion = req.httpVersionMajor + '.' + req.httpVersionMinor,
status = res.statusCode;
// Similar to, but not anywhere near compatible with, CLF. So don't try
// parsing it as CLF.
//
log.info(util.format(
'%s - - [%s] "%s %s HTTP/%s" %s',
remoteAddr,
date,
method,
url,
httpVersion,
status
));
};
next();
};