@logtape/logtape 2.1.0-dev.576 → 2.1.0-dev.600
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/dist/config.cjs +108 -18
- package/dist/config.d.cts.map +1 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +108 -18
- package/dist/config.js.map +1 -1
- package/dist/filter.cjs +191 -0
- package/dist/filter.d.cts +138 -1
- package/dist/filter.d.cts.map +1 -1
- package/dist/filter.d.ts +138 -1
- package/dist/filter.d.ts.map +1 -1
- package/dist/filter.js +191 -1
- package/dist/filter.js.map +1 -1
- package/dist/formatter.cjs +159 -14
- package/dist/formatter.d.cts +79 -1
- package/dist/formatter.d.cts.map +1 -1
- package/dist/formatter.d.ts +79 -1
- package/dist/formatter.d.ts.map +1 -1
- package/dist/formatter.js +157 -14
- package/dist/formatter.js.map +1 -1
- package/dist/logger.cjs +7 -0
- package/dist/logger.d.cts.map +1 -1
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +7 -0
- package/dist/logger.js.map +1 -1
- package/dist/mod.cjs +3 -0
- package/dist/mod.d.cts +3 -3
- package/dist/mod.d.ts +3 -3
- package/dist/mod.js +3 -3
- package/package.json +1 -1
- package/skills/logtape/SKILL.md +4 -0
package/dist/config.cjs
CHANGED
|
@@ -14,13 +14,21 @@ let currentConfig = null;
|
|
|
14
14
|
*/
|
|
15
15
|
const strongRefs = /* @__PURE__ */ new Set();
|
|
16
16
|
/**
|
|
17
|
-
*
|
|
17
|
+
* Sync filter disposables to dispose when resetting the configuration.
|
|
18
18
|
*/
|
|
19
|
-
const
|
|
19
|
+
const filterDisposables = /* @__PURE__ */ new Set();
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Sync sink disposables to dispose when resetting the configuration.
|
|
22
22
|
*/
|
|
23
|
-
const
|
|
23
|
+
const sinkDisposables = /* @__PURE__ */ new Set();
|
|
24
|
+
/**
|
|
25
|
+
* Async filter disposables to dispose when resetting the configuration.
|
|
26
|
+
*/
|
|
27
|
+
const asyncFilterDisposables = /* @__PURE__ */ new Set();
|
|
28
|
+
/**
|
|
29
|
+
* Async sink disposables to dispose when resetting the configuration.
|
|
30
|
+
*/
|
|
31
|
+
const asyncSinkDisposables = /* @__PURE__ */ new Set();
|
|
24
32
|
/**
|
|
25
33
|
* Check if a config is for the meta logger.
|
|
26
34
|
*/
|
|
@@ -126,7 +134,7 @@ async function configure(config) {
|
|
|
126
134
|
*/
|
|
127
135
|
function configureSync(config) {
|
|
128
136
|
if (currentConfig != null && !config.reset) throw new ConfigError("Already configured; if you want to reset, turn on the reset flag.");
|
|
129
|
-
if (
|
|
137
|
+
if (asyncFilterDisposables.size > 0 || asyncSinkDisposables.size > 0) throw new ConfigError("Previously configured async disposables are still active. Use configure() instead or explicitly dispose them using dispose().");
|
|
130
138
|
resetSync();
|
|
131
139
|
try {
|
|
132
140
|
configureInternal(config, false);
|
|
@@ -161,15 +169,21 @@ function configureInternal(config, allowAsync) {
|
|
|
161
169
|
}
|
|
162
170
|
require_logger.LoggerImpl.getLogger().contextLocalStorage = config.contextLocalStorage;
|
|
163
171
|
for (const sink of Object.values(config.sinks)) {
|
|
164
|
-
if (Symbol.asyncDispose in sink) if (allowAsync)
|
|
172
|
+
if (Symbol.asyncDispose in sink) if (allowAsync) asyncSinkDisposables.add(sink);
|
|
165
173
|
else throw new ConfigError("Async disposables cannot be used with configureSync().");
|
|
166
|
-
if (Symbol.dispose in sink)
|
|
174
|
+
if (Symbol.dispose in sink) sinkDisposables.add(sink);
|
|
167
175
|
}
|
|
168
176
|
for (const filter of Object.values(config.filters ?? {})) {
|
|
169
177
|
if (filter == null || typeof filter === "string") continue;
|
|
170
|
-
if (Symbol.asyncDispose in filter)
|
|
171
|
-
|
|
172
|
-
|
|
178
|
+
if (Symbol.asyncDispose in filter) {
|
|
179
|
+
if (allowAsync) asyncFilterDisposables.add(filter);
|
|
180
|
+
else throw new ConfigError("Async disposables cannot be used with configureSync().");
|
|
181
|
+
asyncSinkDisposables.delete(filter);
|
|
182
|
+
}
|
|
183
|
+
if (Symbol.dispose in filter) {
|
|
184
|
+
filterDisposables.add(filter);
|
|
185
|
+
sinkDisposables.delete(filter);
|
|
186
|
+
}
|
|
173
187
|
}
|
|
174
188
|
registerDisposeHook(allowAsync);
|
|
175
189
|
const meta = require_logger.LoggerImpl.getLogger(["logtape", "meta"]);
|
|
@@ -213,13 +227,28 @@ function resetInternal() {
|
|
|
213
227
|
* Dispose of the disposables.
|
|
214
228
|
*/
|
|
215
229
|
async function dispose() {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
230
|
+
const errors = [];
|
|
231
|
+
try {
|
|
232
|
+
disposeSyncFilters();
|
|
233
|
+
} catch (error) {
|
|
234
|
+
errors.push(error);
|
|
235
|
+
}
|
|
236
|
+
try {
|
|
237
|
+
await disposeAsyncFilters();
|
|
238
|
+
} catch (error) {
|
|
239
|
+
errors.push(error);
|
|
240
|
+
}
|
|
241
|
+
try {
|
|
242
|
+
disposeSyncSinks();
|
|
243
|
+
} catch (error) {
|
|
244
|
+
errors.push(error);
|
|
221
245
|
}
|
|
222
|
-
|
|
246
|
+
try {
|
|
247
|
+
await disposeAsyncSinks();
|
|
248
|
+
} catch (error) {
|
|
249
|
+
errors.push(error);
|
|
250
|
+
}
|
|
251
|
+
throwDisposeErrors(errors);
|
|
223
252
|
}
|
|
224
253
|
/**
|
|
225
254
|
* Dispose of the sync disposables. Async disposables will be untouched,
|
|
@@ -227,8 +256,69 @@ async function dispose() {
|
|
|
227
256
|
* @since 0.9.0
|
|
228
257
|
*/
|
|
229
258
|
function disposeSync() {
|
|
230
|
-
|
|
231
|
-
|
|
259
|
+
const errors = [];
|
|
260
|
+
try {
|
|
261
|
+
disposeSyncFilters();
|
|
262
|
+
} catch (error) {
|
|
263
|
+
errors.push(error);
|
|
264
|
+
}
|
|
265
|
+
try {
|
|
266
|
+
disposeSyncSinks();
|
|
267
|
+
} catch (error) {
|
|
268
|
+
errors.push(error);
|
|
269
|
+
}
|
|
270
|
+
throwDisposeErrors(errors);
|
|
271
|
+
}
|
|
272
|
+
function disposeSyncFilters() {
|
|
273
|
+
disposeSyncDisposables(filterDisposables);
|
|
274
|
+
}
|
|
275
|
+
function disposeSyncSinks() {
|
|
276
|
+
disposeSyncDisposables(sinkDisposables);
|
|
277
|
+
}
|
|
278
|
+
function disposeSyncDisposables(disposables) {
|
|
279
|
+
const errors = [];
|
|
280
|
+
try {
|
|
281
|
+
for (const disposable of disposables) try {
|
|
282
|
+
disposable[Symbol.dispose]();
|
|
283
|
+
} catch (error) {
|
|
284
|
+
errors.push(error);
|
|
285
|
+
} finally {
|
|
286
|
+
disposables.delete(disposable);
|
|
287
|
+
}
|
|
288
|
+
} finally {
|
|
289
|
+
disposables.clear();
|
|
290
|
+
}
|
|
291
|
+
throwDisposeErrors(errors);
|
|
292
|
+
}
|
|
293
|
+
async function disposeAsyncFilters() {
|
|
294
|
+
await disposeAsyncDisposables(asyncFilterDisposables);
|
|
295
|
+
}
|
|
296
|
+
async function disposeAsyncSinks() {
|
|
297
|
+
await disposeAsyncDisposables(asyncSinkDisposables);
|
|
298
|
+
}
|
|
299
|
+
async function disposeAsyncDisposables(disposables) {
|
|
300
|
+
const promises = [];
|
|
301
|
+
try {
|
|
302
|
+
for (const disposable of disposables) try {
|
|
303
|
+
promises.push(Promise.resolve(disposable[Symbol.asyncDispose]()));
|
|
304
|
+
} catch (error) {
|
|
305
|
+
promises.push(Promise.reject(error));
|
|
306
|
+
} finally {
|
|
307
|
+
disposables.delete(disposable);
|
|
308
|
+
}
|
|
309
|
+
} finally {
|
|
310
|
+
disposables.clear();
|
|
311
|
+
}
|
|
312
|
+
await settleDisposePromises(promises);
|
|
313
|
+
}
|
|
314
|
+
async function settleDisposePromises(promises) {
|
|
315
|
+
const results = await Promise.allSettled(promises);
|
|
316
|
+
throwDisposeErrors(results.filter((result) => result.status === "rejected").map((result) => result.reason));
|
|
317
|
+
}
|
|
318
|
+
function throwDisposeErrors(errors) {
|
|
319
|
+
if (errors.length < 1) return;
|
|
320
|
+
if (errors.length === 1) throw errors[0];
|
|
321
|
+
throw new AggregateError(errors, "Multiple errors occurred while disposing LogTape resources.");
|
|
232
322
|
}
|
|
233
323
|
/**
|
|
234
324
|
* A configuration error.
|
package/dist/config.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.cts","names":[],"sources":["../src/config.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AASA;AAAuB,UAAN,MAAM,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAA;;;;EAUK,KAAE,EALrB,MAKqB,CALd,OAKc,EALL,IAKK,CAAA;EAAU;;;;EAKjB,OAMqB,CAAA,EAXhC,MAWgC,CAXzB,SAWyB,EAXd,UAWc,CAAA;EAAM;AAAP;AAW3C;EAA6B,OAAA,EAjBlB,YAiBkB,CAjBL,OAiBK,EAjBI,SAiBJ,CAAA,EAAA;EAAA;;;AAoCL;
|
|
1
|
+
{"version":3,"file":"config.d.cts","names":[],"sources":["../src/config.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AASA;AAAuB,UAAN,MAAM,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAA;;;;EAUK,KAAE,EALrB,MAKqB,CALd,OAKc,EALL,IAKK,CAAA;EAAU;;;;EAKjB,OAMqB,CAAA,EAXhC,MAWgC,CAXzB,SAWyB,EAXd,UAWc,CAAA;EAAM;AAAP;AAW3C;EAA6B,OAAA,EAjBlB,YAiBkB,CAjBL,OAiBK,EAjBI,SAiBJ,CAAA,EAAA;EAAA;;;AAoCL;EAuHF,mBAAS,CAAA,EAtKP,mBAsKO,CAtKa,MAsKb,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA;EAAA;;;EAGI,KAAzB,CAAA,EAAA,OAAA;;AAAoC;AAgD9C;;AACiB,UA/MA,YA+MA,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAO;;AAAR;AA4HhB;EAOsB,QAAK,EAAA,MAAA,GAAI,MAAA,EAAO;EAUtB;AAgBhB;AA8BA;EA8Fa,KAAA,CAAA,EA3dH,OA2de,EAAA;;;;;;;;;;;;;;YA3cb;;;;;;gBAOI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuHM,oEAGZ,OAAO,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgDvB,wEACN,OAAO,SAAS;;;;;iBA4HV,SAAA,CAAA,GAAa;;;;iBAOP,KAAA,CAAA,GAAS;;;;;;iBAUf,SAAA,CAAA;;;;iBAgBM,OAAA,CAAA,GAAW;;;;;;iBA8BjB,WAAA,CAAA;;;;cA8FH,WAAA,SAAoB,KAAK"}
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","names":[],"sources":["../src/config.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AASA;AAAuB,UAAN,MAAM,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAA;;;;EAUK,KAAE,EALrB,MAKqB,CALd,OAKc,EALL,IAKK,CAAA;EAAU;;;;EAKjB,OAMqB,CAAA,EAXhC,MAWgC,CAXzB,SAWyB,EAXd,UAWc,CAAA;EAAM;AAAP;AAW3C;EAA6B,OAAA,EAjBlB,YAiBkB,CAjBL,OAiBK,EAjBI,SAiBJ,CAAA,EAAA;EAAA;;;AAoCL;
|
|
1
|
+
{"version":3,"file":"config.d.ts","names":[],"sources":["../src/config.ts"],"sourcesContent":[],"mappings":";;;;;;;;;AASA;AAAuB,UAAN,MAAM,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAA;;;;EAUK,KAAE,EALrB,MAKqB,CALd,OAKc,EALL,IAKK,CAAA;EAAU;;;;EAKjB,OAMqB,CAAA,EAXhC,MAWgC,CAXzB,SAWyB,EAXd,UAWc,CAAA;EAAM;AAAP;AAW3C;EAA6B,OAAA,EAjBlB,YAiBkB,CAjBL,OAiBK,EAjBI,SAiBJ,CAAA,EAAA;EAAA;;;AAoCL;EAuHF,mBAAS,CAAA,EAtKP,mBAsKO,CAtKa,MAsKb,CAAA,MAAA,EAAA,OAAA,CAAA,CAAA;EAAA;;;EAGI,KAAzB,CAAA,EAAA,OAAA;;AAAoC;AAgD9C;;AACiB,UA/MA,YA+MA,CAAA,gBAAA,MAAA,EAAA,kBAAA,MAAA,CAAA,CAAA;EAAO;;AAAR;AA4HhB;EAOsB,QAAK,EAAA,MAAA,GAAI,MAAA,EAAO;EAUtB;AAgBhB;AA8BA;EA8Fa,KAAA,CAAA,EA3dH,OA2de,EAAA;;;;;;;;;;;;;;YA3cb;;;;;;gBAOI;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuHM,oEAGZ,OAAO,SAAS,aAAa;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAgDvB,wEACN,OAAO,SAAS;;;;;iBA4HV,SAAA,CAAA,GAAa;;;;iBAOP,KAAA,CAAA,GAAS;;;;;;iBAUf,SAAA,CAAA;;;;iBAgBM,OAAA,CAAA,GAAW;;;;;;iBA8BjB,WAAA,CAAA;;;;cA8FH,WAAA,SAAoB,KAAK"}
|
package/dist/config.js
CHANGED
|
@@ -14,13 +14,21 @@ let currentConfig = null;
|
|
|
14
14
|
*/
|
|
15
15
|
const strongRefs = /* @__PURE__ */ new Set();
|
|
16
16
|
/**
|
|
17
|
-
*
|
|
17
|
+
* Sync filter disposables to dispose when resetting the configuration.
|
|
18
18
|
*/
|
|
19
|
-
const
|
|
19
|
+
const filterDisposables = /* @__PURE__ */ new Set();
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Sync sink disposables to dispose when resetting the configuration.
|
|
22
22
|
*/
|
|
23
|
-
const
|
|
23
|
+
const sinkDisposables = /* @__PURE__ */ new Set();
|
|
24
|
+
/**
|
|
25
|
+
* Async filter disposables to dispose when resetting the configuration.
|
|
26
|
+
*/
|
|
27
|
+
const asyncFilterDisposables = /* @__PURE__ */ new Set();
|
|
28
|
+
/**
|
|
29
|
+
* Async sink disposables to dispose when resetting the configuration.
|
|
30
|
+
*/
|
|
31
|
+
const asyncSinkDisposables = /* @__PURE__ */ new Set();
|
|
24
32
|
/**
|
|
25
33
|
* Check if a config is for the meta logger.
|
|
26
34
|
*/
|
|
@@ -126,7 +134,7 @@ async function configure(config) {
|
|
|
126
134
|
*/
|
|
127
135
|
function configureSync(config) {
|
|
128
136
|
if (currentConfig != null && !config.reset) throw new ConfigError("Already configured; if you want to reset, turn on the reset flag.");
|
|
129
|
-
if (
|
|
137
|
+
if (asyncFilterDisposables.size > 0 || asyncSinkDisposables.size > 0) throw new ConfigError("Previously configured async disposables are still active. Use configure() instead or explicitly dispose them using dispose().");
|
|
130
138
|
resetSync();
|
|
131
139
|
try {
|
|
132
140
|
configureInternal(config, false);
|
|
@@ -161,15 +169,21 @@ function configureInternal(config, allowAsync) {
|
|
|
161
169
|
}
|
|
162
170
|
LoggerImpl.getLogger().contextLocalStorage = config.contextLocalStorage;
|
|
163
171
|
for (const sink of Object.values(config.sinks)) {
|
|
164
|
-
if (Symbol.asyncDispose in sink) if (allowAsync)
|
|
172
|
+
if (Symbol.asyncDispose in sink) if (allowAsync) asyncSinkDisposables.add(sink);
|
|
165
173
|
else throw new ConfigError("Async disposables cannot be used with configureSync().");
|
|
166
|
-
if (Symbol.dispose in sink)
|
|
174
|
+
if (Symbol.dispose in sink) sinkDisposables.add(sink);
|
|
167
175
|
}
|
|
168
176
|
for (const filter of Object.values(config.filters ?? {})) {
|
|
169
177
|
if (filter == null || typeof filter === "string") continue;
|
|
170
|
-
if (Symbol.asyncDispose in filter)
|
|
171
|
-
|
|
172
|
-
|
|
178
|
+
if (Symbol.asyncDispose in filter) {
|
|
179
|
+
if (allowAsync) asyncFilterDisposables.add(filter);
|
|
180
|
+
else throw new ConfigError("Async disposables cannot be used with configureSync().");
|
|
181
|
+
asyncSinkDisposables.delete(filter);
|
|
182
|
+
}
|
|
183
|
+
if (Symbol.dispose in filter) {
|
|
184
|
+
filterDisposables.add(filter);
|
|
185
|
+
sinkDisposables.delete(filter);
|
|
186
|
+
}
|
|
173
187
|
}
|
|
174
188
|
registerDisposeHook(allowAsync);
|
|
175
189
|
const meta = LoggerImpl.getLogger(["logtape", "meta"]);
|
|
@@ -213,13 +227,28 @@ function resetInternal() {
|
|
|
213
227
|
* Dispose of the disposables.
|
|
214
228
|
*/
|
|
215
229
|
async function dispose() {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
230
|
+
const errors = [];
|
|
231
|
+
try {
|
|
232
|
+
disposeSyncFilters();
|
|
233
|
+
} catch (error) {
|
|
234
|
+
errors.push(error);
|
|
235
|
+
}
|
|
236
|
+
try {
|
|
237
|
+
await disposeAsyncFilters();
|
|
238
|
+
} catch (error) {
|
|
239
|
+
errors.push(error);
|
|
240
|
+
}
|
|
241
|
+
try {
|
|
242
|
+
disposeSyncSinks();
|
|
243
|
+
} catch (error) {
|
|
244
|
+
errors.push(error);
|
|
221
245
|
}
|
|
222
|
-
|
|
246
|
+
try {
|
|
247
|
+
await disposeAsyncSinks();
|
|
248
|
+
} catch (error) {
|
|
249
|
+
errors.push(error);
|
|
250
|
+
}
|
|
251
|
+
throwDisposeErrors(errors);
|
|
223
252
|
}
|
|
224
253
|
/**
|
|
225
254
|
* Dispose of the sync disposables. Async disposables will be untouched,
|
|
@@ -227,8 +256,69 @@ async function dispose() {
|
|
|
227
256
|
* @since 0.9.0
|
|
228
257
|
*/
|
|
229
258
|
function disposeSync() {
|
|
230
|
-
|
|
231
|
-
|
|
259
|
+
const errors = [];
|
|
260
|
+
try {
|
|
261
|
+
disposeSyncFilters();
|
|
262
|
+
} catch (error) {
|
|
263
|
+
errors.push(error);
|
|
264
|
+
}
|
|
265
|
+
try {
|
|
266
|
+
disposeSyncSinks();
|
|
267
|
+
} catch (error) {
|
|
268
|
+
errors.push(error);
|
|
269
|
+
}
|
|
270
|
+
throwDisposeErrors(errors);
|
|
271
|
+
}
|
|
272
|
+
function disposeSyncFilters() {
|
|
273
|
+
disposeSyncDisposables(filterDisposables);
|
|
274
|
+
}
|
|
275
|
+
function disposeSyncSinks() {
|
|
276
|
+
disposeSyncDisposables(sinkDisposables);
|
|
277
|
+
}
|
|
278
|
+
function disposeSyncDisposables(disposables) {
|
|
279
|
+
const errors = [];
|
|
280
|
+
try {
|
|
281
|
+
for (const disposable of disposables) try {
|
|
282
|
+
disposable[Symbol.dispose]();
|
|
283
|
+
} catch (error) {
|
|
284
|
+
errors.push(error);
|
|
285
|
+
} finally {
|
|
286
|
+
disposables.delete(disposable);
|
|
287
|
+
}
|
|
288
|
+
} finally {
|
|
289
|
+
disposables.clear();
|
|
290
|
+
}
|
|
291
|
+
throwDisposeErrors(errors);
|
|
292
|
+
}
|
|
293
|
+
async function disposeAsyncFilters() {
|
|
294
|
+
await disposeAsyncDisposables(asyncFilterDisposables);
|
|
295
|
+
}
|
|
296
|
+
async function disposeAsyncSinks() {
|
|
297
|
+
await disposeAsyncDisposables(asyncSinkDisposables);
|
|
298
|
+
}
|
|
299
|
+
async function disposeAsyncDisposables(disposables) {
|
|
300
|
+
const promises = [];
|
|
301
|
+
try {
|
|
302
|
+
for (const disposable of disposables) try {
|
|
303
|
+
promises.push(Promise.resolve(disposable[Symbol.asyncDispose]()));
|
|
304
|
+
} catch (error) {
|
|
305
|
+
promises.push(Promise.reject(error));
|
|
306
|
+
} finally {
|
|
307
|
+
disposables.delete(disposable);
|
|
308
|
+
}
|
|
309
|
+
} finally {
|
|
310
|
+
disposables.clear();
|
|
311
|
+
}
|
|
312
|
+
await settleDisposePromises(promises);
|
|
313
|
+
}
|
|
314
|
+
async function settleDisposePromises(promises) {
|
|
315
|
+
const results = await Promise.allSettled(promises);
|
|
316
|
+
throwDisposeErrors(results.filter((result) => result.status === "rejected").map((result) => result.reason));
|
|
317
|
+
}
|
|
318
|
+
function throwDisposeErrors(errors) {
|
|
319
|
+
if (errors.length < 1) return;
|
|
320
|
+
if (errors.length === 1) throw errors[0];
|
|
321
|
+
throw new AggregateError(errors, "Multiple errors occurred while disposing LogTape resources.");
|
|
232
322
|
}
|
|
233
323
|
/**
|
|
234
324
|
* A configuration error.
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.js","names":["currentConfig: Config<string, string> | null","strongRefs: Set<LoggerImpl>","disposables: Set<Disposable>","asyncDisposables: Set<AsyncDisposable>","cfg: LoggerConfig<TSinkId, TFilterId>","allowAsync: boolean","config: Config<TSinkId, TFilterId>","promises: PromiseLike<void>[]","message: string"],"sources":["../src/config.ts"],"sourcesContent":["import type { ContextLocalStorage } from \"./context.ts\";\nimport { type FilterLike, toFilter } from \"./filter.ts\";\nimport type { LogLevel } from \"./level.ts\";\nimport { LoggerImpl } from \"./logger.ts\";\nimport { getConsoleSink, type Sink } from \"./sink.ts\";\n\n/**\n * A configuration for the loggers.\n */\nexport interface Config<TSinkId extends string, TFilterId extends string> {\n /**\n * The sinks to use. The keys are the sink identifiers, and the values are\n * {@link Sink}s.\n */\n sinks: Record<TSinkId, Sink>;\n /**\n * The filters to use. The keys are the filter identifiers, and the values\n * are either {@link Filter}s or {@link LogLevel}s.\n */\n filters?: Record<TFilterId, FilterLike>;\n\n /**\n * The loggers to configure.\n */\n loggers: LoggerConfig<TSinkId, TFilterId>[];\n\n /**\n * The context-local storage to use for implicit contexts.\n * @since 0.7.0\n */\n contextLocalStorage?: ContextLocalStorage<Record<string, unknown>>;\n\n /**\n * Whether to reset the configuration before applying this one.\n */\n reset?: boolean;\n}\n\n/**\n * A logger configuration.\n */\nexport interface LoggerConfig<\n TSinkId extends string,\n TFilterId extends string,\n> {\n /**\n * The category of the logger. If a string, it is equivalent to an array\n * with one element.\n */\n category: string | string[];\n\n /**\n * The sink identifiers to use.\n */\n sinks?: TSinkId[];\n\n /**\n * Whether to inherit the parent's sinks. If `inherit`, the parent's sinks\n * are used along with the specified sinks. If `override`, the parent's\n * sinks are not used, and only the specified sinks are used.\n *\n * The default is `inherit`.\n * @default `\"inherit\"\n * @since 0.6.0\n */\n parentSinks?: \"inherit\" | \"override\";\n\n /**\n * The filter identifiers to use.\n */\n filters?: TFilterId[];\n\n /**\n * The lowest log level to accept. If `null`, the logger will reject all\n * records.\n * @since 0.8.0\n */\n lowestLevel?: LogLevel | null;\n}\n\n/**\n * The current configuration, if any. Otherwise, `null`.\n */\nlet currentConfig: Config<string, string> | null = null;\n\n/**\n * Strong references to the loggers.\n * This is to prevent the loggers from being garbage collected so that their\n * sinks and filters are not removed.\n */\nconst strongRefs: Set<LoggerImpl> = new Set();\n\n/**\n * Disposables to dispose when resetting the configuration.\n */\nconst disposables: Set<Disposable> = new Set();\n\n/**\n * Async disposables to dispose when resetting the configuration.\n */\nconst asyncDisposables: Set<AsyncDisposable> = new Set();\n\n/**\n * Check if a config is for the meta logger.\n */\nfunction isLoggerConfigMeta<TSinkId extends string, TFilterId extends string>(\n cfg: LoggerConfig<TSinkId, TFilterId>,\n): boolean {\n return cfg.category.length === 0 ||\n (cfg.category.length === 1 && cfg.category[0] === \"logtape\") ||\n (cfg.category.length === 2 &&\n cfg.category[0] === \"logtape\" &&\n cfg.category[1] === \"meta\");\n}\n\nfunction registerDisposeHook(allowAsync: boolean): void {\n const handler = allowAsync ? dispose : disposeSync;\n\n if (\n // deno-lint-ignore no-explicit-any\n typeof (globalThis as any).EdgeRuntime !== \"string\" &&\n \"process\" in globalThis &&\n !(\"Deno\" in globalThis)\n ) {\n // deno-lint-ignore no-explicit-any\n const proc = (globalThis as any).process;\n // Use bracket notation to avoid static analysis detection in Edge Runtime.\n const onMethod = proc?.[\"on\"];\n if (typeof onMethod === \"function\") {\n onMethod.call(proc, \"exit\", handler);\n return;\n }\n }\n\n // Some edge runtimes expose neither process.on() nor addEventListener().\n // In those environments users can still call dispose()/disposeSync() manually.\n // deno-lint-ignore no-explicit-any\n const addEventListenerMethod = (globalThis as any).addEventListener;\n if (typeof addEventListenerMethod !== \"function\") return;\n\n if (\"Deno\" in globalThis) {\n addEventListenerMethod.call(globalThis, \"unload\", handler);\n } else {\n addEventListenerMethod.call(globalThis, \"pagehide\", handler);\n }\n}\n\n/**\n * Configure the loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * @example\n * ```typescript\n * await configure({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * filters: {\n * slow: (log) =>\n * \"duration\" in log.properties &&\n * log.properties.duration as number > 1000,\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * lowestLevel: \"info\",\n * },\n * {\n * category: [\"my-app\", \"sql\"],\n * filters: [\"slow\"],\n * lowestLevel: \"debug\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * lowestLevel: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n */\nexport async function configure<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>): Promise<void> {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n await reset();\n try {\n configureInternal(config, true);\n } catch (e) {\n if (e instanceof ConfigError) await reset();\n throw e;\n }\n}\n\n/**\n * Configure sync loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * Also note that passing async sinks or filters will throw. If\n * necessary use {@link resetSync} or {@link disposeSync}.\n *\n * @example\n * ```typescript\n * configureSync({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * lowestLevel: \"info\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * lowestLevel: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n * @since 0.9.0\n */\nexport function configureSync<TSinkId extends string, TFilterId extends string>(\n config: Config<TSinkId, TFilterId>,\n): void {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n if (asyncDisposables.size > 0) {\n throw new ConfigError(\n \"Previously configured async disposables are still active. \" +\n \"Use configure() instead or explicitly dispose them using dispose().\",\n );\n }\n resetSync();\n try {\n configureInternal(config, false);\n } catch (e) {\n if (e instanceof ConfigError) resetSync();\n throw e;\n }\n}\n\nfunction configureInternal<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>, allowAsync: boolean): void {\n currentConfig = config;\n\n let metaConfigured = false;\n const configuredCategories = new Set<string>();\n\n for (const cfg of config.loggers) {\n if (isLoggerConfigMeta(cfg)) {\n metaConfigured = true;\n }\n\n // Check for duplicate logger categories\n const categoryKey = Array.isArray(cfg.category)\n ? JSON.stringify(cfg.category)\n : JSON.stringify([cfg.category]);\n if (configuredCategories.has(categoryKey)) {\n throw new ConfigError(\n `Duplicate logger configuration for category: ${categoryKey}. ` +\n `Each category can only be configured once.`,\n );\n }\n configuredCategories.add(categoryKey);\n\n const logger = LoggerImpl.getLogger(cfg.category);\n for (const sinkId of cfg.sinks ?? []) {\n const sink = config.sinks[sinkId];\n if (!sink) {\n throw new ConfigError(`Sink not found: ${sinkId}.`);\n }\n logger.sinks.push(sink);\n }\n logger.parentSinks = cfg.parentSinks ?? \"inherit\";\n if (cfg.lowestLevel !== undefined) {\n logger.lowestLevel = cfg.lowestLevel;\n }\n for (const filterId of cfg.filters ?? []) {\n const filter = config.filters?.[filterId];\n if (filter === undefined) {\n throw new ConfigError(`Filter not found: ${filterId}.`);\n }\n logger.filters.push(toFilter(filter));\n }\n strongRefs.add(logger);\n }\n\n LoggerImpl.getLogger().contextLocalStorage = config.contextLocalStorage;\n\n for (const sink of Object.values<Sink>(config.sinks)) {\n if (Symbol.asyncDispose in sink) {\n if (allowAsync) asyncDisposables.add(sink as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n }\n if (Symbol.dispose in sink) disposables.add(sink as Disposable);\n }\n\n for (const filter of Object.values<FilterLike>(config.filters ?? {})) {\n if (filter == null || typeof filter === \"string\") continue;\n if (Symbol.asyncDispose in filter) {\n if (allowAsync) asyncDisposables.add(filter as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n }\n if (Symbol.dispose in filter) disposables.add(filter as Disposable);\n }\n\n registerDisposeHook(allowAsync);\n const meta = LoggerImpl.getLogger([\"logtape\", \"meta\"]);\n if (!metaConfigured) {\n meta.sinks.push(getConsoleSink());\n }\n\n meta.info(\n \"LogTape loggers are configured. Note that LogTape itself uses the meta \" +\n \"logger, which has category {metaLoggerCategory}. The meta logger is \" +\n \"used to log internal diagnostics such as sink exceptions. \" +\n \"It's recommended to configure the meta logger with a separate sink \" +\n \"so that you can easily notice if logging itself fails or is \" +\n \"misconfigured. To turn off this message, configure the meta logger \" +\n \"with higher log levels than {dismissLevel}. See also \" +\n \"<https://logtape.org/manual/categories#meta-logger>.\",\n { metaLoggerCategory: [\"logtape\", \"meta\"], dismissLevel: \"info\" },\n );\n}\n\n/**\n * Get the current configuration, if any. Otherwise, `null`.\n * @returns The current configuration, if any. Otherwise, `null`.\n */\nexport function getConfig(): Config<string, string> | null {\n return currentConfig;\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes.\n */\nexport async function reset(): Promise<void> {\n await dispose();\n resetInternal();\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes. Will not clear async\n * sinks, only use with sync sinks. Use {@link reset} if you have async sinks.\n * @since 0.9.0\n */\nexport function resetSync(): void {\n disposeSync();\n resetInternal();\n}\n\nfunction resetInternal(): void {\n const rootLogger = LoggerImpl.getLogger([]);\n rootLogger.resetDescendants();\n delete rootLogger.contextLocalStorage;\n strongRefs.clear();\n currentConfig = null;\n}\n\n/**\n * Dispose of the disposables.\n */\nexport async function dispose(): Promise<void> {\n disposeSync();\n const promises: PromiseLike<void>[] = [];\n for (const disposable of asyncDisposables) {\n promises.push(disposable[Symbol.asyncDispose]());\n asyncDisposables.delete(disposable);\n }\n await Promise.all(promises);\n}\n\n/**\n * Dispose of the sync disposables. Async disposables will be untouched,\n * use {@link dispose} if you have async sinks.\n * @since 0.9.0\n */\nexport function disposeSync(): void {\n for (const disposable of disposables) disposable[Symbol.dispose]();\n disposables.clear();\n}\n\n/**\n * A configuration error.\n */\nexport class ConfigError extends Error {\n /**\n * Constructs a new configuration error.\n * @param message The error message.\n */\n constructor(message: string) {\n super(message);\n this.name = \"ConfigureError\";\n }\n}\n"],"mappings":";;;;;;;;AAmFA,IAAIA,gBAA+C;;;;;;AAOnD,MAAMC,6BAA8B,IAAI;;;;AAKxC,MAAMC,8BAA+B,IAAI;;;;AAKzC,MAAMC,mCAAyC,IAAI;;;;AAKnD,SAAS,mBACPC,KACS;AACT,QAAO,IAAI,SAAS,WAAW,KAC5B,IAAI,SAAS,WAAW,KAAK,IAAI,SAAS,OAAO,aACjD,IAAI,SAAS,WAAW,KACvB,IAAI,SAAS,OAAO,aACpB,IAAI,SAAS,OAAO;AACzB;AAED,SAAS,oBAAoBC,YAA2B;CACtD,MAAM,UAAU,aAAa,UAAU;AAEvC,YAEU,WAAmB,gBAAgB,YAC3C,aAAa,gBACX,UAAU,aACZ;EAEA,MAAM,OAAQ,WAAmB;EAEjC,MAAM,WAAW,OAAO;AACxB,aAAW,aAAa,YAAY;AAClC,YAAS,KAAK,MAAM,QAAQ,QAAQ;AACpC;EACD;CACF;CAKD,MAAM,yBAA0B,WAAmB;AACnD,YAAW,2BAA2B,WAAY;AAElD,KAAI,UAAU,WACZ,wBAAuB,KAAK,YAAY,UAAU,QAAQ;KAE1D,wBAAuB,KAAK,YAAY,YA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D,eAAsB,UAAyB;AAC7C,cAAa;CACb,MAAME,WAAgC,CAAE;AACxC,MAAK,MAAM,cAAc,kBAAkB;AACzC,WAAS,KAAK,WAAW,OAAO,eAAe,CAAC;AAChD,mBAAiB,OAAO,WAAW;CACpC;AACD,OAAM,QAAQ,IAAI,SAAS;AAC5B;;;;;;AAOD,SAAgB,cAAoB;AAClC,MAAK,MAAM,cAAc,YAAa,YAAW,OAAO,UAAU;AAClE,aAAY,OAAO;AACpB;;;;AAKD,IAAa,cAAb,cAAiC,MAAM;;;;;CAKrC,YAAYC,SAAiB;AAC3B,QAAM,QAAQ;AACd,OAAK,OAAO;CACb;AACF"}
|
|
1
|
+
{"version":3,"file":"config.js","names":["currentConfig: Config<string, string> | null","strongRefs: Set<LoggerImpl>","filterDisposables: Set<Disposable>","sinkDisposables: Set<Disposable>","asyncFilterDisposables: Set<AsyncDisposable>","asyncSinkDisposables: Set<AsyncDisposable>","cfg: LoggerConfig<TSinkId, TFilterId>","allowAsync: boolean","config: Config<TSinkId, TFilterId>","errors: unknown[]","disposables: Set<Disposable>","disposables: Set<AsyncDisposable>","promises: PromiseLike<void>[]","promises: readonly PromiseLike<void>[]","errors: readonly unknown[]","message: string"],"sources":["../src/config.ts"],"sourcesContent":["import type { ContextLocalStorage } from \"./context.ts\";\nimport { type FilterLike, toFilter } from \"./filter.ts\";\nimport type { LogLevel } from \"./level.ts\";\nimport { LoggerImpl } from \"./logger.ts\";\nimport { getConsoleSink, type Sink } from \"./sink.ts\";\n\n/**\n * A configuration for the loggers.\n */\nexport interface Config<TSinkId extends string, TFilterId extends string> {\n /**\n * The sinks to use. The keys are the sink identifiers, and the values are\n * {@link Sink}s.\n */\n sinks: Record<TSinkId, Sink>;\n /**\n * The filters to use. The keys are the filter identifiers, and the values\n * are either {@link Filter}s or {@link LogLevel}s.\n */\n filters?: Record<TFilterId, FilterLike>;\n\n /**\n * The loggers to configure.\n */\n loggers: LoggerConfig<TSinkId, TFilterId>[];\n\n /**\n * The context-local storage to use for implicit contexts.\n * @since 0.7.0\n */\n contextLocalStorage?: ContextLocalStorage<Record<string, unknown>>;\n\n /**\n * Whether to reset the configuration before applying this one.\n */\n reset?: boolean;\n}\n\n/**\n * A logger configuration.\n */\nexport interface LoggerConfig<\n TSinkId extends string,\n TFilterId extends string,\n> {\n /**\n * The category of the logger. If a string, it is equivalent to an array\n * with one element.\n */\n category: string | string[];\n\n /**\n * The sink identifiers to use.\n */\n sinks?: TSinkId[];\n\n /**\n * Whether to inherit the parent's sinks. If `inherit`, the parent's sinks\n * are used along with the specified sinks. If `override`, the parent's\n * sinks are not used, and only the specified sinks are used.\n *\n * The default is `inherit`.\n * @default `\"inherit\"\n * @since 0.6.0\n */\n parentSinks?: \"inherit\" | \"override\";\n\n /**\n * The filter identifiers to use.\n */\n filters?: TFilterId[];\n\n /**\n * The lowest log level to accept. If `null`, the logger will reject all\n * records.\n * @since 0.8.0\n */\n lowestLevel?: LogLevel | null;\n}\n\n/**\n * The current configuration, if any. Otherwise, `null`.\n */\nlet currentConfig: Config<string, string> | null = null;\n\n/**\n * Strong references to the loggers.\n * This is to prevent the loggers from being garbage collected so that their\n * sinks and filters are not removed.\n */\nconst strongRefs: Set<LoggerImpl> = new Set();\n\n/**\n * Sync filter disposables to dispose when resetting the configuration.\n */\nconst filterDisposables: Set<Disposable> = new Set();\n\n/**\n * Sync sink disposables to dispose when resetting the configuration.\n */\nconst sinkDisposables: Set<Disposable> = new Set();\n\n/**\n * Async filter disposables to dispose when resetting the configuration.\n */\nconst asyncFilterDisposables: Set<AsyncDisposable> = new Set();\n\n/**\n * Async sink disposables to dispose when resetting the configuration.\n */\nconst asyncSinkDisposables: Set<AsyncDisposable> = new Set();\n\n/**\n * Check if a config is for the meta logger.\n */\nfunction isLoggerConfigMeta<TSinkId extends string, TFilterId extends string>(\n cfg: LoggerConfig<TSinkId, TFilterId>,\n): boolean {\n return cfg.category.length === 0 ||\n (cfg.category.length === 1 && cfg.category[0] === \"logtape\") ||\n (cfg.category.length === 2 &&\n cfg.category[0] === \"logtape\" &&\n cfg.category[1] === \"meta\");\n}\n\nfunction registerDisposeHook(allowAsync: boolean): void {\n const handler = allowAsync ? dispose : disposeSync;\n\n if (\n // deno-lint-ignore no-explicit-any\n typeof (globalThis as any).EdgeRuntime !== \"string\" &&\n \"process\" in globalThis &&\n !(\"Deno\" in globalThis)\n ) {\n // deno-lint-ignore no-explicit-any\n const proc = (globalThis as any).process;\n // Use bracket notation to avoid static analysis detection in Edge Runtime.\n const onMethod = proc?.[\"on\"];\n if (typeof onMethod === \"function\") {\n onMethod.call(proc, \"exit\", handler);\n return;\n }\n }\n\n // Some edge runtimes expose neither process.on() nor addEventListener().\n // In those environments users can still call dispose()/disposeSync() manually.\n // deno-lint-ignore no-explicit-any\n const addEventListenerMethod = (globalThis as any).addEventListener;\n if (typeof addEventListenerMethod !== \"function\") return;\n\n if (\"Deno\" in globalThis) {\n addEventListenerMethod.call(globalThis, \"unload\", handler);\n } else {\n addEventListenerMethod.call(globalThis, \"pagehide\", handler);\n }\n}\n\n/**\n * Configure the loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * @example\n * ```typescript\n * await configure({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * filters: {\n * slow: (log) =>\n * \"duration\" in log.properties &&\n * log.properties.duration as number > 1000,\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * lowestLevel: \"info\",\n * },\n * {\n * category: [\"my-app\", \"sql\"],\n * filters: [\"slow\"],\n * lowestLevel: \"debug\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * lowestLevel: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n */\nexport async function configure<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>): Promise<void> {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n await reset();\n try {\n configureInternal(config, true);\n } catch (e) {\n if (e instanceof ConfigError) await reset();\n throw e;\n }\n}\n\n/**\n * Configure sync loggers with the specified configuration.\n *\n * Note that if the given sinks or filters are disposable, they will be\n * disposed when the configuration is reset, or when the process exits.\n *\n * Also note that passing async sinks or filters will throw. If\n * necessary use {@link resetSync} or {@link disposeSync}.\n *\n * @example\n * ```typescript\n * configureSync({\n * sinks: {\n * console: getConsoleSink(),\n * },\n * loggers: [\n * {\n * category: \"my-app\",\n * sinks: [\"console\"],\n * lowestLevel: \"info\",\n * },\n * {\n * category: \"logtape\",\n * sinks: [\"console\"],\n * lowestLevel: \"error\",\n * },\n * ],\n * });\n * ```\n *\n * @param config The configuration.\n * @since 0.9.0\n */\nexport function configureSync<TSinkId extends string, TFilterId extends string>(\n config: Config<TSinkId, TFilterId>,\n): void {\n if (currentConfig != null && !config.reset) {\n throw new ConfigError(\n \"Already configured; if you want to reset, turn on the reset flag.\",\n );\n }\n if (asyncFilterDisposables.size > 0 || asyncSinkDisposables.size > 0) {\n throw new ConfigError(\n \"Previously configured async disposables are still active. \" +\n \"Use configure() instead or explicitly dispose them using dispose().\",\n );\n }\n resetSync();\n try {\n configureInternal(config, false);\n } catch (e) {\n if (e instanceof ConfigError) resetSync();\n throw e;\n }\n}\n\nfunction configureInternal<\n TSinkId extends string,\n TFilterId extends string,\n>(config: Config<TSinkId, TFilterId>, allowAsync: boolean): void {\n currentConfig = config;\n\n let metaConfigured = false;\n const configuredCategories = new Set<string>();\n\n for (const cfg of config.loggers) {\n if (isLoggerConfigMeta(cfg)) {\n metaConfigured = true;\n }\n\n // Check for duplicate logger categories\n const categoryKey = Array.isArray(cfg.category)\n ? JSON.stringify(cfg.category)\n : JSON.stringify([cfg.category]);\n if (configuredCategories.has(categoryKey)) {\n throw new ConfigError(\n `Duplicate logger configuration for category: ${categoryKey}. ` +\n `Each category can only be configured once.`,\n );\n }\n configuredCategories.add(categoryKey);\n\n const logger = LoggerImpl.getLogger(cfg.category);\n for (const sinkId of cfg.sinks ?? []) {\n const sink = config.sinks[sinkId];\n if (!sink) {\n throw new ConfigError(`Sink not found: ${sinkId}.`);\n }\n logger.sinks.push(sink);\n }\n logger.parentSinks = cfg.parentSinks ?? \"inherit\";\n if (cfg.lowestLevel !== undefined) {\n logger.lowestLevel = cfg.lowestLevel;\n }\n for (const filterId of cfg.filters ?? []) {\n const filter = config.filters?.[filterId];\n if (filter === undefined) {\n throw new ConfigError(`Filter not found: ${filterId}.`);\n }\n logger.filters.push(toFilter(filter));\n }\n strongRefs.add(logger);\n }\n\n LoggerImpl.getLogger().contextLocalStorage = config.contextLocalStorage;\n\n for (const sink of Object.values<Sink>(config.sinks)) {\n if (Symbol.asyncDispose in sink) {\n if (allowAsync) asyncSinkDisposables.add(sink as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n }\n if (Symbol.dispose in sink) sinkDisposables.add(sink as Disposable);\n }\n\n for (const filter of Object.values<FilterLike>(config.filters ?? {})) {\n if (filter == null || typeof filter === \"string\") continue;\n if (Symbol.asyncDispose in filter) {\n if (allowAsync) asyncFilterDisposables.add(filter as AsyncDisposable);\n else {\n throw new ConfigError(\n \"Async disposables cannot be used with configureSync().\",\n );\n }\n asyncSinkDisposables.delete(filter as AsyncDisposable);\n }\n if (Symbol.dispose in filter) {\n filterDisposables.add(filter as Disposable);\n sinkDisposables.delete(filter as Disposable);\n }\n }\n\n registerDisposeHook(allowAsync);\n const meta = LoggerImpl.getLogger([\"logtape\", \"meta\"]);\n if (!metaConfigured) {\n meta.sinks.push(getConsoleSink());\n }\n\n meta.info(\n \"LogTape loggers are configured. Note that LogTape itself uses the meta \" +\n \"logger, which has category {metaLoggerCategory}. The meta logger is \" +\n \"used to log internal diagnostics such as sink exceptions. \" +\n \"It's recommended to configure the meta logger with a separate sink \" +\n \"so that you can easily notice if logging itself fails or is \" +\n \"misconfigured. To turn off this message, configure the meta logger \" +\n \"with higher log levels than {dismissLevel}. See also \" +\n \"<https://logtape.org/manual/categories#meta-logger>.\",\n { metaLoggerCategory: [\"logtape\", \"meta\"], dismissLevel: \"info\" },\n );\n}\n\n/**\n * Get the current configuration, if any. Otherwise, `null`.\n * @returns The current configuration, if any. Otherwise, `null`.\n */\nexport function getConfig(): Config<string, string> | null {\n return currentConfig;\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes.\n */\nexport async function reset(): Promise<void> {\n await dispose();\n resetInternal();\n}\n\n/**\n * Reset the configuration. Mostly for testing purposes. Will not clear async\n * sinks, only use with sync sinks. Use {@link reset} if you have async sinks.\n * @since 0.9.0\n */\nexport function resetSync(): void {\n disposeSync();\n resetInternal();\n}\n\nfunction resetInternal(): void {\n const rootLogger = LoggerImpl.getLogger([]);\n rootLogger.resetDescendants();\n delete rootLogger.contextLocalStorage;\n strongRefs.clear();\n currentConfig = null;\n}\n\n/**\n * Dispose of the disposables.\n */\nexport async function dispose(): Promise<void> {\n const errors: unknown[] = [];\n try {\n disposeSyncFilters();\n } catch (error) {\n errors.push(error);\n }\n try {\n await disposeAsyncFilters();\n } catch (error) {\n errors.push(error);\n }\n try {\n disposeSyncSinks();\n } catch (error) {\n errors.push(error);\n }\n try {\n await disposeAsyncSinks();\n } catch (error) {\n errors.push(error);\n }\n throwDisposeErrors(errors);\n}\n\n/**\n * Dispose of the sync disposables. Async disposables will be untouched,\n * use {@link dispose} if you have async sinks.\n * @since 0.9.0\n */\nexport function disposeSync(): void {\n const errors: unknown[] = [];\n try {\n disposeSyncFilters();\n } catch (error) {\n errors.push(error);\n }\n try {\n disposeSyncSinks();\n } catch (error) {\n errors.push(error);\n }\n throwDisposeErrors(errors);\n}\n\nfunction disposeSyncFilters(): void {\n disposeSyncDisposables(filterDisposables);\n}\n\nfunction disposeSyncSinks(): void {\n disposeSyncDisposables(sinkDisposables);\n}\n\nfunction disposeSyncDisposables(disposables: Set<Disposable>): void {\n const errors: unknown[] = [];\n try {\n for (const disposable of disposables) {\n try {\n disposable[Symbol.dispose]();\n } catch (error) {\n errors.push(error);\n } finally {\n disposables.delete(disposable);\n }\n }\n } finally {\n disposables.clear();\n }\n throwDisposeErrors(errors);\n}\n\nasync function disposeAsyncFilters(): Promise<void> {\n await disposeAsyncDisposables(asyncFilterDisposables);\n}\n\nasync function disposeAsyncSinks(): Promise<void> {\n await disposeAsyncDisposables(asyncSinkDisposables);\n}\n\nasync function disposeAsyncDisposables(\n disposables: Set<AsyncDisposable>,\n): Promise<void> {\n const promises: PromiseLike<void>[] = [];\n try {\n for (const disposable of disposables) {\n try {\n promises.push(Promise.resolve(disposable[Symbol.asyncDispose]()));\n } catch (error) {\n promises.push(Promise.reject(error));\n } finally {\n disposables.delete(disposable);\n }\n }\n } finally {\n disposables.clear();\n }\n await settleDisposePromises(promises);\n}\n\nasync function settleDisposePromises(\n promises: readonly PromiseLike<void>[],\n): Promise<void> {\n const results = await Promise.allSettled(promises);\n throwDisposeErrors(\n results\n .filter((result): result is PromiseRejectedResult =>\n result.status === \"rejected\"\n )\n .map((result) => result.reason),\n );\n}\n\nfunction throwDisposeErrors(errors: readonly unknown[]): void {\n if (errors.length < 1) return;\n if (errors.length === 1) throw errors[0];\n throw new AggregateError(\n errors,\n \"Multiple errors occurred while disposing LogTape resources.\",\n );\n}\n\n/**\n * A configuration error.\n */\nexport class ConfigError extends Error {\n /**\n * Constructs a new configuration error.\n * @param message The error message.\n */\n constructor(message: string) {\n super(message);\n this.name = \"ConfigureError\";\n }\n}\n"],"mappings":";;;;;;;;AAmFA,IAAIA,gBAA+C;;;;;;AAOnD,MAAMC,6BAA8B,IAAI;;;;AAKxC,MAAMC,oCAAqC,IAAI;;;;AAK/C,MAAMC,kCAAmC,IAAI;;;;AAK7C,MAAMC,yCAA+C,IAAI;;;;AAKzD,MAAMC,uCAA6C,IAAI;;;;AAKvD,SAAS,mBACPC,KACS;AACT,QAAO,IAAI,SAAS,WAAW,KAC5B,IAAI,SAAS,WAAW,KAAK,IAAI,SAAS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|
27
27
|
else if (level === "trace") return () => true;
|
|
28
28
|
throw new TypeError(`Invalid log level: ${level}.`);
|
|
29
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Returns a stateful filter that rate-limits repeated log records.
|
|
32
|
+
*
|
|
33
|
+
* The default key treats records with the same category, level, and raw
|
|
34
|
+
* message template as identical, ignoring substituted values. If summary
|
|
35
|
+
* logging is enabled, the filter logs summaries as a side effect when
|
|
36
|
+
* suppression ends, when a suppressed key is evicted, or when the filter is
|
|
37
|
+
* disposed.
|
|
38
|
+
*
|
|
39
|
+
* @param options Throttling options.
|
|
40
|
+
* @returns A throttling filter.
|
|
41
|
+
* @since 2.1.0
|
|
42
|
+
*/
|
|
43
|
+
function getThrottlingFilter(options) {
|
|
44
|
+
validatePositiveInteger("limit", options.limit);
|
|
45
|
+
validatePositiveNumber("windowMs", options.windowMs);
|
|
46
|
+
if (options.maxKeys != null) validatePositiveInteger("maxKeys", options.maxKeys);
|
|
47
|
+
const limit = options.limit;
|
|
48
|
+
const windowMs = options.windowMs;
|
|
49
|
+
const mode = options.mode ?? "fixed";
|
|
50
|
+
const timeSource = options.timeSource ?? "clock";
|
|
51
|
+
const clock = options.clock ?? Date.now;
|
|
52
|
+
const getKey = options.key ?? getDefaultThrottlingKey;
|
|
53
|
+
const maxKeys = options.maxKeys === void 0 ? 1e3 : options.maxKeys;
|
|
54
|
+
const buckets = /* @__PURE__ */ new Map();
|
|
55
|
+
const summaryRecord = Symbol.for("LogTape.throttlingSummaryRecord");
|
|
56
|
+
let emittingSummary = false;
|
|
57
|
+
if (mode !== "fixed" && mode !== "sliding") throw new TypeError(`Invalid throttling mode: ${String(mode)}.`);
|
|
58
|
+
if (timeSource !== "clock" && timeSource !== "record") throw new TypeError(`Invalid throttling timeSource: ${String(timeSource)}.`);
|
|
59
|
+
const filter = (record) => {
|
|
60
|
+
if (emittingSummary && isSummaryRecord(record)) return true;
|
|
61
|
+
const key = getKey(record);
|
|
62
|
+
let bucket = buckets.get(key);
|
|
63
|
+
const observedTime = timeSource === "record" ? record.timestamp : clock();
|
|
64
|
+
const now = bucket == null || timeSource === "record" ? observedTime : Math.max(observedTime, bucket.lastTime);
|
|
65
|
+
if (bucket == null) {
|
|
66
|
+
evictKeysIfNeeded(now);
|
|
67
|
+
bucket = {
|
|
68
|
+
windowStart: now,
|
|
69
|
+
summaryStartTime: now,
|
|
70
|
+
acceptedAt: [],
|
|
71
|
+
allowed: 0,
|
|
72
|
+
suppressed: 0,
|
|
73
|
+
firstRecord: record,
|
|
74
|
+
lastRecord: record,
|
|
75
|
+
lastTime: now
|
|
76
|
+
};
|
|
77
|
+
buckets.set(key, bucket);
|
|
78
|
+
} else if (maxKeys != null) {
|
|
79
|
+
buckets.delete(key);
|
|
80
|
+
buckets.set(key, bucket);
|
|
81
|
+
}
|
|
82
|
+
if (mode === "fixed") return filterFixedWindow(key, bucket, record, now);
|
|
83
|
+
return filterSlidingWindow(key, bucket, record, now);
|
|
84
|
+
};
|
|
85
|
+
filter[Symbol.dispose] = () => {
|
|
86
|
+
for (const [key, bucket] of buckets) {
|
|
87
|
+
const endTime = timeSource === "record" ? bucket.lastTime : Math.max(clock(), bucket.lastTime);
|
|
88
|
+
emitSummary(key, bucket, "dispose", endTime);
|
|
89
|
+
}
|
|
90
|
+
buckets.clear();
|
|
91
|
+
};
|
|
92
|
+
return filter;
|
|
93
|
+
function filterFixedWindow(key, bucket, record, now) {
|
|
94
|
+
if (now - bucket.windowStart >= windowMs) {
|
|
95
|
+
emitSummary(key, bucket, "window", now);
|
|
96
|
+
bucket.windowStart = now;
|
|
97
|
+
bucket.summaryStartTime = now;
|
|
98
|
+
bucket.acceptedAt.length = 0;
|
|
99
|
+
bucket.allowed = 0;
|
|
100
|
+
bucket.suppressed = 0;
|
|
101
|
+
bucket.firstRecord = record;
|
|
102
|
+
}
|
|
103
|
+
const allowed = bucket.allowed < limit;
|
|
104
|
+
if (allowed) bucket.allowed++;
|
|
105
|
+
else bucket.suppressed++;
|
|
106
|
+
bucket.lastRecord = record;
|
|
107
|
+
bucket.lastTime = now;
|
|
108
|
+
return allowed;
|
|
109
|
+
}
|
|
110
|
+
function filterSlidingWindow(key, bucket, record, now) {
|
|
111
|
+
pruneExpiredAcceptedAt(bucket, now);
|
|
112
|
+
const allowed = bucket.acceptedAt.length < limit;
|
|
113
|
+
if (allowed) {
|
|
114
|
+
if (bucket.suppressed > 0) {
|
|
115
|
+
emitSummary(key, bucket, "window", now);
|
|
116
|
+
bucket.allowed = 0;
|
|
117
|
+
bucket.suppressed = 0;
|
|
118
|
+
bucket.summaryStartTime = now;
|
|
119
|
+
bucket.firstRecord = record;
|
|
120
|
+
}
|
|
121
|
+
pushAcceptedAt(bucket, now);
|
|
122
|
+
bucket.allowed++;
|
|
123
|
+
} else bucket.suppressed++;
|
|
124
|
+
bucket.lastRecord = record;
|
|
125
|
+
bucket.lastTime = now;
|
|
126
|
+
return allowed;
|
|
127
|
+
}
|
|
128
|
+
function pruneExpiredAcceptedAt(bucket, now) {
|
|
129
|
+
let acceptedAtCount = 0;
|
|
130
|
+
for (let i = 0; i < bucket.acceptedAt.length; i++) {
|
|
131
|
+
const acceptedAt = bucket.acceptedAt[i];
|
|
132
|
+
if (acceptedAt <= now && now - acceptedAt < windowMs) {
|
|
133
|
+
bucket.acceptedAt[acceptedAtCount] = acceptedAt;
|
|
134
|
+
acceptedAtCount++;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
bucket.acceptedAt.length = acceptedAtCount;
|
|
138
|
+
}
|
|
139
|
+
function pushAcceptedAt(bucket, acceptedAt) {
|
|
140
|
+
bucket.acceptedAt.push(acceptedAt);
|
|
141
|
+
}
|
|
142
|
+
function evictKeysIfNeeded(now) {
|
|
143
|
+
if (maxKeys == null) return;
|
|
144
|
+
while (buckets.size >= maxKeys) {
|
|
145
|
+
const keyToEvict = buckets.keys().next().value;
|
|
146
|
+
if (keyToEvict === void 0) return;
|
|
147
|
+
const bucket = buckets.get(keyToEvict);
|
|
148
|
+
if (bucket != null) {
|
|
149
|
+
const endTime = timeSource === "record" ? bucket.lastTime : Math.max(now, bucket.lastTime);
|
|
150
|
+
emitSummary(keyToEvict, bucket, "eviction", endTime);
|
|
151
|
+
buckets.delete(keyToEvict);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function emitSummary(key, bucket, reason, endTime) {
|
|
156
|
+
const summaryOptions = options.summary;
|
|
157
|
+
if (summaryOptions == null || bucket.suppressed < 1) return;
|
|
158
|
+
const summary = {
|
|
159
|
+
key,
|
|
160
|
+
suppressed: bucket.suppressed,
|
|
161
|
+
allowed: bucket.allowed,
|
|
162
|
+
reason,
|
|
163
|
+
startTime: bucket.summaryStartTime,
|
|
164
|
+
endTime,
|
|
165
|
+
firstRecord: bucket.firstRecord,
|
|
166
|
+
lastRecord: bucket.lastRecord
|
|
167
|
+
};
|
|
168
|
+
const level = typeof summaryOptions.level === "function" ? summaryOptions.level(summary) : summaryOptions.level ?? "warning";
|
|
169
|
+
const message = typeof summaryOptions.message === "function" ? summaryOptions.message(summary) : summaryOptions.message ?? "Last log message was suppressed {suppressed} times.";
|
|
170
|
+
const log = summaryOptions.logger[level];
|
|
171
|
+
if (typeof log !== "function") return;
|
|
172
|
+
const properties = {
|
|
173
|
+
key: summary.key,
|
|
174
|
+
suppressed: summary.suppressed,
|
|
175
|
+
allowed: summary.allowed,
|
|
176
|
+
reason: summary.reason,
|
|
177
|
+
startTime: summary.startTime,
|
|
178
|
+
endTime: summary.endTime,
|
|
179
|
+
firstRecord: summary.firstRecord,
|
|
180
|
+
lastRecord: summary.lastRecord,
|
|
181
|
+
[summaryRecord]: true
|
|
182
|
+
};
|
|
183
|
+
emittingSummary = true;
|
|
184
|
+
try {
|
|
185
|
+
log.call(summaryOptions.logger, message, properties);
|
|
186
|
+
} finally {
|
|
187
|
+
emittingSummary = false;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
function isSummaryRecord(record) {
|
|
191
|
+
const properties = record.properties;
|
|
192
|
+
return properties != null && typeof properties === "object" && properties[summaryRecord] === true;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function validatePositiveInteger(name, value) {
|
|
196
|
+
if (!Number.isInteger(value) || value < 1) throw new RangeError(`${name} must be a positive integer.`);
|
|
197
|
+
}
|
|
198
|
+
function validatePositiveNumber(name, value) {
|
|
199
|
+
if (!Number.isFinite(value) || value <= 0) throw new RangeError(`${name} must be a positive number.`);
|
|
200
|
+
}
|
|
201
|
+
function getDefaultThrottlingKey(record) {
|
|
202
|
+
const categoryKey = encodeKeyParts(record.category);
|
|
203
|
+
const rawMessage = getRawMessageTemplate(record.rawMessage);
|
|
204
|
+
const rawMessageKey = typeof rawMessage === "string" ? `s${encodeKeyPart(rawMessage)}` : `t${encodeKeyParts(rawMessage)}`;
|
|
205
|
+
return `c${categoryKey}l${encodeKeyPart(record.level)}r${rawMessageKey}`;
|
|
206
|
+
}
|
|
207
|
+
function getRawMessageTemplate(rawMessage) {
|
|
208
|
+
if (typeof rawMessage === "string") return rawMessage;
|
|
209
|
+
for (const part of rawMessage) if (typeof part !== "string") return rawMessage.raw;
|
|
210
|
+
return rawMessage;
|
|
211
|
+
}
|
|
212
|
+
function encodeKeyParts(parts) {
|
|
213
|
+
let key = `${parts.length}:`;
|
|
214
|
+
for (const part of parts) key += encodeKeyPart(part);
|
|
215
|
+
return key;
|
|
216
|
+
}
|
|
217
|
+
function encodeKeyPart(part) {
|
|
218
|
+
return `${part.length}:${part}`;
|
|
219
|
+
}
|
|
30
220
|
|
|
31
221
|
//#endregion
|
|
32
222
|
exports.getLevelFilter = getLevelFilter;
|
|
223
|
+
exports.getThrottlingFilter = getThrottlingFilter;
|
|
33
224
|
exports.toFilter = toFilter;
|