-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.js
183 lines (169 loc) · 5.82 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @module Logger
*
* The Logger class is a singleton that provides a unified interface for logging messages.
* It is used to abstract the specifics of how the logging is implemented.
*
* The following methods are implemented:
* - setLevel(level) - Sets the logging level of the logger.
* - getLevel() - Gets the current logging level of the logger.
* - shouldLog(level) - Checks if the specified logging level should currently output.
* - debug(...messages) - Logs a debug message if the current level allows for debug messages.
* - debugExt(...messages) - Logs a detailed debug message if the current level allows for debugExt messages.
* - info(...messages) - Logs an informational message if the current level allows for info messages.
* - warning(...messages) - Logs a warning message if the current level allows for warning messages.
* - error(...messages) - Logs an error message using console.error if the current level allows for error messages.
* - getInstance(env, defaultLevel) - Returns the singleton instance of the Logger.
*/
import defaultSettings from '../_config_/defaultSettings.js';
import * as optlyHelper from '../_helpers_/optimizelyHelper.js';
/**
* Class representing a singleton logger.
* Ensures a single logger instance across the application.
*/
class Logger {
/**
* Creates an instance of the Logger.
* @param {Object} env - The environment object containing the LOG_LEVEL variable.
* @param {string} [defaultLevel='info'] - The default logging level.
*/
constructor(env, defaultLevel = 'info') {
this.env = env;
if (Logger.instance) {
return Logger.instance;
}
if (env && env.LOG_LEVEL) {
this.level = env.LOG_LEVEL;
} else {
this.level = defaultSettings.logLevel || defaultLevel;
}
this.levels = {
debugExt: 4,
debug: 3,
info: 2,
warning: 1.5,
error: 1,
};
Logger.instance = this;
}
/**
* Sets the logging level of the logger.
* @param {string} level - The logging level to set ('debugExt', 'debug', 'info', 'warning', 'error').
* @throws {Error} Throws an error if an invalid logging level is provided.
*/
setLevel(level) {
if (this.levels[level] !== undefined) {
this.level = level;
} else {
throw new Error('Invalid logging level');
}
}
/**
* Gets the current logging level of the logger.
*
* @returns {string} The current logging level ('debugExt', 'debug', 'info', 'warning', or 'error').
*/
getLevel() {
return this.level;
}
/**
* Checks if the specified logging level should currently output.
* @param {string} level - The logging level to check.
* @returns {boolean} Returns true if the logging should occur, false otherwise.
*/
shouldLog(level) {
return this.levels[level] <= this.levels[this.level];
}
/**
* Formats the log messages.
* @param {...any} messages - The messages to format.
* @returns {string} The formatted log message.
*/
formatMessages(...messages) {
try {
const result = messages
.map((msg) => {
const isValidObject = optlyHelper.isValidObject(msg);
if (typeof msg === 'object' && isValidObject) {
return optlyHelper.safelyStringifyJSON(msg);
} else {
if (typeof msg === 'object' && !isValidObject) {
return '[Empty Object]';
} else {
return String(msg);
}
}
})
.join(' ');
return result;
} catch (error) {
console.error('Error formatting messages in logger module [formatMessages]:', error);
return 'Error while attempting to format messages [formatMessages]';
}
}
/**
* Logs a detailed debug message if the current level allows for debugExt messages.
* @param {...any} messages - The messages to log.
*/
debugExt(...messages) {
if (this.shouldLog('debugExt')) {
console.debug(`DEBUG EXT: ${this.formatMessages(...messages)}`);
}
}
/**
* Logs a debug message if the current level allows for debug messages.
* @param {...any} messages - The messages to log.
*/
debug(...messages) {
if (this.shouldLog('debug')) {
console.debug(`DEBUG: ${this.formatMessages(...messages)}`);
}
}
/**
* Logs an informational message if the current level allows for info messages.
* @param {...any} messages - The messages to log.
*/
info(...messages) {
if (this.shouldLog('info')) {
console.info(`INFO: ${this.formatMessages(...messages)}`);
}
}
/**
* Logs a warning message if the current level allows for warning messages.
* @param {...any} messages - The messages to log.
*/
warning(...messages) {
if (this.shouldLog('warning')) {
console.warn(`WARNING: ${this.formatMessages(...messages)}`);
}
}
/**
* Logs an error message using console.error if the current level allows for error messages.
* @param {...any} messages - The messages to log.
*/
error(...messages) {
if (this.shouldLog('error')) {
console.error(`ERROR: ${this.formatMessages(...messages)}`);
}
}
/**
* Returns the singleton instance of the Logger.
* @param {Object} env - The environment object containing the LOG_LEVEL variable.
* @param {string} [defaultLevel='info'] - The default logging level.
* @returns {Logger} The singleton instance of the Logger.
*/
static getInstance(env, defaultLevel = 'info') {
if (!Logger.instance) {
Logger.instance = new Logger(env, defaultLevel);
}
return Logger.instance;
}
}
export default Logger;
// Usage example
// const logger = Logger.getInstance(env, 'debugExt'); // Creates or retrieves the singleton logger instance
// logger.debugExt('This is a detailed debug message'); // Outputs a detailed debug message
// logger.debug('This is a debug message'); // Outputs a debug message
// logger.info('This is an info message'); // Outputs an informational message
// logger.warning('This is a warning message'); // Outputs a warning message
// logger.error('Error retrieving flag keys [retrieveFlagKeys]:', error); // Outputs an error message with additional parameters