/
log.js
58 lines (48 loc) · 1.2 KB
/
log.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
// document hack
import root from './window-or-global'
(function (base) {
window = base || window
if (!window.document) {
window.document = { documentElement: { style: { WebkitAppearance: true } } }
}
if(!window.localStorage) window.localStorage = {};
})(root);
let debug = () => {};
export class Log {
constructor(namespace, enable) {
this._namespace = namespace || 'firestack';
this.l = null;
this.loggers = {};
this.enabled = false;
this.enable(enable);
}
enable(booleanOrStringDebug) {
window.localStorage.debug =
typeof booleanOrStringDebug === 'string' ?
booleanOrStringDebug :
(booleanOrStringDebug ? '*' : booleanOrStringDebug);
this.enabled = !!window.localStorage.debug;
}
info(...args) {
this._logger('info')(...args);
}
error(...args) {
this._logger('error')(...args);
}
debug(...args) {
this._logger('debug')(...args);
}
_logger(level) {
if (!this.loggers[level]) {
this.loggers[level] = this._debug()(`${this._namespace}:${level}`)
}
return this.loggers[level];
}
_debug() {
if (!this.l) {
this.l = debug = require('debug');
}
return this.l;
}
}
export default Log;