-
Notifications
You must be signed in to change notification settings - Fork 131
/
Copy pathlog.js
44 lines (37 loc) · 1.13 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
// document hack
import root from './window-or-global'
let bows;
(function (base) {
window = base || window
if(!window.localStorage) window.localStorage = {};
})(root);
const levels = [
'warn', 'info', 'error', 'debug'
];
export class Log {
constructor(namespace) {
this._namespace = namespace || 'firestack';
this.loggers = {};
// Add the logging levels for each level
levels
.forEach(level => this[level] = (...args) => this._log(level)(...args));
}
static enable(booleanOrStringDebug) {
window.localStorage.debug =
typeof booleanOrStringDebug === 'string' ?
(booleanOrStringDebug === '*' ? true : booleanOrStringDebug) :
(booleanOrStringDebug instanceof RegExp ? booleanOrStringDebug.toString() : booleanOrStringDebug);
window.localStorage.debugColors = !!window.localStorage.debug;
}
_log(level) {
if (!this.loggers[level]) {
(function() {
const bows = require('bows');
bows.config({ padLength: 20 });
this.loggers[level] = bows(this._namespace, `[${level}]`);
}.bind(this))();
}
return this.loggers[level];
}
}
export default Log;