-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurableLogger.js
More file actions
72 lines (65 loc) · 2.8 KB
/
ConfigurableLogger.js
File metadata and controls
72 lines (65 loc) · 2.8 KB
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
/* eslint-disable import/extensions */
import DelegatingLogger from './DelegatingLogger.js';
import Logger from './Logger.js';
export default class ConfigurableLogger extends DelegatingLogger {
static DEFAULT_CONFIG_PATH = 'logging.level';
constructor(config, provider, category, configPath, cache) {
super(provider);
this.config = config;
if (!this.config) {
throw new Error('config is required');
}
this.category = category || Logger.DEFAULT_CATEGORY;
this.configPath = configPath || ConfigurableLogger.DEFAULT_CONFIG_PATH;
this.cache = cache;
if (!this.cache) {
throw new Error('cache is required');
}
this.provider.setLevel(
ConfigurableLogger.getLoggerLevel(
this.category,
this.configPath,
this.config,
this.cache,
),
);
ConfigurableLogger.prototype.setLevel = DelegatingLogger.prototype.setLevel;
ConfigurableLogger.prototype.log = DelegatingLogger.prototype.log;
ConfigurableLogger.prototype.debug = DelegatingLogger.prototype.debug;
ConfigurableLogger.prototype.verbose = DelegatingLogger.prototype.verbose;
ConfigurableLogger.prototype.info = DelegatingLogger.prototype.info;
ConfigurableLogger.prototype.warn = DelegatingLogger.prototype.warn;
ConfigurableLogger.prototype.error = DelegatingLogger.prototype.error;
ConfigurableLogger.prototype.fatal = DelegatingLogger.prototype.fatal;
ConfigurableLogger.prototype.isLevelEnabled = DelegatingLogger.prototype.isLevelEnabled;
ConfigurableLogger.prototype.isDebugEnabled = DelegatingLogger.prototype.isDebugEnabled;
ConfigurableLogger.prototype.isVerboseEnabled = DelegatingLogger.prototype.isVerboseEnabled;
ConfigurableLogger.prototype.isInfoEnabled = DelegatingLogger.prototype.isInfoEnabled;
ConfigurableLogger.prototype.isWarnEnabled = DelegatingLogger.prototype.isWarnEnabled;
ConfigurableLogger.prototype.isErrorEnabled = DelegatingLogger.prototype.isErrorEnabled;
ConfigurableLogger.prototype.isFatalEnabled = DelegatingLogger.prototype.isFatalEnabled;
}
static getLoggerLevel(category, configPath, config, cache) {
let level = 'info';
const path = configPath || ConfigurableLogger.DEFAULT_CONFIG_PATH;
const categories = (category || '').split('/');
let pathStep = path;
const root = `${pathStep}./`;
if (cache.get(root)) {
level = cache.get(root);
} else if (config.has(root)) {
level = config.get(root);
cache.put(root, level);
}
for (let i = 0; i < categories.length; i++) {
pathStep = `${pathStep}${i === 0 ? '.' : '/'}${categories[i]}`;
if (cache.get(pathStep)) {
level = cache.get(pathStep);
} else if (config.has(pathStep)) {
level = config.get(pathStep);
cache.put(pathStep, level);
}
}
return level;
}
}