forked from lsongdev/kelp-logger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
57 lines (56 loc) · 1.1 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
'use strict';
/**
* [STATUS_CODES description]
* @type {Object}
*/
const STATUS_CODES = {
2: 32,
3: 36,
4: 33,
5: 31
};
/**
* [color description]
* @param {[type]} str [description]
* @param {[type]} c [description]
* @return {[type]} [description]
*/
function color(str, c){
return "\x1b[" + c + "m" + str + "\x1b[0m";
};
// logger
module.exports = function(req, res, next){
var err = null;
var end = res.end;
var start = new Date;
var respod = false;
function commit(){
var cc = STATUS_CODES[ res.statusCode / 100 | 0 ];
console.log(
err && !respod ? '-x' : '->' ,
color(req.method , 35),
color(req.url , 90),
color(res.statusCode , cc),
color((new Date - start) + "ms" , 90)
);
};
/**
* [function description]
* @return {[type]} [description]
*/
res.end = function(){
respod = true;
end.apply(res, arguments);
commit();
};
try{
next();
}catch(e){
err = e;
if(!respod){
res.statusCode = 500;
commit();
}
throw err;
}
};