@decaf-ts/logging 0.10.0 → 0.10.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +298 -171
- package/dist/logging.cjs +1 -1
- package/dist/logging.cjs.map +1 -1
- package/dist/logging.js +1 -1
- package/dist/logging.js.map +1 -1
- package/lib/LoggedClass.cjs +4 -4
- package/lib/LoggedClass.d.ts +4 -4
- package/lib/constants.cjs +32 -63
- package/lib/constants.d.ts +32 -63
- package/lib/constants.js.map +1 -1
- package/lib/decorators.cjs +28 -28
- package/lib/decorators.d.ts +29 -36
- package/lib/decorators.js.map +1 -1
- package/lib/environment.cjs +34 -41
- package/lib/environment.d.ts +31 -32
- package/lib/environment.js.map +1 -1
- package/lib/esm/LoggedClass.d.ts +4 -4
- package/lib/esm/LoggedClass.js +4 -4
- package/lib/esm/constants.d.ts +32 -63
- package/lib/esm/constants.js +32 -63
- package/lib/esm/constants.js.map +1 -1
- package/lib/esm/decorators.d.ts +29 -36
- package/lib/esm/decorators.js +28 -28
- package/lib/esm/decorators.js.map +1 -1
- package/lib/esm/environment.d.ts +31 -32
- package/lib/esm/environment.js +34 -41
- package/lib/esm/environment.js.map +1 -1
- package/lib/esm/filters/LogFilter.d.ts +11 -11
- package/lib/esm/filters/LogFilter.js +5 -5
- package/lib/esm/filters/PatternFilter.d.ts +15 -15
- package/lib/esm/filters/PatternFilter.js +12 -12
- package/lib/esm/index.d.ts +2 -2
- package/lib/esm/index.js +3 -3
- package/lib/esm/logging.d.ts +119 -113
- package/lib/esm/logging.js +223 -138
- package/lib/esm/logging.js.map +1 -1
- package/lib/esm/pino/index.d.ts +6 -0
- package/lib/esm/pino/index.js +6 -0
- package/lib/esm/pino/index.js.map +1 -1
- package/lib/esm/pino/pino.d.ts +13 -5
- package/lib/esm/pino/pino.js +70 -124
- package/lib/esm/pino/pino.js.map +1 -1
- package/lib/esm/text.d.ts +29 -67
- package/lib/esm/text.js +29 -67
- package/lib/esm/text.js.map +1 -1
- package/lib/esm/time.d.ts +45 -43
- package/lib/esm/time.js +38 -36
- package/lib/esm/time.js.map +1 -1
- package/lib/esm/types.d.ts +59 -50
- package/lib/esm/utils.d.ts +43 -0
- package/lib/esm/utils.js +55 -3
- package/lib/esm/utils.js.map +1 -1
- package/lib/esm/web.d.ts +2 -2
- package/lib/esm/web.js +2 -2
- package/lib/esm/winston/index.d.ts +7 -0
- package/lib/esm/winston/index.js +7 -1
- package/lib/esm/winston/index.js.map +1 -1
- package/lib/esm/winston/winston.d.ts +17 -21
- package/lib/esm/winston/winston.js +29 -36
- package/lib/esm/winston/winston.js.map +1 -1
- package/lib/filters/LogFilter.cjs +5 -5
- package/lib/filters/LogFilter.d.ts +11 -11
- package/lib/filters/PatternFilter.cjs +12 -12
- package/lib/filters/PatternFilter.d.ts +15 -15
- package/lib/index.cjs +3 -3
- package/lib/index.d.ts +2 -2
- package/lib/logging.cjs +224 -139
- package/lib/logging.d.ts +119 -113
- package/lib/logging.js.map +1 -1
- package/lib/pino/index.cjs +6 -0
- package/lib/pino/index.d.ts +6 -0
- package/lib/pino/index.js.map +1 -1
- package/lib/pino/pino.cjs +102 -126
- package/lib/pino/pino.d.ts +13 -5
- package/lib/pino/pino.js.map +1 -1
- package/lib/text.cjs +29 -67
- package/lib/text.d.ts +29 -67
- package/lib/text.js.map +1 -1
- package/lib/time.cjs +38 -36
- package/lib/time.d.ts +45 -43
- package/lib/time.js.map +1 -1
- package/lib/types.d.ts +59 -50
- package/lib/utils.cjs +55 -3
- package/lib/utils.d.ts +43 -0
- package/lib/utils.js.map +1 -1
- package/lib/web.cjs +2 -2
- package/lib/web.d.ts +2 -2
- package/lib/winston/index.cjs +22 -0
- package/lib/winston/index.d.ts +7 -0
- package/lib/winston/index.js.map +1 -1
- package/lib/winston/winston.cjs +29 -36
- package/lib/winston/winston.d.ts +17 -21
- package/lib/winston/winston.js.map +1 -1
- package/package.json +9 -10
- package/lib/accumulate.types.cjs +0 -27
- package/lib/accumulate.types.d.ts +0 -1
- package/lib/accumulate.types.js.map +0 -1
- package/lib/esm/accumulate.types.d.ts +0 -1
- package/lib/esm/accumulate.types.js +0 -25
- package/lib/esm/accumulate.types.js.map +0 -1
package/lib/logging.cjs
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Logging = exports.MiniLogger = void 0;
|
|
3
|
+
exports.Logging = exports.MiniLogger = exports.ROOT_CONTEXT_SYMBOL = void 0;
|
|
4
4
|
const styled_string_builder_1 = require("styled-string-builder");
|
|
5
5
|
const constants_1 = require("./constants.cjs");
|
|
6
6
|
const text_1 = require("./text.cjs");
|
|
7
7
|
const environment_1 = require("./environment.cjs");
|
|
8
8
|
const utils_1 = require("./utils.cjs");
|
|
9
|
+
exports.ROOT_CONTEXT_SYMBOL = Symbol("MiniLoggerRootContext");
|
|
9
10
|
/**
|
|
10
11
|
* @description A minimal logger implementation.
|
|
11
|
-
* @summary MiniLogger is a lightweight logging class that implements the Logger interface.
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* @param {string}
|
|
15
|
-
* @param {Partial<LoggingConfig>} conf - Optional configuration to override global settings
|
|
12
|
+
* @summary MiniLogger is a lightweight logging class that implements the Logger interface. It provides basic logging functionality with support for different log levels, verbosity, context-aware logging, and customizable formatting.
|
|
13
|
+
* @param {string} [context] - The context (typically class name) this logger is associated with.
|
|
14
|
+
* @param {Partial<LoggingConfig>} [conf] - Optional configuration to override global settings.
|
|
15
|
+
* @param {string[]} [baseContext=[]] - The base context for the logger.
|
|
16
16
|
* @class MiniLogger
|
|
17
17
|
* @example
|
|
18
18
|
* // Create a new logger for a class
|
|
@@ -31,9 +31,13 @@ const utils_1 = require("./utils.cjs");
|
|
|
31
31
|
* logger.for('specialMethod', { style: true }).info('Styled message');
|
|
32
32
|
*/
|
|
33
33
|
class MiniLogger {
|
|
34
|
-
constructor(context, conf) {
|
|
35
|
-
this.context = context;
|
|
34
|
+
constructor(context, conf, baseContext = []) {
|
|
36
35
|
this.conf = conf;
|
|
36
|
+
this.baseContext = Array.isArray(baseContext) ? [...baseContext] : [];
|
|
37
|
+
if (context)
|
|
38
|
+
this.baseContext.push(context);
|
|
39
|
+
this.context = [...this.baseContext];
|
|
40
|
+
this[exports.ROOT_CONTEXT_SYMBOL] = [...this.baseContext];
|
|
37
41
|
}
|
|
38
42
|
config(key) {
|
|
39
43
|
if (this.conf && key in this.conf)
|
|
@@ -41,17 +45,28 @@ class MiniLogger {
|
|
|
41
45
|
return Logging.getConfig()[key];
|
|
42
46
|
}
|
|
43
47
|
/**
|
|
44
|
-
* @description Creates a child logger for a specific method or context
|
|
45
|
-
* @summary Returns a new logger instance with the current context extended by the specified method name
|
|
46
|
-
* @param {string | Function} method - The method name or
|
|
47
|
-
* @param {Partial<LoggingConfig>} config - Optional configuration to override settings
|
|
48
|
-
* @param {...any[]} args - Additional arguments to pass to the logger factory
|
|
49
|
-
* @return {Logger} A new logger instance for the specified method
|
|
48
|
+
* @description Creates a child logger for a specific method or context.
|
|
49
|
+
* @summary Returns a new logger instance with the current context extended by the specified method name.
|
|
50
|
+
* @param {string | Function | object | Partial<LoggingConfig>} [method] - The method name, function, or configuration to create a logger for.
|
|
51
|
+
* @param {Partial<LoggingConfig>} [config] - Optional configuration to override settings.
|
|
52
|
+
* @param {...any[]} args - Additional arguments to pass to the logger factory.
|
|
53
|
+
* @return {Logger} A new logger instance for the specified method.
|
|
50
54
|
*/
|
|
51
55
|
for(method, config, ...args // eslint-disable-line @typescript-eslint/no-unused-vars
|
|
52
56
|
) {
|
|
53
57
|
let contextName;
|
|
54
58
|
let childConfig = config;
|
|
59
|
+
const parentContext = Array.isArray(this.context)
|
|
60
|
+
? [...this.context]
|
|
61
|
+
: typeof this.context === "string" && this.context
|
|
62
|
+
? [this.context]
|
|
63
|
+
: [];
|
|
64
|
+
const rootCandidate = this[exports.ROOT_CONTEXT_SYMBOL];
|
|
65
|
+
const baseContext = Array.isArray(rootCandidate)
|
|
66
|
+
? [...rootCandidate]
|
|
67
|
+
: Array.isArray(this.baseContext)
|
|
68
|
+
? [...this.baseContext]
|
|
69
|
+
: [];
|
|
55
70
|
if (typeof method === "string") {
|
|
56
71
|
contextName = method;
|
|
57
72
|
}
|
|
@@ -63,6 +78,9 @@ class MiniLogger {
|
|
|
63
78
|
childConfig = method;
|
|
64
79
|
}
|
|
65
80
|
}
|
|
81
|
+
let contextSegments = contextName
|
|
82
|
+
? [...parentContext, contextName]
|
|
83
|
+
: [...parentContext];
|
|
66
84
|
return new Proxy(this, {
|
|
67
85
|
get: (target, p, receiver) => {
|
|
68
86
|
const result = Reflect.get(target, p, receiver);
|
|
@@ -75,37 +93,65 @@ class MiniLogger {
|
|
|
75
93
|
}
|
|
76
94
|
return Reflect.apply(target, receiver, argArray);
|
|
77
95
|
},
|
|
78
|
-
get: (target,
|
|
79
|
-
if (childConfig &&
|
|
80
|
-
return childConfig[
|
|
81
|
-
return Reflect.get(target,
|
|
96
|
+
get: (target, key) => {
|
|
97
|
+
if (childConfig && key in childConfig)
|
|
98
|
+
return childConfig[key];
|
|
99
|
+
return Reflect.get(target, key, receiver);
|
|
82
100
|
},
|
|
83
101
|
});
|
|
84
102
|
}
|
|
85
|
-
if (p === "
|
|
86
|
-
return
|
|
103
|
+
if (p === "clear") {
|
|
104
|
+
return () => {
|
|
105
|
+
contextSegments = [...baseContext];
|
|
106
|
+
childConfig = undefined;
|
|
107
|
+
return receiver;
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
if (p === "context") {
|
|
111
|
+
return contextSegments;
|
|
112
|
+
}
|
|
113
|
+
if (p === "root") {
|
|
114
|
+
return [...baseContext];
|
|
115
|
+
}
|
|
116
|
+
if (p === exports.ROOT_CONTEXT_SYMBOL) {
|
|
117
|
+
return baseContext;
|
|
118
|
+
}
|
|
119
|
+
if (p === "for") {
|
|
120
|
+
return (...innerArgs) => {
|
|
121
|
+
const originalContext = Array.isArray(target.context)
|
|
122
|
+
? [...target.context]
|
|
123
|
+
: typeof target.context === "string" && target.context
|
|
124
|
+
? [target.context]
|
|
125
|
+
: [];
|
|
126
|
+
target.context = [...contextSegments];
|
|
127
|
+
try {
|
|
128
|
+
// eslint-disable-next-line prefer-spread
|
|
129
|
+
return target.for.apply(target, innerArgs);
|
|
130
|
+
}
|
|
131
|
+
finally {
|
|
132
|
+
target.context = originalContext;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
87
135
|
}
|
|
88
136
|
return result;
|
|
89
137
|
},
|
|
90
138
|
});
|
|
91
139
|
}
|
|
92
140
|
/**
|
|
93
|
-
* @description Creates a formatted log string
|
|
94
|
-
* @summary Generates a log string with timestamp, colored log level, context, and message
|
|
95
|
-
* @param {LogLevel} level - The log level for this message
|
|
96
|
-
* @param {StringLike | Error} message - The message to log or an Error object
|
|
97
|
-
* @param {
|
|
98
|
-
* @return {string} A formatted log string with all components
|
|
141
|
+
* @description Creates a formatted log string.
|
|
142
|
+
* @summary Generates a log string with timestamp, colored log level, context, and message.
|
|
143
|
+
* @param {LogLevel} level - The log level for this message.
|
|
144
|
+
* @param {StringLike | Error} message - The message to log or an Error object.
|
|
145
|
+
* @param {Error} [error] - Optional error to extract stack trace to include in the log.
|
|
146
|
+
* @return {string} A formatted log string with all components.
|
|
99
147
|
*/
|
|
100
148
|
createLog(level, message, error) {
|
|
101
149
|
const log = {};
|
|
102
150
|
const style = this.config("style");
|
|
103
151
|
const separator = this.config("separator");
|
|
104
|
-
const app =
|
|
152
|
+
const app = Logging.getConfig().app;
|
|
105
153
|
if (app)
|
|
106
|
-
log.app = style
|
|
107
|
-
? Logging.theme(app, "app", level)
|
|
108
|
-
: app;
|
|
154
|
+
log.app = style ? Logging.theme(app, "app", level) : app;
|
|
109
155
|
if (separator)
|
|
110
156
|
log.separator = style
|
|
111
157
|
? Logging.theme(separator, "separator", level)
|
|
@@ -122,10 +168,16 @@ class MiniLogger {
|
|
|
122
168
|
log.level = lvl.toUpperCase();
|
|
123
169
|
}
|
|
124
170
|
if (this.config("context")) {
|
|
125
|
-
const
|
|
126
|
-
?
|
|
127
|
-
: this.context
|
|
128
|
-
|
|
171
|
+
const contextSegments = Array.isArray(this.context)
|
|
172
|
+
? this.context
|
|
173
|
+
: typeof this.context === "string" && this.context
|
|
174
|
+
? [this.context]
|
|
175
|
+
: [];
|
|
176
|
+
if (contextSegments.length) {
|
|
177
|
+
const joined = contextSegments.join(this.config("contextSeparator") || ".");
|
|
178
|
+
const context = style ? Logging.theme(joined, "class", level) : joined;
|
|
179
|
+
log.context = context;
|
|
180
|
+
}
|
|
129
181
|
}
|
|
130
182
|
if (this.config("correlationId")) {
|
|
131
183
|
{
|
|
@@ -168,12 +220,11 @@ class MiniLogger {
|
|
|
168
220
|
}
|
|
169
221
|
}
|
|
170
222
|
/**
|
|
171
|
-
* @description Logs a message with the specified log level
|
|
172
|
-
* @summary Checks if the message should be logged based on the current log level,
|
|
173
|
-
*
|
|
174
|
-
* @param {
|
|
175
|
-
* @param {
|
|
176
|
-
* @param {string} [error] - Optional stack trace to include in the log
|
|
223
|
+
* @description Logs a message with the specified log level.
|
|
224
|
+
* @summary Checks if the message should be logged based on the current log level, then uses the appropriate console method to output the formatted log.
|
|
225
|
+
* @param {LogLevel} level - The log level of the message.
|
|
226
|
+
* @param {StringLike | Error} msg - The message to be logged or an Error object.
|
|
227
|
+
* @param {Error} [error] - Optional stack trace to include in the log.
|
|
177
228
|
* @return {void}
|
|
178
229
|
*/
|
|
179
230
|
log(level, msg, error) {
|
|
@@ -210,19 +261,19 @@ class MiniLogger {
|
|
|
210
261
|
method(this.createLog(level, msg, error));
|
|
211
262
|
}
|
|
212
263
|
/**
|
|
213
|
-
* @description Logs a message at the benchmark level
|
|
214
|
-
* @summary Logs a message at the benchmark level if the current verbosity setting allows it
|
|
215
|
-
* @param {StringLike} msg - The message to be logged
|
|
264
|
+
* @description Logs a message at the benchmark level.
|
|
265
|
+
* @summary Logs a message at the benchmark level if the current verbosity setting allows it.
|
|
266
|
+
* @param {StringLike} msg - The message to be logged.
|
|
216
267
|
* @return {void}
|
|
217
268
|
*/
|
|
218
269
|
benchmark(msg) {
|
|
219
270
|
this.log(constants_1.LogLevel.benchmark, msg);
|
|
220
271
|
}
|
|
221
272
|
/**
|
|
222
|
-
* @description Logs a message at the silly level
|
|
223
|
-
* @summary Logs a message at the silly level if the current verbosity setting allows it
|
|
224
|
-
* @param {StringLike} msg - The message to be logged
|
|
225
|
-
* @param {number} [verbosity=0] - The verbosity level of the message
|
|
273
|
+
* @description Logs a message at the silly level.
|
|
274
|
+
* @summary Logs a message at the silly level if the current verbosity setting allows it.
|
|
275
|
+
* @param {StringLike} msg - The message to be logged.
|
|
276
|
+
* @param {number} [verbosity=0] - The verbosity level of the message.
|
|
226
277
|
* @return {void}
|
|
227
278
|
*/
|
|
228
279
|
silly(msg, verbosity = 0) {
|
|
@@ -230,10 +281,10 @@ class MiniLogger {
|
|
|
230
281
|
this.log(constants_1.LogLevel.silly, msg);
|
|
231
282
|
}
|
|
232
283
|
/**
|
|
233
|
-
* @description Logs a message at the verbose level
|
|
234
|
-
* @summary Logs a message at the verbose level if the current verbosity setting allows it
|
|
235
|
-
* @param {StringLike} msg - The message to be logged
|
|
236
|
-
* @param {number} [verbosity=0] - The verbosity level of the message
|
|
284
|
+
* @description Logs a message at the verbose level.
|
|
285
|
+
* @summary Logs a message at the verbose level if the current verbosity setting allows it.
|
|
286
|
+
* @param {StringLike} msg - The message to be logged.
|
|
287
|
+
* @param {number} [verbosity=0] - The verbosity level of the message.
|
|
237
288
|
* @return {void}
|
|
238
289
|
*/
|
|
239
290
|
verbose(msg, verbosity = 0) {
|
|
@@ -241,67 +292,77 @@ class MiniLogger {
|
|
|
241
292
|
this.log(constants_1.LogLevel.verbose, msg);
|
|
242
293
|
}
|
|
243
294
|
/**
|
|
244
|
-
* @description Logs a message at the info level
|
|
245
|
-
* @summary Logs a message at the info level for general application information
|
|
246
|
-
* @param {StringLike} msg - The message to be logged
|
|
295
|
+
* @description Logs a message at the info level.
|
|
296
|
+
* @summary Logs a message at the info level for general application information.
|
|
297
|
+
* @param {StringLike} msg - The message to be logged.
|
|
247
298
|
* @return {void}
|
|
248
299
|
*/
|
|
249
300
|
info(msg) {
|
|
250
301
|
this.log(constants_1.LogLevel.info, msg);
|
|
251
302
|
}
|
|
252
303
|
/**
|
|
253
|
-
* @description Logs a message at the debug level
|
|
254
|
-
* @summary Logs a message at the debug level for detailed troubleshooting information
|
|
255
|
-
* @param {StringLike} msg - The message to be logged
|
|
304
|
+
* @description Logs a message at the debug level.
|
|
305
|
+
* @summary Logs a message at the debug level for detailed troubleshooting information.
|
|
306
|
+
* @param {StringLike} msg - The message to be logged.
|
|
256
307
|
* @return {void}
|
|
257
308
|
*/
|
|
258
309
|
debug(msg) {
|
|
259
310
|
this.log(constants_1.LogLevel.debug, msg);
|
|
260
311
|
}
|
|
261
312
|
/**
|
|
262
|
-
* @description Logs a message at the error level
|
|
263
|
-
* @summary Logs a message at the error level for errors and exceptions
|
|
264
|
-
* @param {StringLike | Error} msg - The message to be logged or an Error object
|
|
265
|
-
* @param e
|
|
313
|
+
* @description Logs a message at the error level.
|
|
314
|
+
* @summary Logs a message at the error level for errors and exceptions.
|
|
315
|
+
* @param {StringLike | Error} msg - The message to be logged or an Error object.
|
|
316
|
+
* @param {Error} [e] - Optional error to include in the log.
|
|
266
317
|
* @return {void}
|
|
267
318
|
*/
|
|
268
319
|
error(msg, e) {
|
|
269
320
|
this.log(constants_1.LogLevel.error, msg, e);
|
|
270
321
|
}
|
|
271
322
|
/**
|
|
272
|
-
* @description Logs a message at the
|
|
273
|
-
* @summary Logs a message at the
|
|
274
|
-
* @param {StringLike} msg - The message to be logged
|
|
323
|
+
* @description Logs a message at the warning level.
|
|
324
|
+
* @summary Logs a message at the warning level for potential issues.
|
|
325
|
+
* @param {StringLike} msg - The message to be logged.
|
|
275
326
|
* @return {void}
|
|
276
327
|
*/
|
|
277
328
|
warn(msg) {
|
|
278
329
|
this.log(constants_1.LogLevel.warn, msg);
|
|
279
330
|
}
|
|
280
331
|
/**
|
|
281
|
-
* @description Logs a message at the
|
|
282
|
-
* @summary Logs a message at the
|
|
283
|
-
* @param {StringLike} msg - The message to be logged
|
|
332
|
+
* @description Logs a message at the trace level.
|
|
333
|
+
* @summary Logs a message at the trace level for tracing code execution.
|
|
334
|
+
* @param {StringLike} msg - The message to be logged.
|
|
284
335
|
* @return {void}
|
|
285
336
|
*/
|
|
286
337
|
trace(msg) {
|
|
287
338
|
this.log(constants_1.LogLevel.trace, msg);
|
|
288
339
|
}
|
|
289
340
|
/**
|
|
290
|
-
* @description Updates the logger configuration
|
|
291
|
-
* @summary Merges the provided configuration with the existing configuration
|
|
292
|
-
* @param {Partial<LoggingConfig>} config - The configuration options to apply
|
|
341
|
+
* @description Updates the logger configuration.
|
|
342
|
+
* @summary Merges the provided configuration with the existing configuration.
|
|
343
|
+
* @param {Partial<LoggingConfig>} config - The configuration options to apply.
|
|
293
344
|
* @return {void}
|
|
294
345
|
*/
|
|
295
346
|
setConfig(config) {
|
|
296
347
|
this.conf = { ...(this.conf || {}), ...config };
|
|
297
348
|
}
|
|
349
|
+
get root() {
|
|
350
|
+
return [...this.baseContext];
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* @description Clears any contextual overrides applied by `for`.
|
|
354
|
+
* @summary Returns the same logger instance so more contexts can be chained afterwards.
|
|
355
|
+
* @return {this} The same logger instance.
|
|
356
|
+
*/
|
|
357
|
+
clear() {
|
|
358
|
+
this.context = [...this.baseContext];
|
|
359
|
+
return this;
|
|
360
|
+
}
|
|
298
361
|
}
|
|
299
362
|
exports.MiniLogger = MiniLogger;
|
|
300
363
|
/**
|
|
301
|
-
* @description A static class for managing logging operations
|
|
302
|
-
* @summary The Logging class provides a centralized logging mechanism with support for
|
|
303
|
-
* different log levels, verbosity, and styling. It uses a singleton pattern to maintain a global
|
|
304
|
-
* logger instance and allows creating specific loggers for different classes and methods.
|
|
364
|
+
* @description A static class for managing logging operations.
|
|
365
|
+
* @summary The Logging class provides a centralized logging mechanism with support for different log levels, verbosity, and styling. It uses a singleton pattern to maintain a global logger instance and allows creating specific loggers for different classes and methods.
|
|
305
366
|
* @class Logging
|
|
306
367
|
* @example
|
|
307
368
|
* // Set global configuration
|
|
@@ -364,27 +425,31 @@ exports.MiniLogger = MiniLogger;
|
|
|
364
425
|
*/
|
|
365
426
|
class Logging {
|
|
366
427
|
/**
|
|
367
|
-
* @description Factory function for creating logger instances
|
|
428
|
+
* @description Factory function for creating logger instances.
|
|
368
429
|
* @summary A function that creates new Logger instances. By default, it creates a MiniLogger.
|
|
369
430
|
*/
|
|
370
431
|
static { this._factory = (object, config) => {
|
|
371
|
-
|
|
432
|
+
const base = typeof environment_1.LoggedEnvironment.app === "string"
|
|
433
|
+
? [environment_1.LoggedEnvironment.app]
|
|
434
|
+
: [];
|
|
435
|
+
return new MiniLogger(object, config, base);
|
|
372
436
|
}; }
|
|
373
437
|
static { this._config = environment_1.LoggedEnvironment; }
|
|
374
438
|
constructor() { }
|
|
375
439
|
/**
|
|
376
|
-
* @description Sets the factory function for creating logger instances
|
|
377
|
-
* @summary Allows customizing how logger instances are created
|
|
378
|
-
* @param {LoggerFactory} factory - The factory function to use for creating loggers
|
|
440
|
+
* @description Sets the factory function for creating logger instances.
|
|
441
|
+
* @summary Allows customizing how logger instances are created.
|
|
442
|
+
* @param {LoggerFactory} factory - The factory function to use for creating loggers.
|
|
379
443
|
* @return {void}
|
|
380
444
|
*/
|
|
381
445
|
static setFactory(factory) {
|
|
382
446
|
Logging._factory = factory;
|
|
447
|
+
this.global = undefined;
|
|
383
448
|
}
|
|
384
449
|
/**
|
|
385
|
-
* @description Updates the global logging configuration
|
|
386
|
-
* @summary Allows updating the global logging configuration with new settings
|
|
387
|
-
* @param {Partial<LoggingConfig>} config - The configuration options to apply
|
|
450
|
+
* @description Updates the global logging configuration.
|
|
451
|
+
* @summary Allows updating the global logging configuration with new settings.
|
|
452
|
+
* @param {Partial<LoggingConfig>} config - The configuration options to apply.
|
|
388
453
|
* @return {void}
|
|
389
454
|
*/
|
|
390
455
|
static setConfig(config) {
|
|
@@ -393,9 +458,9 @@ class Logging {
|
|
|
393
458
|
});
|
|
394
459
|
}
|
|
395
460
|
/**
|
|
396
|
-
* @description Gets a copy of the current global logging configuration
|
|
397
|
-
* @summary Returns a copy of the current global logging configuration
|
|
398
|
-
* @return {LoggingConfig} A copy of the current configuration
|
|
461
|
+
* @description Gets a copy of the current global logging configuration.
|
|
462
|
+
* @summary Returns a copy of the current global logging configuration.
|
|
463
|
+
* @return {LoggingConfig} A copy of the current configuration.
|
|
399
464
|
*/
|
|
400
465
|
static getConfig() {
|
|
401
466
|
return this._config;
|
|
@@ -403,19 +468,17 @@ class Logging {
|
|
|
403
468
|
/**
|
|
404
469
|
* @description Retrieves or creates the global logger instance.
|
|
405
470
|
* @summary Returns the existing global logger or creates a new one if it doesn't exist.
|
|
406
|
-
*
|
|
407
|
-
* @return The global VerbosityLogger instance.
|
|
471
|
+
* @return {Logger} The global Logger instance.
|
|
408
472
|
*/
|
|
409
473
|
static get() {
|
|
410
|
-
|
|
411
|
-
return this.global;
|
|
474
|
+
return this.ensureRoot();
|
|
412
475
|
}
|
|
413
476
|
/**
|
|
414
477
|
* @description Logs a verbose message.
|
|
415
478
|
* @summary Delegates the verbose logging to the global logger instance.
|
|
416
|
-
*
|
|
417
|
-
* @param
|
|
418
|
-
* @
|
|
479
|
+
* @param {StringLike} msg - The message to be logged.
|
|
480
|
+
* @param {number} [verbosity=0] - The verbosity level of the message.
|
|
481
|
+
* @return {void}
|
|
419
482
|
*/
|
|
420
483
|
static verbose(msg, verbosity = 0) {
|
|
421
484
|
return this.get().verbose(msg, verbosity);
|
|
@@ -423,17 +486,17 @@ class Logging {
|
|
|
423
486
|
/**
|
|
424
487
|
* @description Logs an info message.
|
|
425
488
|
* @summary Delegates the info logging to the global logger instance.
|
|
426
|
-
*
|
|
427
|
-
* @
|
|
489
|
+
* @param {StringLike} msg - The message to be logged.
|
|
490
|
+
* @return {void}
|
|
428
491
|
*/
|
|
429
492
|
static info(msg) {
|
|
430
493
|
return this.get().info(msg);
|
|
431
494
|
}
|
|
432
495
|
/**
|
|
433
|
-
* @description Logs
|
|
434
|
-
* @summary Delegates the
|
|
435
|
-
*
|
|
436
|
-
* @
|
|
496
|
+
* @description Logs a trace message.
|
|
497
|
+
* @summary Delegates the trace logging to the global logger instance.
|
|
498
|
+
* @param {StringLike} msg - The message to be logged.
|
|
499
|
+
* @return {void}
|
|
437
500
|
*/
|
|
438
501
|
static trace(msg) {
|
|
439
502
|
return this.get().trace(msg);
|
|
@@ -441,8 +504,8 @@ class Logging {
|
|
|
441
504
|
/**
|
|
442
505
|
* @description Logs a debug message.
|
|
443
506
|
* @summary Delegates the debug logging to the global logger instance.
|
|
444
|
-
*
|
|
445
|
-
* @
|
|
507
|
+
* @param {StringLike} msg - The message to be logged.
|
|
508
|
+
* @return {void}
|
|
446
509
|
*/
|
|
447
510
|
static debug(msg) {
|
|
448
511
|
return this.get().debug(msg);
|
|
@@ -450,26 +513,26 @@ class Logging {
|
|
|
450
513
|
/**
|
|
451
514
|
* @description Logs a benchmark message.
|
|
452
515
|
* @summary Delegates the benchmark logging to the global logger instance.
|
|
453
|
-
*
|
|
454
|
-
* @
|
|
516
|
+
* @param {StringLike} msg - The message to be logged.
|
|
517
|
+
* @return {void}
|
|
455
518
|
*/
|
|
456
519
|
static benchmark(msg) {
|
|
457
520
|
return this.get().benchmark(msg);
|
|
458
521
|
}
|
|
459
522
|
/**
|
|
460
523
|
* @description Logs a silly message.
|
|
461
|
-
* @summary Delegates the
|
|
462
|
-
*
|
|
463
|
-
* @
|
|
524
|
+
* @summary Delegates the silly logging to the global logger instance.
|
|
525
|
+
* @param {StringLike} msg - The message to be logged.
|
|
526
|
+
* @return {void}
|
|
464
527
|
*/
|
|
465
528
|
static silly(msg) {
|
|
466
529
|
return this.get().silly(msg);
|
|
467
530
|
}
|
|
468
531
|
/**
|
|
469
|
-
* @description Logs a
|
|
470
|
-
* @summary Delegates the
|
|
471
|
-
*
|
|
472
|
-
* @
|
|
532
|
+
* @description Logs a warning message.
|
|
533
|
+
* @summary Delegates the warning logging to the global logger instance.
|
|
534
|
+
* @param {StringLike} msg - The message to be logged.
|
|
535
|
+
* @return {void}
|
|
473
536
|
*/
|
|
474
537
|
static warn(msg) {
|
|
475
538
|
return this.get().warn(msg);
|
|
@@ -477,49 +540,71 @@ class Logging {
|
|
|
477
540
|
/**
|
|
478
541
|
* @description Logs an error message.
|
|
479
542
|
* @summary Delegates the error logging to the global logger instance.
|
|
480
|
-
*
|
|
481
|
-
* @param
|
|
482
|
-
* @
|
|
543
|
+
* @param {StringLike | Error} msg - The message to be logged.
|
|
544
|
+
* @param {Error} [e] - Optional error to include in the log.
|
|
545
|
+
* @return {void}
|
|
483
546
|
*/
|
|
484
547
|
static error(msg, e) {
|
|
485
548
|
return this.get().error(msg, e);
|
|
486
549
|
}
|
|
487
550
|
/**
|
|
488
|
-
* @description Creates a logger for a specific object or context
|
|
489
|
-
* @summary Creates a new logger instance for the given object or context using the factory function
|
|
490
|
-
* @param {LoggingContext} object - The object, class, or context to create a logger for
|
|
491
|
-
* @param {Partial<LoggingConfig>} [config] - Optional configuration to override global settings
|
|
492
|
-
* @param {...any} args - Additional arguments to pass to the logger factory
|
|
493
|
-
* @return {Logger} A new logger instance for the specified object or context
|
|
551
|
+
* @description Creates a logger for a specific object or context.
|
|
552
|
+
* @summary Creates a new logger instance for the given object or context using the factory function.
|
|
553
|
+
* @param {LoggingContext} object - The object, class, or context to create a logger for.
|
|
554
|
+
* @param {Partial<LoggingConfig>} [config] - Optional configuration to override global settings.
|
|
555
|
+
* @param {...any} args - Additional arguments to pass to the logger factory.
|
|
556
|
+
* @return {Logger} A new logger instance for the specified object or context.
|
|
494
557
|
*/
|
|
495
558
|
static for(object, config, ...args) {
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
: object.constructor
|
|
500
|
-
? object.constructor.name
|
|
501
|
-
: object.name;
|
|
502
|
-
return this._factory(object, config, ...args);
|
|
559
|
+
const root = this.global ? this.global : this.ensureRoot(args);
|
|
560
|
+
const callArgs = config !== undefined ? [object, config] : [object];
|
|
561
|
+
return root.for(...callArgs);
|
|
503
562
|
}
|
|
504
563
|
/**
|
|
505
|
-
* @description Creates a logger for a specific reason or correlation context
|
|
506
|
-
* @summary Utility to quickly create a logger labeled with a free-form reason and optional identifier
|
|
507
|
-
*
|
|
508
|
-
* @param {string}
|
|
509
|
-
* @
|
|
510
|
-
* @return {Logger} A new logger instance labeled with the provided reason and id
|
|
564
|
+
* @description Creates a logger for a specific reason or correlation context.
|
|
565
|
+
* @summary Utility to quickly create a logger labeled with a free-form reason and optional identifier so that ad-hoc operations can be traced without tying the logger to a class or method name.
|
|
566
|
+
* @param {string} reason - A textual reason or context label for this logger instance.
|
|
567
|
+
* @param {string} [id] - Optional identifier to help correlate related log entries.
|
|
568
|
+
* @return {Logger} A new logger instance labeled with the provided reason and id.
|
|
511
569
|
*/
|
|
512
570
|
static because(reason, id) {
|
|
513
|
-
|
|
571
|
+
const root = this.ensureRoot();
|
|
572
|
+
let logger = root.for(reason, this._config);
|
|
573
|
+
if (id)
|
|
574
|
+
logger = logger.for(id);
|
|
575
|
+
return logger;
|
|
576
|
+
}
|
|
577
|
+
static baseContext() {
|
|
578
|
+
const app = this._config.app;
|
|
579
|
+
return typeof app === "string" && app.length ? [app] : [];
|
|
580
|
+
}
|
|
581
|
+
static attachRootContext(logger) {
|
|
582
|
+
const base = logger.root && Array.isArray(logger.root)
|
|
583
|
+
? [...logger.root]
|
|
584
|
+
: this.baseContext();
|
|
585
|
+
if (!logger.context ||
|
|
586
|
+
(Array.isArray(logger.context) &&
|
|
587
|
+
logger.context.length === 0)) {
|
|
588
|
+
logger.context = [...base];
|
|
589
|
+
}
|
|
590
|
+
logger[exports.ROOT_CONTEXT_SYMBOL] = [...base];
|
|
591
|
+
return logger;
|
|
592
|
+
}
|
|
593
|
+
static ensureRoot(extras = []) {
|
|
594
|
+
if (!this.global) {
|
|
595
|
+
const instance = this._factory(undefined, undefined, ...extras);
|
|
596
|
+
this.global = this.attachRootContext(instance);
|
|
597
|
+
}
|
|
598
|
+
return this.global;
|
|
514
599
|
}
|
|
515
600
|
/**
|
|
516
|
-
* @description Applies theme styling to text
|
|
517
|
-
* @summary Applies styling (colors, formatting) to text based on the theme configuration
|
|
518
|
-
* @param {string} text - The text to style
|
|
519
|
-
* @param
|
|
520
|
-
* @param {LogLevel} loggerLevel - The log level to use for styling
|
|
521
|
-
* @param {Theme} [template=DefaultTheme] - The theme to use for styling
|
|
522
|
-
* @return {string} The styled text
|
|
601
|
+
* @description Applies theme styling to text.
|
|
602
|
+
* @summary Applies styling (colors, formatting) to text based on the theme configuration.
|
|
603
|
+
* @param {string} text - The text to style.
|
|
604
|
+
* @param type - The type of element to style (e.g., "class", "message", "logLevel").
|
|
605
|
+
* @param {LogLevel} loggerLevel - The log level to use for styling.
|
|
606
|
+
* @param {Theme} [template=DefaultTheme] - The theme to use for styling.
|
|
607
|
+
* @return {string} The styled text.
|
|
523
608
|
* @mermaid
|
|
524
609
|
* sequenceDiagram
|
|
525
610
|
* participant Caller
|