-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
81 lines (69 loc) · 2.16 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
"use strict";
/*
* express-fluent-logger
* Copyright(c) 2014-2015 Toshiya SAITOH
*/
var logger = require('fluent-logger');
var debug = require('debug')('express-fluent-logger');
/**
* Create a fluent logger middleware.
*
* @public
* @param {String|Function} tag
* @param {Object} options
* @return {Function}
*/
exports = module.exports = function expressFluentLogger(tag, options) {
if (typeof tag === 'object') {
options = tag;
tag = 'debug';
}
if (typeof tag === 'undefined') {
tag = 'debug';
}
options = options || { host: '127.0.0.1', port: 24224, timeout: 3.0 };
logger.configure(tag, options);
logger.on('error', debug);
return function(req, res, next) {
var start = new Date();
function emitHandler() {
res.removeListener('finish', emitHandler);
res.removeListener('close', emitHandler);
var logObject = {
'timestamp': start.getTime(),
'remote-address': req.ip,
'method': req.method,
'url': req.protocol + '://' + req.get('host') + req.originalUrl ? req.originalUrl : req.url,
'http-version': req.httpVersion,
'status': res.statusCode,
'content-length': res.get('content-length'),
'referrer': req.get('referrer') || req.get('referer') || '',
'response-time': new Date() - start
};
// logging request headers.
Object.keys(req.headers)
.filter(function(key) {
return key !== 'host' && key !== 'connection' && key !== 'referrer' && key !== 'referer';
})
.forEach(function(key) {
key = key.toLowerCase();
logObject[key] = req.get(key);
});
// logging response headers.
options.responseHeaders = options.responseHeaders || [];
options.responseHeaders
.filter(function(key) {
return res.get(key);
})
.forEach(function(key) {
key = key.toLowerCase();
logObject[key.toLowerCase()] = res.get(key);
});
logger.emit('access', logObject);
}
res.on('finish', emitHandler);
res.on('close', emitHandler);
req.logger = logger;
next();
};
};