@matter/general 0.13.0-alpha.0-20250318-c1aa38b08 → 0.13.0-alpha.0-20250323-770919c6a

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