-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggerFactory.js
More file actions
141 lines (129 loc) · 4.52 KB
/
LoggerFactory.js
File metadata and controls
141 lines (129 loc) · 4.52 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* eslint-disable import/extensions */
import ConfigurableLogger from './ConfigurableLogger.js';
import ConsoleLogger from './ConsoleLogger.js';
import LoggerCategoryCache from './LoggerCategoryCache.js';
import JSONFormatter from './JSONFormatter.js';
import PlainTextFormatter from './PlainTextFormatter.js';
export default class LoggerFactory {
static loggerCategoryCache = new LoggerCategoryCache();
static getGlobalRef() {
let $globalref = null;
if (LoggerFactory.detectBrowser()) {
$globalref = window;
} else {
$globalref = global;
}
return $globalref;
}
static getGlobalRoot(key) {
const $globalref = LoggerFactory.getGlobalRef();
let $key = ($globalref && $globalref.boot);
$key = $key && $key.contexts;
$key = $key && $key.root;
$key = $key && $key[`${key}`];
return $key;
}
static detectBrowser() {
const browser = !(typeof window === 'undefined');
return browser;
}
static detectConfig(configArg) {
let $config = null;
if (!(typeof config === 'undefined')) {
// eslint-disable-next-line no-undef
$config = config;
}
if (LoggerFactory.getGlobalRoot('config')) {
$config = LoggerFactory.getGlobalRoot('config');
}
if (LoggerFactory.detectBrowser() && window?.config) {
$config = window.config;
}
$config = configArg || $config;
if ($config) {
return $config;
}
throw new Error('Unable to detect config, is \'config\' declared or provided?');
}
static detectLoggerFactory() {
let $loggerFactory = null;
if (!(typeof loggerFactory === 'undefined')) {
// eslint-disable-next-line no-undef
$loggerFactory = loggerFactory;
}
if (!(typeof global === 'undefined') && global?.boot?.contexts?.root?.loggerFactory) {
$loggerFactory = global.boot.contexts.root.loggerFactory;
}
if (LoggerFactory.detectBrowser() && window?.loggerFactory) {
$loggerFactory = window.loggerFactory;
}
if (LoggerFactory.detectBrowser() && window?.boot?.contexts?.root?.loggerFactory) {
$loggerFactory = window.boot.contexts.root.loggerFactory;
}
return $loggerFactory;
}
static getFormatter(configArg) {
let format = 'json';
const $config = this.detectConfig(configArg);
if (LoggerFactory.detectBrowser()) {
format = 'text';
}
if ($config.has('logging.format')) {
format = $config.get('logging.format');
}
const formatter = (format.toLowerCase() === 'text') ? new PlainTextFormatter() : new JSONFormatter();
return formatter;
}
static getLogger(category, configArg, configPath, cache) {
const loggerFactory = this.detectLoggerFactory();
if (loggerFactory) {
return loggerFactory.getLogger(category);
}
const $configArg = (typeof category === 'object' ? category : configArg);
const $category = (typeof category === 'object' ? '' : category);
return new ConfigurableLogger(LoggerFactory.detectConfig($configArg),
new ConsoleLogger($category,
null, null, null,
LoggerFactory.getFormatter($configArg),
null),
$category,
configPath,
cache || LoggerFactory.loggerCategoryCache);
}
constructor(config, cache, configPath) {
this.config = config;
this.cache = cache;
this.configPath = configPath || ConfigurableLogger.DEFAULT_CONFIG_PATH;
if (!this.config) {
throw new Error('config is required');
}
if (!this.cache) {
throw new Error('cache is required');
}
}
getLogger(categoryArg) {
const category = (typeof categoryArg === 'string') ? categoryArg
: (categoryArg && categoryArg.qualifier)
|| (categoryArg && categoryArg.name)
|| (categoryArg && categoryArg.constructor && categoryArg.constructor.name);
return new ConfigurableLogger(this.config,
new ConsoleLogger(category,
null, null, null,
this.getFormatter(),
null),
category,
this.configPath,
this.cache);
}
getFormatter() {
let format = 'json';
if (LoggerFactory.detectBrowser()) {
format = 'text';
}
if (this.config.has('logging.format')) {
format = this.config.get('logging.format');
}
const formatter = (format.toLowerCase() === 'text') ? new PlainTextFormatter() : new JSONFormatter();
return formatter;
}
}