-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
48 lines (43 loc) · 1.45 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
var log4js = require('log4js');
var util = require('util');
var date = require('./lib/date_format.js');
// date, addr, method, url, HTTP/version, content-length, user-agent
var DEFAULT = "%s %s -- %s %s HTTP/%s, %s %s";
/*
* middleware
*/
module.exports = function(opts){
var logger;
if(!opts){
logger = log4js.getLogger(); // 默认使用 console logger
}else{
var loggerName = 'normal';
log4js.configure({
appenders: [
{
type: 'console'
},
{
type: 'file',
filename: opts.file || opts.filename,
maxLogSize: opts.size || 10*1024*1024,
backups: opts.backups || 4,
category: loggerName
}
],
replaceConsole: true
});
logger = log4js.getLogger(loggerName);
}
return function* (next){
var req = this.request, header = req.header, nodeReq = this.req;
var str = util.format(DEFAULT, date.asString(new Date), req.ip, req.method, req.url, nodeReq.httpVersion, req.length || null, header['user-agent']);
logger.debug(str);
yield next;
}
}
// where to get status, referer
// add more info
// 解析配置选项, 创建file logger, 支持log 内容配置
// 从其他middleware中可以获取到 logger
// 日志格式可参看 log4js的 connetct-logger