-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
741 lines (678 loc) · 26.2 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
var fs = require('fs'),
path = require('path'),
domain = require('domain'),
url = require('url'),
crypto = require('crypto');
const callsites = require('callsites');
const {mkdirp} = require('mkdirp');
var util = require('./lib/util.js'),
colors = require('colors');
var data_path = __dirname + '/'; //模板地址默认在模块里
var log_path = __dirname + '/log';
var LOGGER_CACHE = {};
var LOGFILE_CACHE = {};
//日志等级
var LEVELS = {
0: 'ACCESS',
3: 'ACCESS_ERROR',
//应用日志等级 ODP格式
1: 'FATAL',
2: 'WARNING',
4: 'NOTICE',
8: 'TRACE',
16: 'DEBUG'
};
var LEVELS_REVERSE = {};
for (var num in LEVELS) {
LEVELS_REVERSE[LEVELS[num]] = num;
}
//debug模式下应用日志等级对应的颜色
var COLORS = {
1: 'red',
2: 'yellow',
3: 'magenta',
4: 'grey',
8: 'cyan',
16: 'grey'
};
var Logger = function (opts, req) {
//模板文件地址,可以不填
if (opts && opts['data_path']) {
data_path = opts['data_path'];
}
//用户只需要填写log_path配置
if (opts && opts['log_path']) {
log_path = opts['log_path'];
}
this.req = req || {
headers: {}
};
this.opts = this.extend(
{
stdout_only: false, // 只输出 stdout,不写文件
debug: 0,
intLevel: 16,
auto_rotate: 1,
use_sub_dir: 1,
IS_ODP: true,
IS_OMP: 0,
IS_JSON: false,
log_path: log_path,
access_log_path: log_path + '/access',
access_error_log_path: log_path + '/access',
data_path: data_path + 'data'
},
opts
);
//保存一次错误及请求的详情信息
this.params = {};
//var format_wf = '%L: %{%m-%d %H:%M:%S}t %{app}x * %{pid}x [logid=%l filename=%f lineno=%N errno=%{err_no}x %{encoded_str_array}x errmsg=%{u_err_msg}x]';
//[10/Jun/2014:22:01:35 +0800]
//应用日志格式,默认wf日志与default日志一样
this.format = {
ACCESS:
this.opts['access'] ||
'%h - - [%{%d/%b/%Y:%H:%M:%S %Z}t] "%m %U %H/%{http_version}i" %{status}i %b %{Referer}i %{Cookie}i %{User-Agent}i %D',
ACCESS_ERROR:
'%h - - [%{%d/%b/%Y:%H:%M:%S %Z}t] "%m %U %H/%{http_version}i" %{status}i %b %{Referer}i %{Cookie}i %{User-Agent}i %D',
WF:
this.opts['format_wf'] ||
'%L: %t [%f:%N] errno[%E] logId[%l] uri[%U] user[%u] refer[%{referer}i] cookie[%{cookie}i] custom[%{encoded_str_array}x] %S %M',
DEFAULT:
this.opts['format'] ||
'%L: %t [%f:%N] errno[%E] logId[%l] uri[%U] user[%u] refer[%{referer}i] cookie[%{cookie}i] custom[%{encoded_str_array}x] %S %M',
STD: '%L: %{%m-%d %H:%M:%S}t %{app}x * %{pid}x [logid=%l filename=%f lineno=%N errno=%{err_no}x %{encoded_str_array}x errmsg=%{u_err_msg}x]',
STD_DETAIL:
'%L: %{%m-%d %H:%M:%S}t %{app}x * %{pid}x [logid=%l filename=%f lineno=%N errno=%{err_no}x %{encoded_str_array}x errmsg=%{u_err_msg}x cookie=%{u_cookie}x]'
};
};
Logger.prototype = {
fatal: function () {
return this.log.call(this, 'FATAL', arguments[0]);
},
notice: function () {
return this.log.call(this, 'NOTICE', arguments[0]);
},
trace: function () {
return this.log.call(this, 'TRACE', arguments[0]);
},
warning: function () {
return this.log.call(this, 'WARNING', arguments[0]);
},
debug: function () {
return this.log.call(this, 'DEBUG', arguments[0]);
},
//level表示日志I等级,obj表示错误消息或者错误选项
log: function (level, obj) {
level = String(level).toUpperCase(); // WARNING格式
var intLevel = this.getLogLevelInt(level); // 2格式
var format = this.getLogFormat(level);
if (intLevel < 0 || !format) {
return false;
}
var option = {};
if (obj) {
if (typeof obj === 'string') {
option['msg'] = obj;
} else if (typeof obj === 'object') {
option = obj;
}
}
//解析错误堆栈信息
this.parseStackInfo(option);
//解析自定义字段,存放在对应ODP的 encoded_str_array中
this.params['encoded_str_array'] = '';
if (option['custom']) {
this.parseCustomLog(option['custom']);
}
if (intLevel === 0 || intLevel === 3) {
//访问日志
this.writeLog(intLevel, option, format);
} else {
//IS_OMP等于0打印两种格式日志,等于1打印STD日志,等于2打印WF/Default日志
if (this.opts['IS_OMP'] === 0 || this.opts['IS_OMP'] === 2) {
option['filename_suffix'] = '';
option['escape_msg'] = false; //错误消息不转义
this.writeLog(intLevel, option, format);
}
if (this.opts['IS_OMP'] === 0 || this.opts['IS_OMP'] === 1) {
option['filename_suffix'] = '.new';
option['escape_msg'] = true; //错误消息转义
this.writeLog(intLevel, option, this.format['STD']);
}
}
},
extend: function (destination, source) {
for (var property in source) {
if (source.hasOwnProperty(property)) {
destination[property] = source[property];
}
}
return destination;
},
getLogFormat: function (level) {
level = level.toUpperCase();
var formats = this.format;
if (this.getLogLevelInt(level) < 0) {
return false;
}
var format = formats['DEFAULT']; //默认格式,
// ACCESS为访问格式
if (level === 'ACCESS') {
format = formats['ACCESS'];
} else if (level === 'ACCESS_ERROR') {
format = formats['ACCESS_ERROR']; //访问错误 404 301等单独存储
} else {
//warning和fatal格式不一样,且单独存储
if (level === 'WARNING' || level === 'FATAL') {
format = formats['WF'];
}
}
return format;
},
/**
* 解析错误信息,获取函数名文件行数等
* @param {[type]} option [description]
* @return {[type]} [description]
*/
parseStackInfo: function (option) {
this.params['errno'] = option['errno'] || 0; //错误号
this.params['error_msg'] = option['msg'] || ''; //自定义错误信息,%M默认不转义
this.params['TypeName'] = '';
this.params['FunctionName'] = '';
this.params['MethodName'] = '';
this.params['FileName'] = '';
this.params['LineNumber'] = '';
this.params['isNative'] = '';
// 如果没有传msg,但是传了stack,则用stack信息替代msg
if (option['stack']) {
try {
if (!option['msg']) {
this.params['error_msg'] = this.opts['debug']
? option['stack']
: String(option['stack']).replace(/(\n)+|(\r\n)+/g, ' ');
}
} catch (e) {
//this.log('notice','wrong error obj');
}
}
},
//解析自定义字段,'custom'字段
parseCustomLog: function (obj) {
if ('object' !== typeof obj) {
return false;
}
var items = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
items.push(escape(key) + '=' + escape(obj[key]));
}
}
if (items.length > 0) {
this.params['encoded_str_array'] = items.join(' ');
}
},
//初始化请求相关的参数
parseReqParams: function (req, res) {
if (!req || !req.headers || !res) {
return false;
}
this.params['CLIENT_IP'] =
req.headers['x-forwarded-for'] || req.connection.remoteAddress || req.headers['x-real-ip'];
this.params['REFERER'] = req.headers['referer'];
this.params['COOKIE'] = req.headers['cookie'];
this.params['USER_AGENT'] = req.headers['user-agent'];
this.params['SERVER_ADDR'] = req.headers.host;
this.params['SERVER_PROTOCOL'] = String(req.protocol).toUpperCase();
this.params['REQUEST_METHOD'] = req.method || '';
this.params['SERVER_PORT'] = req.app.settings ? req.app.settings.port : '';
this.params['QUERY_STRING'] = this.getQueryString(req.originalUrl);
this.params['REQUEST_URI'] = req.originalUrl;
this.params['REQUEST_PATHNAME'] = req._parsedUrl ? req._parsedUrl.pathname : '-';
this.params['REQUEST_QUERY'] = req._parsedUrl ? req._parsedUrl.query : '-';
this.params['HOSTNAME'] = req.hostname;
this.params['HTTP_HOST'] = req.headers.host;
this.params['HTTP_VERSION'] = req.httpVersionMajor + '.' + req.httpVersionMinor;
this.params['STATUS'] = res.statusCode ? res.statusCode : null;
this.params['CONTENT_LENGTH'] = (res.getHeaders() || {})['content-length'] || '-';
this.params['BYTES_SENT'] = res.socket ? res.socket.bytesRead : 0;
this.params['HEADERS'] = req.headers;
this.params['pid'] = process.pid;
},
/**
* 获取请求的querystring
*
* @param {string} url 请求 url
* @return {string} querystring
*/
getQueryString(rawUrl) {
try {
var urlObj = url.parse(rawUrl);
return urlObj.query || '';
} catch (e) {
console.log(e);
}
return '';
},
/**
* ODP环境下日志的前缀为AppName,非ODP环境需要配置指定前缀
* @return {[string]} [description]
*/
getLogPrefix: function () {
if (this.opts.autoAppName && this.req && this.req.CURRENT_APP) {
return this.req.CURRENT_APP;
}
if (this.opts['IS_ODP'] === true) {
return this.opts['app'];
}
return 'unknow';
},
//获取logID,如果没有生成唯一随机数
getLogID: function (req, logIDName) {
var logId = 0;
if (this.params['LogId']) {
return this.params['LogId'];
}
if (req) {
if (req.headers[logIDName]) {
logId = parseInt(req.headers[logIDName], 10);
} else if (parseInt(req.query['logid'], 10) > 0) {
logId = parseInt(req.query['logid'], 10);
} else if (parseInt(this.getCookie('logid'), 10) > 0) {
logId = parseInt(this.getCookie('logid'), 10);
}
}
if (logId === 0) {
var obj = util.gettimeofday();
logId = (obj['sec'] * 100000 + obj['usec'] / 10 + Math.floor(Math.random() * 100)) & 0x7fffffff;
}
return logId;
},
//获取日志文件地址。注意访问日志与应用日志的差异
getLogFile: function (intLevel) {
var prefix = this.getLogPrefix() || 'yog';
var logFile = '',
log_path = '';
switch (intLevel) {
case '0': //访问日志前缀默认access
logFile = this.opts['access_log_file'] || 'access'; //访问日志
log_path = this.opts['access_log_path'] || this.opts['log_path'];
break;
case '3':
logFile = this.opts['access_error_log_file'] || 'error'; //访问日志
log_path = this.opts['access_error_log_path'] || this.opts['log_path'];
break;
default:
//错误日志为app前缀
//是否使用子目录,app区分
log_path = this.opts['use_sub_dir'] ? this.opts['log_path'] + '/' + prefix : this.opts['log_path'];
logFile = prefix;
}
return log_path + '/' + logFile + '.log';
},
/**
* 写入信息到日志文件中,异步方式
* @param {[type]} intLevel [日志等级,整数]
* @param {[type]} options [写入选项]
* @param {[type]} log_format [日志格式]
* @return {[type]} [description]
*/
writeLog: function (intLevel, options, log_format) {
//日志等级高于配置则不输出日志
if ((intLevel > this.opts['intLevel'] || !LEVELS[intLevel]) && !this.opts['debug']) {
return false;
}
this.params['current_level'] = LEVELS[intLevel];
//日志文件名称
var logFile = this.getLogFile(intLevel),
filename_suffix = options['filename_suffix'] || '',
errno = options['errno'] || 0;
if (this.getLogLevelInt('WARNING') === intLevel || this.getLogLevelInt('FATAL') === intLevel) {
logFile += '.wf';
}
//文件后缀
logFile += filename_suffix;
var logFileType = logFile;
//是否按小时自动切分
if (this.opts['auto_rotate']) {
logFile += '.' + util.strftime(new Date(), '%Y%m%d%H');
}
//STD日志需将错误日志转义
if (this.params['error_msg'] && !this.opts['debug']) {
this.params['error_msg'] =
options['escape_msg'] === true ? escape(this.params['error_msg']) : unescape(this.params['error_msg']);
}
var format = log_format || this.format['DEFAULT'];
var str = this.getLogString(format, options);
if (!str) {
return false;
}
// stdout_only 主要是给容器化部署用的,开启后也写入控制台,但没颜色
if (this.opts['stdout_only']) {
console.log(str);
// debug 模式,console.log输出颜色标记的日志
} else if (this.opts['debug'] && COLORS[intLevel] && process.env.NODE_ENV === 'dev') {
var color = COLORS[intLevel];
var _str = unescape(str);
console.log(_str[color]);
}
if (!LOGFILE_CACHE[logFileType]) {
LOGFILE_CACHE[logFileType] = {};
}
// 获取此类文件的FD缓存
var fdCache = LOGFILE_CACHE[logFileType];
if (!fdCache[logFile] && !this.opts['stdout_only']) {
// 关闭老的日志流
for (var oldFile in fdCache) {
if (fdCache.hasOwnProperty(oldFile)) {
try {
fdCache[oldFile].end();
} catch (e) {}
delete fdCache[oldFile];
}
}
var pathname = path.dirname(logFile);
if (!fs.existsSync(pathname)) {
mkdirp.sync(pathname);
}
fdCache[logFile] = fs.createWriteStream(logFile, {
flags: 'a'
});
}
if (!this.opts['stdout_only']) {
fdCache[logFile].write(str);
}
},
//获取字符串标识对应的日志等级,没有返回-1
getLogLevelInt: function (level) {
return LEVELS_REVERSE[level] || -1;
},
/**
* 获取日志字符串,,执行模板函数读取日志数据
* @return {[type]} [description]
*/
getLogString: function (format, options) {
if (this.opts['IS_JSON']) {
const caller = callsites()[3];
const callerFileName = caller.getFileName();
const callerFunctionName = caller.getFunctionName();
return (
JSON.stringify({
date: new Date().toJSON(),
level: this.params['current_level'],
msg: options['msg'],
file: callerFileName,
function: callerFunctionName,
uri: this.params['REQUEST_URI'] || ''
}) + '\n'
);
}
if (!format) {
return false;
}
if (!LOGGER_CACHE[format]) {
try {
var jsStr = this.parseFormat(format); //获取各个format对应的js执行函数字符串
LOGGER_CACHE[format] = requireFromString(jsStr);
} catch (e) {
console.error(e);
//mail("生成日志模板js失败");
return false;
}
}
return LOGGER_CACHE[format](this, util) + '\n';
},
//生产环境是否应当使用
md5: function (data, len) {
var md5sum = crypto.createHash('md5'),
encoding = typeof data === 'string' ? 'utf8' : 'binary';
md5sum.update(data, encoding);
len = len || 10;
return md5sum.digest('hex').substring(0, len);
},
//解析日志配置,生成相应的模板函数
parseFormat: function (format) {
var regex = /%(?:{([^}]*)})?(.)/g;
var m;
var action = [];
while ((m = regex.exec(format)) != null) {
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
var code = m[2],
param = m[1];
switch (code) {
case 'h':
action.push("logger.getParams('CLIENT_IP')");
break;
case 't':
var _act = "util.strftime(new Date(),'%y-%m-%d %H:%M:%S')";
if (param && param !== '') {
_act = "util.strftime(new Date(), '" + String(param) + "')";
}
action.push(_act);
break;
case 'i':
var key = String(param).toUpperCase().replace(/-/g, '_');
action.push("logger.getParams('" + key + "')");
break;
// 不转换参数格式, 方便获取自定义 header 参数
case 'I':
action.push("logger.getParams('" + param + "')");
break;
case 'a':
action.push("logger.getParams('CLIENT_IP')");
break;
case 'A':
action.push("logger.getParams('SERVER_ADDR')");
break;
case 'b':
action.push("logger.getParams('CONTENT_LENGTH')");
break;
case 'C':
if (param === '') {
action.push("logger.getParams('HTTP_COOKIE')");
} else {
action.push("logger.getCookie('" + param + "')");
}
break;
case 'D':
//暂不确定计算准确
action.push("logger.getParams('REQUEST_TIME')");
break;
case 'e':
//TODO
action.push("''");
break;
case 'f':
action.push("logger.getParams('FileName')");
break;
case 'H':
action.push("logger.getParams('SERVER_PROTOCOL')");
break;
case 'm':
action.push("logger.getParams('REQUEST_METHOD')");
break;
case 'p':
action.push("logger.getParams('SERVER_PORT')");
break;
case 'q':
action.push("logger.getParams('QUERY_STRING')");
break;
case 'T':
//TODO
action.push("''");
break;
case 'U':
action.push("logger.getParams('REQUEST_URI')");
break;
case 'v':
action.push("logger.getParams('HOSTNAME')");
break;
case 'V':
action.push("logger.getParams('HTTP_HOST')");
break;
case 'L':
action.push("logger.getParams('current_level')");
break;
case 'N':
action.push("logger.getParams('LineNumber')");
break;
case 'E':
action.push("logger.getParams('errno')");
break;
case 'l':
action.push("logger.getParams('LogId')");
break;
case 'u':
//TODO 用户ID 用户名
action.push("logger.getParams('user')");
break;
case 'S':
//TODO
action.push("''");
break;
case 'M':
action.push("logger.getParams('error_msg')");
break;
case 'x':
//TODO
if (param.indexOf('u_') == 0) {
param = param.substr(2);
}
switch (param) {
case 'log_level':
action.push("logger.getParams('current_level')");
break;
case 'line':
action.push("logger.getParams('LineNumber')");
break;
case 'function':
action.push("logger.getParams('FunctionName')");
break;
case 'err_no':
action.push("logger.getParams('errno')");
break;
case 'err_msg':
action.push("logger.getParams('error_msg')");
break;
case 'log_id':
action.push("logger.getParams('LogId')");
break;
case 'app':
action.push('logger.getLogPrefix()');
break;
/*case 'function_param':
$action[] = 'Bd_Log::flattenArgs(Bd_Log::$current_instance->current_function_param)';
break;*/
/*case 'argv':
$action[] = '(isset($GLOBALS["argv"])? Bd_Log::flattenArgs($GLOBALS["argv"]) : \'\')';
break;*/
case 'pid':
action.push("logger.getParams('pid')");
break;
case 'encoded_str_array':
action.push("logger.getParams('encoded_str_array')");
break;
case 'cookie':
action.push("logger.getParams('cookie')");
break;
default:
action.push("''");
}
break;
case '%':
action.push("'%'");
break;
default:
action.push("''");
}
}
var strformat = util.preg_split(regex, format);
var logCode = "'" + strformat[0] + "'";
for (var i = 1; i < strformat.length; i++) {
logCode = logCode + ' + ' + action[i - 1] + " + '" + strformat[i] + "'";
}
var cmt = '/* Used for app ' + this.opts['app'] + '\n';
cmt += ' * Original format string:' + format.replace(/\*\//g, '* /');
var str = cmt + '*/ \n module.exports=function(logger, util){\n return ' + logCode + '; \n}';
return str;
},
getParams: function (name) {
if (this.params.hasOwnProperty(name) && this.params[name] !== undefined && this.params[name] !== '') {
return this.params[name];
}
name = name.toLowerCase();
if (this.params.HEADERS && this.params.HEADERS.hasOwnProperty(name) && this.params.HEADERS[name]) {
return this.params.HEADERS[name];
}
return '-';
},
setParams: function (name, value) {
this.params[name] = value;
},
getCookie: function (name) {
name = String(name).replace(/(^\s*)|(\s*$)/g, '');
var match = String(this.getParams('COOKIE')).match(new RegExp(name + '=([^;]+)'));
if (match) {
return match[1];
}
return false;
}
};
function requireFromString(src, filename) {
var Module = module.constructor;
var m = new Module();
m._compile(src, filename || 'whatever');
return m.exports;
}
module.exports = function (config) {
config = config || {};
return function (req, res, next) {
var current;
var logger;
current = domain.create();
logger = new Logger(config, req);
current.add(logger);
current.logger = logger; // Add request object to custom property
function logRequest() {
res.removeListener('finish', logRequest);
res.removeListener('close', logRequest);
//以下参数需要在response finish的时候计算
logger.params['STATUS'] = res.statusCode ? res.statusCode : null;
logger.params['CONTENT_LENGTH'] = (res.getHeaders() || {})['content-length'] || '-';
if (req._startAt) {
var diff = process.hrtime(req._startAt);
var ms = diff[0] * 1e3 + diff[1] * 1e-6;
logger.params['REQUEST_TIME'] = ms.toFixed(3);
}
//不区分访问错误日志
logger.log('ACCESS');
}
//只在请求过来的时候才设置LogId
logger.params['LogId'] = logger.getLogID(req, config.LogIdName || 'x_bd_logid');
logger.parseReqParams(req, res);
//response-time启动埋点
req._startAt = process.hrtime();
res.once('finish', logRequest);
res.once('close', logRequest);
res.on('log', function (e, level) {
var option = e || {};
level = level || 'notice';
// logger.parseReqParams(req, res);
logger.log(level, option);
});
//只要url设置了_node_debug参数,则开启debug模式,console.log输出日志
if (req.query && req.query._node_debug) {
logger.opts['debug'] = 1;
}
current.run(next);
};
};
module.exports.Logger = Logger;
module.exports.getLogger = function (config) {
if (process.domain && process.domain.logger) {
return process.domain.logger;
}
return new Logger(config);
};