-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (40 loc) · 1.44 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
var default_reqfmt = null;
var default_resfmt = null;
module.exports.logger = function (options) {
options = options || {};
var reqfmt = options.reqfmt;
if (!reqfmt) {
var reqtpl = options.reqtpl;
if (!reqtpl) {
if (!default_reqfmt) {
default_reqfmt = require('dot').template(require('curly-colors')('<{magenta>{{=it.name}}<}> << <{yellow>{{=it.method}}<}> {{=it.path}}{{? it.data}} {{=JSON.stringify(it.data, undefined, 2)}}{{?}}'));
}
reqfmt = default_reqfmt;
} else {
reqfmt = require('dot').template(require('curly-colors')(reqtpl));
}
}
var resfmt = options.resfmt;
if (!resfmt) {
var restpl = options.restpl;
if (!restpl) {
if (!default_resfmt) {
default_resfmt = require('dot').template(require('curly-colors')('<{magenta>{{=it.req.options.name}}<}> >> <{yellow>{{=it.req.method}}<}> {{=it.req.path}} {{? it.res.statusCode <= 299}}<{green>{{=it.res.statusCode}}<}>{{??}}<{red>{{=it.res.statusCode}}<}>{{?}}{{? it.res.data}} {{=JSON.stringify(it.res.data, undefined, 2)}}{{?}}'));
}
resfmt = default_resfmt;
} else {
resfmt = require('dot').template(require('curly-colors')(restpl));
}
}
var log = options.log || console.log;
return {
before: function (options, next) {
log(reqfmt(options));
return next();
},
after: function (req, res, next) {
log(resfmt({ req: req, res: res }));
return next();
}
};
};