Skip to content

Commit 227798d

Browse files
committed
cleanup logger
1 parent 384fa16 commit 227798d

File tree

1 file changed

+27
-29
lines changed

1 file changed

+27
-29
lines changed

lib/utils/log.js

Lines changed: 27 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,42 @@
1-
// document hack
2-
import root from './window-or-global';
31

4-
let bows;
5-
(function (base) {
6-
window = base || window
7-
if (!window.localStorage) window.localStorage = {};
8-
})(root);
92

10-
const levels = [
11-
'warn', 'info', 'error', 'debug'
12-
];
3+
import { windowOrGlobal } from './';
4+
5+
((base) => {
6+
window = base || window;
7+
if (!window.localStorage) window.localStorage = {};
8+
})(windowOrGlobal);
139

14-
class Log {
10+
export default class Log {
1511
constructor(namespace) {
1612
this._namespace = namespace || 'firestack';
13+
require('bows').config({ padLength: 20 });
1714
this.loggers = {};
18-
// Add the logging levels for each level
19-
levels
20-
.forEach(level => this[level] = (...args) => this._log(level)(...args));
2115
}
2216

23-
static enable(booleanOrStringDebug) {
24-
window.localStorage.debug =
25-
typeof booleanOrStringDebug === 'string' ?
26-
(booleanOrStringDebug === '*' ? true : booleanOrStringDebug) :
27-
(booleanOrStringDebug instanceof RegExp ? booleanOrStringDebug.toString() : booleanOrStringDebug);
17+
get warn() {
18+
return this._createOrGetLogger('warn');
19+
}
20+
21+
get info() {
22+
return this._createOrGetLogger('info');
23+
}
2824

25+
get error() {
26+
return this._createOrGetLogger('error');
27+
}
28+
29+
get debug() {
30+
return this._createOrGetLogger('debug');
31+
}
32+
33+
static enable(booleanOrStringDebug) {
34+
window.localStorage.debug = booleanOrStringDebug;
2935
window.localStorage.debugColors = !!window.localStorage.debug;
3036
}
3137

32-
_log(level) {
33-
if (!this.loggers[level]) {
34-
(function () {
35-
const bows = require('bows');
36-
bows.config({ padLength: 20 });
37-
this.loggers[level] = bows(this._namespace, `[${level}]`);
38-
}.bind(this))();
39-
}
38+
_createOrGetLogger(level) {
39+
if (!this.loggers[level]) this.loggers[level] = require('bows')(this._namespace, `[${level}]`);
4040
return this.loggers[level];
4141
}
4242
}
43-
44-
export default Log;

0 commit comments

Comments
 (0)