-
-
Notifications
You must be signed in to change notification settings - Fork 629
/
logger.js
68 lines (54 loc) · 1.35 KB
/
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
const consoleLogger = {
type: 'logger',
log(args) {
this.output('log', args);
},
warn(args) {
this.output('warn', args);
},
error(args) {
this.output('error', args);
},
output(type, args) {
/* eslint no-console: 0 */
if (console && console[type]) console[type](...args);
},
};
class Logger {
constructor(concreteLogger, options = {}) {
this.init(concreteLogger, options);
}
init(concreteLogger, options = {}) {
this.prefix = options.prefix || 'i18next:';
this.logger = concreteLogger || consoleLogger;
this.options = options;
this.debug = options.debug;
}
setDebug(bool) {
this.debug = bool;
}
log(...args) {
return this.forward(args, 'log', '', true);
}
warn(...args) {
return this.forward(args, 'warn', '', true);
}
error(...args) {
return this.forward(args, 'error', '');
}
deprecate(...args) {
return this.forward(args, 'warn', 'WARNING DEPRECATED: ', true);
}
forward(args, lvl, prefix, debugOnly) {
if (debugOnly && !this.debug) return null;
if (typeof args[0] === 'string') args[0] = `${prefix}${this.prefix} ${args[0]}`;
return this.logger[lvl](args);
}
create(moduleName) {
return new Logger(this.logger, {
...{ prefix: `${this.prefix}:${moduleName}:` },
...this.options,
});
}
}
export default new Logger();