@matter/general 0.13.0-alpha.0-20250322-f085fa576 → 0.13.0-alpha.0-20250324-d7b7d9731

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.
Files changed (66) hide show
  1. package/dist/cjs/log/Console.d.ts +21 -0
  2. package/dist/cjs/log/Console.d.ts.map +1 -0
  3. package/dist/cjs/log/Console.js +58 -0
  4. package/dist/cjs/log/Console.js.map +6 -0
  5. package/dist/cjs/log/Diagnostic.d.ts +6 -0
  6. package/dist/cjs/log/Diagnostic.d.ts.map +1 -1
  7. package/dist/cjs/log/Diagnostic.js +16 -0
  8. package/dist/cjs/log/Diagnostic.js.map +1 -1
  9. package/dist/cjs/log/LogDestination.d.ts +67 -0
  10. package/dist/cjs/log/LogDestination.d.ts.map +1 -0
  11. package/dist/cjs/log/LogDestination.js +65 -0
  12. package/dist/cjs/log/LogDestination.js.map +6 -0
  13. package/dist/cjs/log/LogFormat.d.ts +18 -12
  14. package/dist/cjs/log/LogFormat.d.ts.map +1 -1
  15. package/dist/cjs/log/LogFormat.js +17 -24
  16. package/dist/cjs/log/LogFormat.js.map +1 -1
  17. package/dist/cjs/log/LogLevel.d.ts +19 -9
  18. package/dist/cjs/log/LogLevel.d.ts.map +1 -1
  19. package/dist/cjs/log/LogLevel.js +40 -25
  20. package/dist/cjs/log/LogLevel.js.map +1 -1
  21. package/dist/cjs/log/Logger.d.ts +157 -106
  22. package/dist/cjs/log/Logger.d.ts.map +1 -1
  23. package/dist/cjs/log/Logger.js +298 -256
  24. package/dist/cjs/log/Logger.js.map +2 -2
  25. package/dist/cjs/log/index.d.ts +1 -0
  26. package/dist/cjs/log/index.d.ts.map +1 -1
  27. package/dist/cjs/log/index.js +1 -0
  28. package/dist/cjs/log/index.js.map +1 -1
  29. package/dist/cjs/transaction/Transaction.d.ts +19 -19
  30. package/dist/esm/log/Console.d.ts +21 -0
  31. package/dist/esm/log/Console.d.ts.map +1 -0
  32. package/dist/esm/log/Console.js +38 -0
  33. package/dist/esm/log/Console.js.map +6 -0
  34. package/dist/esm/log/Diagnostic.d.ts +6 -0
  35. package/dist/esm/log/Diagnostic.d.ts.map +1 -1
  36. package/dist/esm/log/Diagnostic.js +16 -0
  37. package/dist/esm/log/Diagnostic.js.map +1 -1
  38. package/dist/esm/log/LogDestination.d.ts +67 -0
  39. package/dist/esm/log/LogDestination.d.ts.map +1 -0
  40. package/dist/esm/log/LogDestination.js +45 -0
  41. package/dist/esm/log/LogDestination.js.map +6 -0
  42. package/dist/esm/log/LogFormat.d.ts +18 -12
  43. package/dist/esm/log/LogFormat.d.ts.map +1 -1
  44. package/dist/esm/log/LogFormat.js +17 -24
  45. package/dist/esm/log/LogFormat.js.map +1 -1
  46. package/dist/esm/log/LogLevel.d.ts +19 -9
  47. package/dist/esm/log/LogLevel.d.ts.map +1 -1
  48. package/dist/esm/log/LogLevel.js +40 -25
  49. package/dist/esm/log/LogLevel.js.map +1 -1
  50. package/dist/esm/log/Logger.d.ts +157 -106
  51. package/dist/esm/log/Logger.d.ts.map +1 -1
  52. package/dist/esm/log/Logger.js +298 -256
  53. package/dist/esm/log/Logger.js.map +2 -2
  54. package/dist/esm/log/index.d.ts +1 -0
  55. package/dist/esm/log/index.d.ts.map +1 -1
  56. package/dist/esm/log/index.js +1 -0
  57. package/dist/esm/log/index.js.map +1 -1
  58. package/dist/esm/transaction/Transaction.d.ts +19 -19
  59. package/package.json +2 -2
  60. package/src/log/Console.ts +52 -0
  61. package/src/log/Diagnostic.ts +21 -0
  62. package/src/log/LogDestination.ts +113 -0
  63. package/src/log/LogFormat.ts +39 -36
  64. package/src/log/LogLevel.ts +55 -25
  65. package/src/log/Logger.ts +379 -314
  66. package/src/log/index.ts +1 -0
@@ -9,123 +9,278 @@ import { ImplementationError, NotImplementedError } from "../MatterError.js";
9
9
  import { Time } from "../time/Time.js";
10
10
  import { Bytes } from "../util/Bytes.js";
11
11
  import { Diagnostic } from "./Diagnostic.js";
12
+ import { LogDestination, LogDestinations } from "./LogDestination.js";
12
13
  import { LogFormat } from "./LogFormat.js";
13
14
  import { LogLevel } from "./LogLevel.js";
14
- function consoleLogger(level, formattedLog, _facility) {
15
- const console2 = consoleLogger.console;
16
- switch (level) {
17
- case LogLevel.DEBUG:
18
- console2.debug(formattedLog);
19
- break;
20
- case LogLevel.INFO:
21
- console2.info(formattedLog);
22
- break;
23
- case LogLevel.NOTICE:
24
- console2.info(formattedLog);
25
- break;
26
- case LogLevel.WARN:
27
- console2.warn(formattedLog);
28
- break;
29
- case LogLevel.ERROR:
30
- console2.error(formattedLog);
31
- break;
32
- case LogLevel.FATAL:
33
- console2.error(formattedLog);
34
- break;
35
- }
36
- }
37
- const globalConsole = console;
38
- ((consoleLogger2) => {
39
- consoleLogger2.console = globalConsole;
40
- })(consoleLogger || (consoleLogger = {}));
41
- function logFormatterFor(formatName) {
42
- const format = LogFormat(formatName);
43
- return (now, level, facility, prefix, ...values) => format(Diagnostic.message({ now, level, facility, prefix, values }));
44
- }
45
15
  class Logger {
46
- static logger;
16
+ /**
17
+ * Log destinations.
18
+ *
19
+ * By default there is a single destination named "default". You can create new destinations using
20
+ * {@link LogDestination}. Add or remove destinations by modifying this object.
21
+ *
22
+ * Throws an error if you access a destination that doesn't exist.
23
+ */
24
+ static destinations = LogDestinations();
25
+ /**
26
+ * The number of indents to print with messages.
27
+ */
47
28
  static nestingLevel;
48
29
  #name;
49
- /** Add additional logger to the list of loggers including the default configuration. */
50
- static addLogger(identifier, logger, options) {
51
- if (Logger.logger.some((logger2) => logger2.logIdentifier === identifier)) {
52
- throw new NotImplementedError(`Logger "${identifier}" already exists`);
30
+ /**
31
+ * Create a new logger for a facility.
32
+ *
33
+ * @param name the name of the facility
34
+ * @returns a new facility
35
+ */
36
+ static get(name) {
37
+ return new Logger(name);
38
+ }
39
+ /**
40
+ * Get the default log level.
41
+ */
42
+ static get level() {
43
+ return LogDestination.defaults.level;
44
+ }
45
+ /**
46
+ * Set log level as name or number for all destinations.
47
+ */
48
+ static set level(level) {
49
+ level = LogLevel(level);
50
+ LogDestination.defaults.level = level;
51
+ for (const name in this.destinations) {
52
+ this.destinations[name].level = level;
53
+ }
54
+ }
55
+ /**
56
+ * Get the default facility levels.
57
+ */
58
+ static get facilityLevels() {
59
+ return LogDestination.defaults.facilityLevels;
60
+ }
61
+ /**
62
+ * Set log level as name or number for facilities in all destinations.
63
+ *
64
+ * Existing levels that are not named in {@link levels} will remain unchanged.
65
+ */
66
+ static set facilityLevels(levels) {
67
+ for (const name in levels) {
68
+ levels[name] = LogLevel(levels[name]);
69
+ }
70
+ Object.assign(LogDestination.defaults.facilityLevels, levels);
71
+ for (const name in this.destinations) {
72
+ Object.assign(this.destinations[name].facilityLevels, levels);
73
+ }
74
+ }
75
+ /**
76
+ * Get the default format name.
77
+ */
78
+ static get format() {
79
+ return LogDestination.defaults.format.name;
80
+ }
81
+ /**
82
+ * Set the format for all destinations.
83
+ */
84
+ static set format(format) {
85
+ format = LogFormat(format);
86
+ LogDestination.defaults.format = format;
87
+ for (const name in this.destinations) {
88
+ this.destinations[name].format = format;
89
+ }
90
+ }
91
+ /**
92
+ * Mask a string with a given character. If unmaskedLength is provided then these number of characters will be
93
+ * shown unmasked.
94
+ *
95
+ * @param str String to mask
96
+ * @param maskChar character to mask with
97
+ * @param unmaskedLength number of characters to show unmasked in the beginning
98
+ */
99
+ static maskString(str, maskChar = "*", unmaskedLength) {
100
+ return str.substring(0, unmaskedLength ?? 0) + str.substring(unmaskedLength ?? 0).replace(/./g, maskChar);
101
+ }
102
+ /**
103
+ * Perform operations in a nested logging context. Messages will be
104
+ * indented while the context executes.
105
+ */
106
+ static nest(context) {
107
+ this.nestingLevel++;
108
+ try {
109
+ return context();
110
+ } finally {
111
+ this.nestingLevel--;
112
+ }
113
+ }
114
+ /**
115
+ * Async version of nest().
116
+ */
117
+ static async nestAsync(context) {
118
+ this.nestingLevel++;
119
+ try {
120
+ return await context();
121
+ } finally {
122
+ this.nestingLevel--;
123
+ }
124
+ }
125
+ /**
126
+ * Unhandled error reporter.
127
+ *
128
+ * Some environments do not report full error details such as {@link Error#cause} and {@link AggregateError#errors}.
129
+ *
130
+ * To ensure these details are always recorded somewhere, unhandled errors may be reported here.
131
+ *
132
+ * To disable this behavior replace this function.
133
+ */
134
+ static reportUnhandledError(error) {
135
+ try {
136
+ Logger.get("Logger").fatal("Unhandled error detected:", error);
137
+ } catch (e) {
138
+ }
139
+ }
140
+ constructor(name) {
141
+ this.#name = name;
142
+ }
143
+ debug(...values) {
144
+ this.#log(LogLevel.DEBUG, values);
145
+ }
146
+ info(...values) {
147
+ this.#log(LogLevel.INFO, values);
148
+ }
149
+ notice(...values) {
150
+ this.#log(LogLevel.NOTICE, values);
151
+ }
152
+ warn(...values) {
153
+ this.#log(LogLevel.WARN, values);
154
+ }
155
+ error(...values) {
156
+ this.#log(LogLevel.ERROR, values);
157
+ }
158
+ fatal(...values) {
159
+ this.#log(LogLevel.FATAL, values);
160
+ }
161
+ log(level, ...values) {
162
+ this.#log(level, values);
163
+ }
164
+ #log(level, values) {
165
+ for (const name in Logger.destinations) {
166
+ const dest = Logger.destinations[name];
167
+ if (level < (dest.facilityLevels?.[this.#name] ?? dest.level)) {
168
+ return;
169
+ }
170
+ if (!dest.context) {
171
+ dest.context = Diagnostic.Context();
172
+ }
173
+ dest.context.run(
174
+ () => dest.add(
175
+ Diagnostic.message({
176
+ now: Time.now(),
177
+ facility: this.#name,
178
+ level,
179
+ prefix: nestingPrefix(),
180
+ values
181
+ })
182
+ )
183
+ );
53
184
  }
54
- Logger.logger.push({
55
- logIdentifier: identifier,
56
- logFormatter: logFormatterFor(options?.logFormat ?? LogFormat.PLAIN),
57
- log: logger,
58
- defaultLogLevel: options?.defaultLogLevel ?? LogLevel.DEBUG,
59
- logLevels: options?.logLevels ?? {}
185
+ }
186
+ //
187
+ // DEPRECATED API SURFACE FOLLOWS
188
+ //
189
+ /**
190
+ * Stringify a value (BigInt aware) as JSON.
191
+ *
192
+ * @param data the value to stringify
193
+ * @returns the stringified value
194
+ *
195
+ * @deprecated use {@link Diagnostic.json}
196
+ */
197
+ static toJSON(data) {
198
+ return JSON.stringify(data, (_, value) => {
199
+ if (typeof value === "bigint") {
200
+ return value.toString();
201
+ }
202
+ if (value instanceof Uint8Array) {
203
+ return Bytes.toHex(value);
204
+ }
205
+ if (value === void 0) {
206
+ return "undefined";
207
+ }
208
+ return value;
60
209
  });
61
210
  }
211
+ /**
212
+ * Add additional logger to the list of loggers including the default configuration.
213
+ *
214
+ * @deprecated use {@link destinations}
215
+ */
216
+ static addLogger(identifier, logger, options) {
217
+ if (identifier in this.destinations) {
218
+ throw new ImplementationError(`Logger "${identifier}" already exists`);
219
+ }
220
+ const dest = LogDestination({ name: identifier });
221
+ const legacy = adaptDestinationToLegacy(dest);
222
+ legacy.log = logger;
223
+ if (options?.defaultLogLevel !== void 0) {
224
+ legacy.defaultLogLevel = options.defaultLogLevel;
225
+ }
226
+ if (options?.logLevels !== void 0) {
227
+ legacy.logLevels = options.logLevels;
228
+ }
229
+ if (options?.logFormat !== void 0) {
230
+ legacy.logFormatter = logFormatterFor(options.logFormat);
231
+ }
232
+ this.destinations[identifier] = dest;
233
+ }
234
+ /**
235
+ * @deprecated use {@link destinations}
236
+ */
62
237
  static removeLogger(identifier) {
63
- const index = Logger.logger.findIndex((logger) => logger.logIdentifier === identifier);
64
- if (index === -1) {
238
+ if (!(identifier in this.destinations)) {
65
239
  throw new NotImplementedError(`Logger "${identifier}" does not exist`);
66
240
  }
67
- Logger.logger.splice(index, 1);
241
+ delete this.destinations[identifier];
68
242
  }
69
243
  /**
70
244
  * Check if a logger with the matching identifier exists.
71
245
  * @param identifier The identifier of the logger
246
+ *
247
+ * @deprecated use {@link destinations}
72
248
  */
73
249
  static hasLoggerForIdentifier(identifier) {
74
- return Logger.logger.some((logger) => logger.logIdentifier === identifier);
250
+ return identifier in this.destinations;
75
251
  }
76
252
  /**
77
253
  * Get the logger with the matching identifier.
78
254
  * @param identifier The identifier of the logger
255
+ *
256
+ * @deprecated use {@link destinations}
79
257
  */
80
258
  static getLoggerForIdentifier(identifier) {
81
- const logger = Logger.logger.find((logger2) => logger2.logIdentifier === identifier);
82
- if (logger === void 0) {
259
+ const dest = this.destinations[identifier];
260
+ if (dest === void 0) {
83
261
  throw new NotImplementedError(`Unknown logger "${identifier}"`);
84
262
  }
85
- return logger;
86
- }
87
- /**
88
- * Set log level using configuration-style level name for the default logger.
89
- */
90
- static set level(level) {
91
- if (level === void 0) {
92
- level = LogLevel.DEBUG;
93
- }
94
- let levelNum;
95
- if (typeof level === "string") {
96
- if (level.match(/^\d+$/)) {
97
- levelNum = Number.parseInt(level);
98
- } else {
99
- levelNum = LogLevel[level.toUpperCase()];
100
- if (levelNum === void 0) {
101
- throw new ImplementationError(`Unsupported log level "${level}"`);
102
- }
103
- }
104
- } else {
105
- levelNum = level;
106
- }
107
- if (LogLevel[levelNum] === void 0) {
108
- throw new ImplementationError(`Unsupported log level "${level}"`);
109
- }
110
- Logger.defaultLogLevel = levelNum;
263
+ return adaptDestinationToLegacy(dest);
111
264
  }
112
265
  /**
113
- * Set logFormatter using configuration-style format name.
114
- *
115
- * @param format the name of the formatter (see Format enum)
266
+ * @deprecated use {@link destinations}
116
267
  */
117
- static set format(format) {
118
- Logger.setLogFormatterForLogger("default", logFormatterFor(format));
268
+ static getLoggerforIdentifier(identifier) {
269
+ return this.getLoggerForIdentifier(identifier);
119
270
  }
120
271
  /**
121
272
  * Set facility loglevels for the default logger.
122
273
  * @param levels The levels to set
274
+ *
275
+ * @deprecated use {@link destinations}
123
276
  */
124
277
  static set logLevels(levels) {
125
278
  Logger.setLogLevelsForLogger("default", levels);
126
279
  }
127
280
  /**
128
281
  * Get facility loglevels for the default logger.
282
+ *
283
+ * @deprecated use {@link Logger.facilityLevels}
129
284
  */
130
285
  static get logLevels() {
131
286
  return Logger.getLoggerForIdentifier("default").logLevels;
@@ -134,12 +289,16 @@ class Logger {
134
289
  * Set default loglevel for the default logger.
135
290
  *
136
291
  * @param level The level to set
292
+ *
293
+ * @deprecated use {@link Logger.level}
137
294
  */
138
295
  static set defaultLogLevel(level) {
139
296
  Logger.setDefaultLoglevelForLogger("default", level);
140
297
  }
141
298
  /**
142
299
  * Get default loglevel for the default logger.
300
+ *
301
+ * @deprecated use {@link destinations}
143
302
  */
144
303
  static get defaultLogLevel() {
145
304
  return Logger.getLoggerForIdentifier("default").defaultLogLevel;
@@ -148,12 +307,16 @@ class Logger {
148
307
  * Set the log function for the default logger.
149
308
  *
150
309
  * @param log The log function to set
310
+ *
311
+ * @deprecated use {@link destinations}
151
312
  */
152
313
  static set log(log) {
153
314
  Logger.setLogger("default", log);
154
315
  }
155
316
  /**
156
317
  * Get the log function for the default logger.
318
+ *
319
+ * @deprecated use {@link destinations}
157
320
  */
158
321
  static get log() {
159
322
  return Logger.getLoggerForIdentifier("default").log;
@@ -162,12 +325,16 @@ class Logger {
162
325
  * Set the log formatter for the default logger.
163
326
  *
164
327
  * @param logFormatter
328
+ *
329
+ * @deprecated use {@link destinations}
165
330
  */
166
331
  static set logFormatter(logFormatter) {
167
332
  Logger.setLogFormatterForLogger("default", logFormatter);
168
333
  }
169
334
  /**
170
335
  * Get the log formatter for the default logger.
336
+ *
337
+ * @deprecated use {@link destinations}
171
338
  */
172
339
  static get logFormatter() {
173
340
  return Logger.getLoggerForIdentifier("default").logFormatter;
@@ -177,208 +344,55 @@ class Logger {
177
344
  *
178
345
  * @param identifier The identifier of the logger
179
346
  * @param format the name of the formatter (see Format enum)
347
+ *
348
+ * @deprecated use {@link destinations}
180
349
  */
181
350
  static setFormatForLogger(identifier, format) {
182
- const logger = Logger.logger.find((logger2) => logger2.logIdentifier === identifier);
183
- if (logger) {
184
- logger.logFormatter = logFormatterFor(format);
185
- } else {
186
- throw new NotImplementedError(`Unknown logger "${identifier}"`);
187
- }
351
+ this.getLoggerForIdentifier(identifier).logFormatter = logFormatterFor(format);
188
352
  }
189
353
  /**
190
354
  * Set default loglevel for the logger with the matching identifier.
191
355
  *
192
356
  * @param identifier The identifier of the logger
193
357
  * @param level The level to set
358
+ *
359
+ * @deprecated use {@link destinations}
194
360
  */
195
361
  static setDefaultLoglevelForLogger(identifier, level) {
196
- const logger = Logger.logger.find((logger2) => logger2.logIdentifier === identifier);
197
- if (logger) {
198
- logger.defaultLogLevel = level;
199
- } else {
200
- throw new NotImplementedError(`Unknown logger "${identifier}"`);
201
- }
362
+ this.getLoggerForIdentifier(identifier).defaultLogLevel = level;
202
363
  }
203
364
  /**
204
365
  * Set facility loglevels for the logger with the matching identifier.
205
366
  *
206
367
  * @param identifier The identifier of the logger
207
368
  * @param levels The levels to set
369
+ *
370
+ * @deprecated use {@link destinations}
208
371
  */
209
372
  static setLogLevelsForLogger(identifier, levels) {
210
- const logger = Logger.logger.find((logger2) => logger2.logIdentifier === identifier);
211
- if (logger) {
212
- logger.logLevels = levels;
213
- } else {
214
- throw new NotImplementedError(`Unknown logger "${identifier}"`);
215
- }
373
+ this.getLoggerForIdentifier(identifier).logLevels = levels;
216
374
  }
217
375
  /**
218
376
  * Set the log function for the logger with the matching identifier.
219
377
  *
220
378
  * @param identifier The identifier of the logger
221
379
  * @param log The log function to set
380
+ *
381
+ * @deprecated use {@link destinations}
222
382
  */
223
383
  static setLogger(identifier, log) {
224
- const logger = Logger.logger.find((logger2) => logger2.logIdentifier === identifier);
225
- if (logger) {
226
- logger.log = log;
227
- } else {
228
- throw new NotImplementedError(`Unknown logger "${identifier}"`);
229
- }
384
+ this.getLoggerForIdentifier(identifier).log = log;
230
385
  }
231
386
  /**
232
387
  * Set the log formatter for the logger with the matching identifier.
233
388
  *
234
389
  * @param identifier The identifier of the logger
235
390
  * @param logFormatter The log formatter to set
236
- */
237
- static setLogFormatterForLogger(identifier, logFormatter) {
238
- const logger = Logger.logger.find((logger2) => logger2.logIdentifier === identifier);
239
- if (logger) {
240
- logger.logFormatter = logFormatter;
241
- } else {
242
- throw new NotImplementedError(`Unknown logger "${identifier}"`);
243
- }
244
- }
245
- /**
246
- * Create a new facility.
247
- *
248
- * @param name the name of the facility
249
- * @returns a new facility
250
- */
251
- static get(name) {
252
- return new Logger(name);
253
- }
254
- /**
255
- * Stringify a value (BigInt aware) as JSON.
256
- *
257
- * @param data the value to stringify
258
- * @returns the stringified value
259
- */
260
- static toJSON(data) {
261
- return JSON.stringify(data, (_, value) => {
262
- if (typeof value === "bigint") {
263
- return value.toString();
264
- }
265
- if (value instanceof Uint8Array) {
266
- return Bytes.toHex(value);
267
- }
268
- if (value === void 0) {
269
- return "undefined";
270
- }
271
- return value;
272
- });
273
- }
274
- /**
275
- * Mask a string with a given character. If unmaskedLength is provided then these number of characters will be
276
- * shown unmasked.
277
- *
278
- * @param str String to mask
279
- * @param maskChar character to mask with
280
- * @param unmaskedLength number of characters to show unmasked in the beginning
281
- */
282
- static maskString(str, maskChar = "*", unmaskedLength) {
283
- return str.substring(0, unmaskedLength ?? 0) + str.substring(unmaskedLength ?? 0).replace(/./g, maskChar);
284
- }
285
- /**
286
- * Perform operations in a nested logging context. Messages will be
287
- * indented while the context executes.
288
- */
289
- static nest(context) {
290
- this.nestingLevel++;
291
- try {
292
- return context();
293
- } finally {
294
- this.nestingLevel--;
295
- }
296
- }
297
- /**
298
- * Async version of nest().
299
- */
300
- static async nestAsync(context) {
301
- this.nestingLevel++;
302
- try {
303
- return await context();
304
- } finally {
305
- this.nestingLevel--;
306
- }
307
- }
308
- /**
309
- * Unhandled error reporter.
310
- *
311
- * Some environments do not report full error details such as {@link Error#cause} and {@link AggregateError#errors}.
312
- *
313
- * To ensure these details are always recorded somewhere, unhandled errors may be reported here.
314
391
  *
315
- * To disable this behavior replace this function.
316
- */
317
- static reportUnhandledError(error) {
318
- try {
319
- Logger.get("Logger").fatal("Unhandled error detected:", error);
320
- } catch (e) {
321
- }
322
- }
323
- /**
324
- * Invoke logic and return any log messages produced.
392
+ * @deprecated use {@link destinations}
325
393
  */
326
- static capture(fn, fromLogger = "default") {
327
- if (!Logger) {
328
- throw new Error("No logger loaded, cannot capture logs");
329
- }
330
- const logger = Logger.getLoggerForIdentifier(fromLogger);
331
- const actualLogSettings = {
332
- logFormatter: logger.logFormatter,
333
- log: logger.log,
334
- defaultLogLevel: logger.defaultLogLevel,
335
- logLevels: { ...logger.logLevels }
336
- };
337
- try {
338
- Logger.setFormatForLogger(fromLogger, LogFormat.PLAIN);
339
- const captured = new Array();
340
- Logger.setLogger(
341
- fromLogger,
342
- (level, message) => captured.push({
343
- level,
344
- message: message.replace(/\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\.\d\d\d/, "xxxx-xx-xx xx:xx:xx.xxx")
345
- })
346
- );
347
- fn();
348
- return captured;
349
- } finally {
350
- Logger.setLogFormatterForLogger(fromLogger, actualLogSettings.logFormatter);
351
- Logger.setDefaultLoglevelForLogger(fromLogger, actualLogSettings.defaultLogLevel);
352
- Logger.setLogLevelsForLogger(fromLogger, actualLogSettings.logLevels);
353
- Logger.setLogger(fromLogger, actualLogSettings.log);
354
- }
355
- }
356
- constructor(name) {
357
- this.#name = name;
358
- }
359
- debug = (...values) => this.#log(LogLevel.DEBUG, values);
360
- info = (...values) => this.#log(LogLevel.INFO, values);
361
- notice = (...values) => this.#log(LogLevel.NOTICE, values);
362
- warn = (...values) => this.#log(LogLevel.WARN, values);
363
- error = (...values) => this.#log(LogLevel.ERROR, values);
364
- fatal = (...values) => this.#log(LogLevel.FATAL, values);
365
- log = (level, ...values) => this.#log(level, values);
366
- #log(level, values) {
367
- for (const logger of Logger.logger) {
368
- if (level < (logger.logLevels[this.#name] ?? logger.defaultLogLevel)) {
369
- return;
370
- }
371
- if (!logger.context) {
372
- logger.context = Diagnostic.Context();
373
- }
374
- logger.context.run(
375
- () => logger.log(
376
- level,
377
- logger.logFormatter(Time.now(), level, this.#name, nestingPrefix(), values),
378
- this.#name
379
- )
380
- );
381
- }
394
+ static setLogFormatterForLogger(identifier, logFormatter) {
395
+ this.getLoggerForIdentifier(identifier).logFormatter = logFormatter;
382
396
  }
383
397
  }
384
398
  function nestingPrefix() {
@@ -388,21 +402,49 @@ function nestingPrefix() {
388
402
  return "";
389
403
  }
390
404
  Boot.init(() => {
391
- Logger.logger = new Array({
392
- logIdentifier: "default",
393
- logFormatter: LogFormat.plain,
394
- log: consoleLogger,
395
- defaultLogLevel: LogLevel.DEBUG,
396
- logLevels: {}
397
- });
405
+ Logger.destinations = LogDestinations();
398
406
  Logger.nestingLevel = 0;
399
407
  if (typeof MatterHooks !== "undefined") {
400
408
  MatterHooks.loggerSetup?.(Logger);
401
409
  }
402
410
  });
403
411
  CancelablePromise.logger = Logger.get("CancelablePromise");
412
+ function logFormatterFor(formatName) {
413
+ const format = LogFormat(formatName);
414
+ return (now, level, facility, prefix, ...values) => format(Diagnostic.message({ now, level, facility, prefix, values }));
415
+ }
416
+ function adaptDestinationToLegacy(destination) {
417
+ return {
418
+ get logIdentifier() {
419
+ return destination.name;
420
+ },
421
+ get logFormatter() {
422
+ return (now, level, facility, prefix, values) => destination.format(Diagnostic.message({ now, level, facility, prefix, values }));
423
+ },
424
+ set logFormatter(logFormatter) {
425
+ destination.format = (message) => logFormatter(message.now, message.level, message.facility, message.prefix, message.values);
426
+ },
427
+ get log() {
428
+ return (level, formattedLog, facility) => destination.write(formattedLog, Diagnostic.message({ level, facility }));
429
+ },
430
+ set log(log) {
431
+ destination.write = (text, message) => log(message.level, text, message.facility);
432
+ },
433
+ get defaultLogLevel() {
434
+ return destination.level;
435
+ },
436
+ set defaultLogLevel(level) {
437
+ destination.level = level;
438
+ },
439
+ get logLevels() {
440
+ return destination.facilityLevels;
441
+ },
442
+ set logLevels(levels) {
443
+ destination.facilityLevels = levels;
444
+ }
445
+ };
446
+ }
404
447
  export {
405
- Logger,
406
- consoleLogger
448
+ Logger
407
449
  };
408
450
  //# sourceMappingURL=Logger.js.map