-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnullish-logger.js
75 lines (61 loc) · 1.39 KB
/
nullish-logger.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
class NullishLogger {
#enabled = true;
#quiet = true;
#suppress = ['info', 'warn'];
constructor() {
this.#methods = Object.create(console);
this.#configure();
}
#instance = null;
#methods = null;
#noop = () => {};
#configure() {
this.#instance = this.#enabled ? this.#methods : null;
if (this.#instance && this.#quiet) {
this.suppress.forEach(method => {
this.#instance[method] = this.#noop;
});
} else if (this.#instance) {
this.suppress.forEach(method => {
this.#instance[method] = console[method].bind(console);
});
}
}
get suppress() {
return [...this.#suppress];
}
set suppress(suppressed) {
if (!Array.isArray(suppressed)) {
throw new TypeError('Must provide an array of strings');
}
if (!suppressed.every(method => typeof method === 'string')) {
throw new TypeError('Must provide an array of strings');
}
this.#suppress = [...suppressed];
this.#configure();
}
get logger() {
return this.#instance;
}
get enabled() {
return this.#enabled;
}
set enabled(val) {
this.#enabled = !!val;
this.#configure();
}
get quiet() {
return this.#quiet;
}
set quiet(val) {
this.#quiet = !!val;
this.#configure();
}
}
const instance = new NullishLogger();
const debug = instance.logger;
export {
NullishLogger,
instance,
debug
}