-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
171 lines (131 loc) · 3.63 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
'use strict';
var x51 = require('x51');
var LEVEL_MAP = {
trace: 10,
debug: 20,
info: 30,
off: Infinity
};
module.exports = function(opts) {
var _level;
setLogLevel(opts.level);
if (!_level) {
throw new Error('Must provide a valid level attribute: ', Object.keys(LEVEL_MAP).join(' | '));
}
var _captureErrors = (opts.captureErrors === false) ? false : true;
var _x51 = x51({
flush: opts.flush,
flushInterval: opts.flushInterval,
maxRecords: opts.maxRecords,
log: opts.log
});
// Public API
var self = {
newMonitor: newMonitor,
flush: flush,
setLogLevel: setLogLevel,
logEnabled: logEnabled
};
return self;
////
function newMonitor(monitorName) {
var serviceName = _formatFileName(monitorName);
// Everything placed on fn is public API
var fn = _monitor.bind(null, 'info', serviceName);
fn.trace = _monitor.bind(null, 'trace', serviceName);
fn.debug = _monitor.bind(null, 'debug', serviceName);
fn.info = _monitor.bind(null, 'info', serviceName);
fn.logTime = function(logLevel, fnName, startTime, endTime) {
if (!logEnabled(logLevel)) {
return;
}
return _logTime(serviceName, logLevel, fnName, startTime, endTime);
};
return fn;
}
function flush() {
return _x51.flush();
}
function setLogLevel(level) {
var lvl = LEVEL_MAP[level || 'info'];
if (!lvl) {
throw new Error('Must provide a valid level attribute: ', Object.keys(LEVEL_MAP).join(' | '));
}
_level = lvl;
}
function logEnabled(logLevel) {
var lvl = LEVEL_MAP[logLevel] || Infinity;
return (lvl >= _level);
}
// Private functions
function _formatFileName(fileName) {
var withoutRoot = fileName.replace(process.cwd(), '');
var fileExt = withoutRoot.lastIndexOf('.');
return (fileExt > -1) ? withoutRoot.substr(0, fileExt) : withoutRoot;
}
function _monitor(logLevel, serviceName, fnName, fn) {
if (typeof fnName === 'function') {
fn = fnName;
fnName = fn.name;
}
function finish(start, err) {
var params;
if (err && _captureErrors) {
params = {
err: err
};
}
_logTime(serviceName, logLevel, fnName, start, Date.now(), params);
}
return function() {
if (!logEnabled(logLevel)) {
return fn.apply(undefined, arguments);
}
var start = Date.now();
if (typeof arguments[arguments.length - 1] === 'function') {
var args = Array.prototype.slice.call(arguments);
// Async callback function
var callback = args.pop();
args.push(function(err) {
finish(start, err);
callback.apply(undefined, arguments);
});
return fn.apply(undefined, args);
} else {
var res;
try {
res = fn.apply(undefined, arguments);
} catch (err) {
finish(start, err);
throw err;
}
if (res && res.then && res.catch) {
// Probably (hopefully) a promise
return res.then(function(data) {
finish(start);
return data;
})
.catch(function(err) {
finish(start, err);
throw err;
});
} else {
// Synchronous function
finish(start);
return res;
}
}
};
}
function _logTime(monitorName, logLevel, fnName, startTime, endTime, params) {
var item = {
service: monitorName,
fnName: fnName,
time: endTime - startTime,
level: logLevel,
created: new Date(startTime),
params: params || {}
};
_x51.push(item);
}
};