-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (38 loc) · 1.17 KB
/
index.js
File metadata and controls
48 lines (38 loc) · 1.17 KB
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
const debug = require('debug');
const isFunction = require('lodash.isfunction');
const levels = [ 'error', 'warn', 'info', 'debug', 'trace' ];
const funcs = levels.map((level, index) => [level, function (...a) { log.call(this, index, a) }]);
function log(level, args) {
if (level <= active_level) {
this.apply(this, args);
}
}
let active_level = -1;
function set_level(name) {
active_level = levels.indexOf(name);
}
function createDebug(namespace) {
const instance = debug(namespace);
// setup log level functions
funcs.forEach(func => { instance[func[0]] = func[1] });
return instance;
}
// expose all underlying properties on the original debug function
for (let name of Object.keys(debug)) {
const property = debug[name];
if (property === debug) {
createDebug[name] = createDebug;
} else if (isFunction(property)) {
createDebug[name] = property;
} else {
Object.defineProperty(createDebug, name, {
get() { return createDebug[name]; },
set(value) { createDebug[name] = value; }
});
}
}
// expose method to change level
createDebug.level = set_level;
// default to env setting
set_level(process.env['DEBUG_LEVEL'] || 'info');
module.exports = createDebug;