-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggerFactory.js
More file actions
116 lines (108 loc) · 3.99 KB
/
LoggerFactory.js
File metadata and controls
116 lines (108 loc) · 3.99 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
/* eslint-disable import/extensions */
import { getGlobalRoot, detectBrowser } from '@alt-javascript/common';
import { config } from '@alt-javascript/config';
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';
/** Factory for creating loggers — auto-detects config from global boot context or explicit args. */
export default class LoggerFactory {
static loggerCategoryCache = new LoggerCategoryCache();
static detectConfig(configArg) {
let $config = null;
if (getGlobalRoot('config')) {
$config = getGlobalRoot('config');
}
if (detectBrowser() && window?.config) {
$config = window.config;
}
$config = configArg || $config;
if ($config) {
return $config;
}
// Fall back to the module-level default (ProfileConfigLoader-backed)
return config;
}
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 (detectBrowser() && window?.loggerFactory) {
$loggerFactory = window.loggerFactory;
}
if (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 (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 || config;
this.cache = cache || LoggerFactory.loggerCategoryCache ;
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 (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;
}
}