-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathaccesslog.js
137 lines (122 loc) · 3.62 KB
/
accesslog.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
var microtime = require('microtime')
, sprintf = require('sprintf').sprintf
, dateformat = require('date-format-lite')
, fs = require('fs')
, path = require('path')
, fd = undefined
, cluster = require('cluster');
;
// public exports
var accesslog = exports;
var conf = {
format: 'CLF',
directory: 'logs',
filename: 'access.log'
};
accesslog.version = require('./package').version;
/**
* Configuration object
*/
accesslog.configure = function accesslogConfigure(opt) {
// writes logs into this directory
conf.directory = (typeof opt.directory === 'string') ? opt.directory : __dirname + pathsep + 'logs';
// write log to file with name
conf.filename = (typeof opt.directory === 'string') ? opt.filename : 'access.log';
// log format
/**
NCSA Common Log Format (CLF)
"%h %l %u %t \"%r\" %>s %b"
NCSA Common Log Format with Virtual Host
"%v %h %l %u %t \"%r\" %>s %b"
NCSA extended/combined log format
"%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
NCSA extended/combined log format with Virtual Host
"%v %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-agent}i\""
Add a '+' to the logformat to also add the clusters worker id to the entry
*/
conf.format = (typeof opt.directory === 'string') ? opt.format : 'CLF';
};
/**
* Log-Middleware
* Will log in the format that is set in conf.format. Default formatg is CLF.
*
* @scope public
*/
accesslog.logger = function log(request, response, next) {
var starttime = microtime.now();
// from behind a proxy
var clientAddr = request.headers['X-Forwarded-For'];
if( clientAddr == undefined ){
// direct connection
clientAddr = request.connection.remoteAddress;
}
// get username (if available)
var username = "-";
if(request.session && request.session.user){
username = request.session.user;
} else {
if(request.session && request.session.id){
username = request.session.id;
}
}
if (typeof next === 'function') {
next();
}
var endtime = microtime.now(); // microseconds
var rendertime = endtime - starttime;
var now = new Date();
var p0 = sprintf("%s - %s [%s/%s/%s:%s:%s:%s] %s",
clientAddr,
username,
now.format("DD"),
now.format("MMM"),
now.format("YYYY"),
now.format("hh"),
now.format("mm"),
now.format("ss"),
rendertime
);
request.protocol = request.protocol || 'unknown';
var p1 = sprintf('"%s %s %s/%s"',
request.method,
request.url,
request.protocol.toUpperCase(),
request.httpVersion
)
var head = response._headers || response.headers;
var bytesRead = 0;
if(response.req && response.req.client && response.req.client.bytesRead){
bytesRead = response.req.client.bytesRead;
}
var p2 = sprintf("%d %s",
response.statusCode,
head['content-length'] || bytesRead
);
var p3 = '';
if(conf.format.match('\\+')){
var clusterid = cluster.worker && cluster.worker.id || 'not-a-cluster';
p3 = ' (worker: '+ clusterid + ')';
}
if(conf.format.match('CLF') > 0){
writeToLog(p0 +" "+ p1 +" "+ p2 + p3);
} else if(conf.format.match('EXTENDED')){
writeToLog(p0 +" "+ p1 +" "+ '"'+response.req.headers['user-agent']+'"' +" "+ p2 + p3);
} else {
// default fallback
writeToLog(p0 +" "+ p1 +" "+ p2 + p3);
}
};
/**
* Function to write to the logfile, configured in conf.directory and conf.filename.
* Will open a filehandle if not opend allready.
*
* @scope private
*/
writeToLog = function( str ){
if(fd == undefined){
fd = fs.openSync(path.join(conf.directory, conf.filename), 'a');
}
fs.write( fd, str+"\n", function(err){
if(err){ console.log(err, str); }
});
}