@logtape/logtape 1.4.0-dev.468 → 2.0.0-dev.470
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/formatter.d.cts +2 -2
- package/dist/formatter.d.ts +2 -2
- package/dist/formatter.js.map +1 -1
- package/dist/logger.cjs +68 -10
- package/dist/logger.d.cts +62 -18
- package/dist/logger.d.cts.map +1 -1
- package/dist/logger.d.ts +62 -18
- package/dist/logger.d.ts.map +1 -1
- package/dist/logger.js +66 -10
- package/dist/logger.js.map +1 -1
- package/dist/mod.cjs +2 -0
- package/dist/mod.d.cts +2 -2
- package/dist/mod.d.ts +2 -2
- package/dist/mod.js +2 -2
- package/dist/sink.d.cts +1 -1
- package/dist/sink.d.ts +1 -1
- package/dist/sink.js.map +1 -1
- package/package.json +1 -1
package/dist/logger.d.ts
CHANGED
|
@@ -3,6 +3,50 @@ import { LogRecord } from "./record.js";
|
|
|
3
3
|
|
|
4
4
|
//#region src/logger.d.ts
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Symbol to identify lazy values.
|
|
8
|
+
*/
|
|
9
|
+
declare const lazySymbol: unique symbol;
|
|
10
|
+
/**
|
|
11
|
+
* A lazy value that is evaluated at logging time.
|
|
12
|
+
*
|
|
13
|
+
* @typeParam T The type of the value.
|
|
14
|
+
* @since 2.0.0
|
|
15
|
+
*/
|
|
16
|
+
interface Lazy<T> {
|
|
17
|
+
readonly [lazySymbol]: true;
|
|
18
|
+
readonly getter: () => T;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Checks if a value is a lazy value.
|
|
22
|
+
*
|
|
23
|
+
* @param value The value to check.
|
|
24
|
+
* @returns `true` if the value is a lazy value, `false` otherwise.
|
|
25
|
+
* @since 2.0.0
|
|
26
|
+
*/
|
|
27
|
+
declare function isLazy(value: unknown): value is Lazy<unknown>;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a lazy value that is evaluated at logging time.
|
|
30
|
+
*
|
|
31
|
+
* This is useful for logging contextual properties that may change over time,
|
|
32
|
+
* such as the current user or request context.
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* let currentUser: string | null = null;
|
|
37
|
+
* const logger = getLogger("app").with({ user: lazy(() => currentUser) });
|
|
38
|
+
*
|
|
39
|
+
* logger.info("User action"); // logs with user: null
|
|
40
|
+
* currentUser = "alice";
|
|
41
|
+
* logger.info("User action"); // logs with user: "alice"
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @typeParam T The type of the value.
|
|
45
|
+
* @param getter A function that returns the value.
|
|
46
|
+
* @returns A lazy value.
|
|
47
|
+
* @since 2.0.0
|
|
48
|
+
*/
|
|
49
|
+
declare function lazy<T>(getter: () => T): Lazy<T>;
|
|
6
50
|
/**
|
|
7
51
|
* A logger interface. It provides methods to log messages at different
|
|
8
52
|
* severity levels.
|
|
@@ -128,7 +172,7 @@ interface Logger {
|
|
|
128
172
|
* `{value}`).
|
|
129
173
|
* @param properties An async callback that returns the properties.
|
|
130
174
|
* @returns A promise that resolves when the log is written.
|
|
131
|
-
* @since
|
|
175
|
+
* @since 2.0.0
|
|
132
176
|
*/
|
|
133
177
|
trace(message: string, properties: () => Promise<Record<string, unknown>>): Promise<void>;
|
|
134
178
|
/**
|
|
@@ -227,7 +271,7 @@ interface Logger {
|
|
|
227
271
|
* `{value}`).
|
|
228
272
|
* @param properties An async callback that returns the properties.
|
|
229
273
|
* @returns A promise that resolves when the log is written.
|
|
230
|
-
* @since
|
|
274
|
+
* @since 2.0.0
|
|
231
275
|
*/
|
|
232
276
|
debug(message: string, properties: () => Promise<Record<string, unknown>>): Promise<void>;
|
|
233
277
|
/**
|
|
@@ -325,7 +369,7 @@ interface Logger {
|
|
|
325
369
|
* `{value}`).
|
|
326
370
|
* @param properties An async callback that returns the properties.
|
|
327
371
|
* @returns A promise that resolves when the log is written.
|
|
328
|
-
* @since
|
|
372
|
+
* @since 2.0.0
|
|
329
373
|
*/
|
|
330
374
|
info(message: string, properties: () => Promise<Record<string, unknown>>): Promise<void>;
|
|
331
375
|
/**
|
|
@@ -386,7 +430,7 @@ interface Logger {
|
|
|
386
430
|
* in the message template instead.
|
|
387
431
|
*
|
|
388
432
|
* @param error The error to log.
|
|
389
|
-
* @since
|
|
433
|
+
* @since 2.0.0
|
|
390
434
|
*/
|
|
391
435
|
warn(error: Error): void;
|
|
392
436
|
/**
|
|
@@ -398,7 +442,7 @@ interface Logger {
|
|
|
398
442
|
*
|
|
399
443
|
* @param message The message.
|
|
400
444
|
* @param error The error to log.
|
|
401
|
-
* @since
|
|
445
|
+
* @since 2.0.0
|
|
402
446
|
*/
|
|
403
447
|
warn(message: string, error: Error): void;
|
|
404
448
|
/**
|
|
@@ -454,7 +498,7 @@ interface Logger {
|
|
|
454
498
|
* `{value}`).
|
|
455
499
|
* @param properties An async callback that returns the properties.
|
|
456
500
|
* @returns A promise that resolves when the log is written.
|
|
457
|
-
* @since
|
|
501
|
+
* @since 2.0.0
|
|
458
502
|
*/
|
|
459
503
|
warn(message: string, properties: () => Promise<Record<string, unknown>>): Promise<void>;
|
|
460
504
|
/**
|
|
@@ -515,7 +559,7 @@ interface Logger {
|
|
|
515
559
|
* in the message template instead.
|
|
516
560
|
*
|
|
517
561
|
* @param error The error to log.
|
|
518
|
-
* @since
|
|
562
|
+
* @since 2.0.0
|
|
519
563
|
*/
|
|
520
564
|
warning(error: Error): void;
|
|
521
565
|
/**
|
|
@@ -527,7 +571,7 @@ interface Logger {
|
|
|
527
571
|
*
|
|
528
572
|
* @param message The message.
|
|
529
573
|
* @param error The error to log.
|
|
530
|
-
* @since
|
|
574
|
+
* @since 2.0.0
|
|
531
575
|
*/
|
|
532
576
|
warning(message: string, error: Error): void;
|
|
533
577
|
/**
|
|
@@ -585,7 +629,7 @@ interface Logger {
|
|
|
585
629
|
* `{value}`).
|
|
586
630
|
* @param properties An async callback that returns the properties.
|
|
587
631
|
* @returns A promise that resolves when the log is written.
|
|
588
|
-
* @since
|
|
632
|
+
* @since 2.0.0
|
|
589
633
|
*/
|
|
590
634
|
warning(message: string, properties: () => Promise<Record<string, unknown>>): Promise<void>;
|
|
591
635
|
/**
|
|
@@ -647,7 +691,7 @@ interface Logger {
|
|
|
647
691
|
* in the message template instead.
|
|
648
692
|
*
|
|
649
693
|
* @param error The error to log.
|
|
650
|
-
* @since
|
|
694
|
+
* @since 2.0.0
|
|
651
695
|
*/
|
|
652
696
|
error(error: Error): void;
|
|
653
697
|
/**
|
|
@@ -659,7 +703,7 @@ interface Logger {
|
|
|
659
703
|
*
|
|
660
704
|
* @param message The message.
|
|
661
705
|
* @param error The error to log.
|
|
662
|
-
* @since
|
|
706
|
+
* @since 2.0.0
|
|
663
707
|
*/
|
|
664
708
|
error(message: string, error: Error): void;
|
|
665
709
|
/**
|
|
@@ -715,7 +759,7 @@ interface Logger {
|
|
|
715
759
|
* `{value}`).
|
|
716
760
|
* @param properties An async callback that returns the properties.
|
|
717
761
|
* @returns A promise that resolves when the log is written.
|
|
718
|
-
* @since
|
|
762
|
+
* @since 2.0.0
|
|
719
763
|
*/
|
|
720
764
|
error(message: string, properties: () => Promise<Record<string, unknown>>): Promise<void>;
|
|
721
765
|
/**
|
|
@@ -776,7 +820,7 @@ interface Logger {
|
|
|
776
820
|
* in the message template instead.
|
|
777
821
|
*
|
|
778
822
|
* @param error The error to log.
|
|
779
|
-
* @since
|
|
823
|
+
* @since 2.0.0
|
|
780
824
|
*/
|
|
781
825
|
fatal(error: Error): void;
|
|
782
826
|
/**
|
|
@@ -788,7 +832,7 @@ interface Logger {
|
|
|
788
832
|
*
|
|
789
833
|
* @param message The message.
|
|
790
834
|
* @param error The error to log.
|
|
791
|
-
* @since
|
|
835
|
+
* @since 2.0.0
|
|
792
836
|
*/
|
|
793
837
|
fatal(message: string, error: Error): void;
|
|
794
838
|
/**
|
|
@@ -844,7 +888,7 @@ interface Logger {
|
|
|
844
888
|
* `{value}`).
|
|
845
889
|
* @param properties An async callback that returns the properties.
|
|
846
890
|
* @returns A promise that resolves when the log is written.
|
|
847
|
-
* @since
|
|
891
|
+
* @since 2.0.0
|
|
848
892
|
*/
|
|
849
893
|
fatal(message: string, properties: () => Promise<Record<string, unknown>>): Promise<void>;
|
|
850
894
|
/**
|
|
@@ -938,7 +982,7 @@ interface Logger {
|
|
|
938
982
|
* @param level The log level to check.
|
|
939
983
|
* @returns `true` if a message of the given level would be logged,
|
|
940
984
|
* `false` otherwise.
|
|
941
|
-
* @since
|
|
985
|
+
* @since 2.0.0
|
|
942
986
|
*/
|
|
943
987
|
isEnabledFor(level: LogLevel): boolean;
|
|
944
988
|
}
|
|
@@ -985,7 +1029,7 @@ interface LogMethod {
|
|
|
985
1029
|
* `{value}`).
|
|
986
1030
|
* @param properties An async callback that returns the properties.
|
|
987
1031
|
* @returns A promise that resolves when the log is written.
|
|
988
|
-
* @since
|
|
1032
|
+
* @since 2.0.0
|
|
989
1033
|
*/
|
|
990
1034
|
(message: string, properties: () => Promise<Record<string, unknown>>): Promise<void>;
|
|
991
1035
|
/**
|
|
@@ -1019,5 +1063,5 @@ declare function getLogger(category?: string | readonly string[]): Logger;
|
|
|
1019
1063
|
* instead. This class is exported for testing purposes.
|
|
1020
1064
|
*/
|
|
1021
1065
|
//#endregion
|
|
1022
|
-
export { LogMethod, Logger, getLogger };
|
|
1066
|
+
export { Lazy, LogMethod, Logger, getLogger, isLazy, lazy };
|
|
1023
1067
|
//# sourceMappingURL=logger.d.ts.map
|
package/dist/logger.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"logger.d.ts","names":[],"sources":["../src/logger.ts"],"sourcesContent":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"logger.d.ts","names":[],"sources":["../src/logger.ts"],"sourcesContent":[],"mappings":";;;;;AAqBA;;;cARM,UAUmB,EAAA,OAAA,MAAA;AAAC;AAU1B;AA4BA;;;;AAA0C,UAxCzB,IAwCyB,CAAA,C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OF;;;;;;;;;;;;;;iBAeG,SAAA,yCAAsD"}
|
package/dist/logger.js
CHANGED
|
@@ -3,6 +3,61 @@ import { compareLogLevel } from "./level.js";
|
|
|
3
3
|
|
|
4
4
|
//#region src/logger.ts
|
|
5
5
|
/**
|
|
6
|
+
* Symbol to identify lazy values.
|
|
7
|
+
*/
|
|
8
|
+
const lazySymbol = Symbol.for("logtape.lazy");
|
|
9
|
+
/**
|
|
10
|
+
* Checks if a value is a lazy value.
|
|
11
|
+
*
|
|
12
|
+
* @param value The value to check.
|
|
13
|
+
* @returns `true` if the value is a lazy value, `false` otherwise.
|
|
14
|
+
* @since 2.0.0
|
|
15
|
+
*/
|
|
16
|
+
function isLazy(value) {
|
|
17
|
+
return value != null && typeof value === "object" && lazySymbol in value && value[lazySymbol] === true;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Creates a lazy value that is evaluated at logging time.
|
|
21
|
+
*
|
|
22
|
+
* This is useful for logging contextual properties that may change over time,
|
|
23
|
+
* such as the current user or request context.
|
|
24
|
+
*
|
|
25
|
+
* @example
|
|
26
|
+
* ```typescript
|
|
27
|
+
* let currentUser: string | null = null;
|
|
28
|
+
* const logger = getLogger("app").with({ user: lazy(() => currentUser) });
|
|
29
|
+
*
|
|
30
|
+
* logger.info("User action"); // logs with user: null
|
|
31
|
+
* currentUser = "alice";
|
|
32
|
+
* logger.info("User action"); // logs with user: "alice"
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @typeParam T The type of the value.
|
|
36
|
+
* @param getter A function that returns the value.
|
|
37
|
+
* @returns A lazy value.
|
|
38
|
+
* @since 2.0.0
|
|
39
|
+
*/
|
|
40
|
+
function lazy(getter) {
|
|
41
|
+
return {
|
|
42
|
+
[lazySymbol]: true,
|
|
43
|
+
getter
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Resolves lazy values in a properties object.
|
|
48
|
+
*
|
|
49
|
+
* @param properties The properties object with potential lazy values.
|
|
50
|
+
* @returns A new object with all lazy values resolved.
|
|
51
|
+
*/
|
|
52
|
+
function resolveProperties(properties) {
|
|
53
|
+
const resolved = {};
|
|
54
|
+
for (const key in properties) {
|
|
55
|
+
const value = properties[key];
|
|
56
|
+
resolved[key] = isLazy(value) ? value.getter() : value;
|
|
57
|
+
}
|
|
58
|
+
return resolved;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
6
61
|
* Get a logger with the given category.
|
|
7
62
|
*
|
|
8
63
|
* ```typescript
|
|
@@ -417,27 +472,28 @@ var LoggerCtx = class LoggerCtx {
|
|
|
417
472
|
});
|
|
418
473
|
}
|
|
419
474
|
log(level, message, properties, bypassSinks) {
|
|
420
|
-
|
|
421
|
-
|
|
475
|
+
const contextProps = this.properties;
|
|
476
|
+
this.logger.log(level, message, typeof properties === "function" ? () => resolveProperties({
|
|
477
|
+
...contextProps,
|
|
422
478
|
...properties()
|
|
423
|
-
}) : {
|
|
424
|
-
...
|
|
479
|
+
}) : () => resolveProperties({
|
|
480
|
+
...contextProps,
|
|
425
481
|
...properties
|
|
426
|
-
}, bypassSinks);
|
|
482
|
+
}), bypassSinks);
|
|
427
483
|
}
|
|
428
484
|
logLazily(level, callback) {
|
|
429
|
-
this.logger.logLazily(level, callback, this.properties);
|
|
485
|
+
this.logger.logLazily(level, callback, resolveProperties(this.properties));
|
|
430
486
|
}
|
|
431
487
|
logTemplate(level, messageTemplate, values) {
|
|
432
|
-
this.logger.logTemplate(level, messageTemplate, values, this.properties);
|
|
488
|
+
this.logger.logTemplate(level, messageTemplate, values, resolveProperties(this.properties));
|
|
433
489
|
}
|
|
434
490
|
emit(record) {
|
|
435
491
|
const recordWithContext = {
|
|
436
492
|
...record,
|
|
437
|
-
properties: {
|
|
493
|
+
properties: resolveProperties({
|
|
438
494
|
...this.properties,
|
|
439
495
|
...record.properties
|
|
440
|
-
}
|
|
496
|
+
})
|
|
441
497
|
};
|
|
442
498
|
this.logger.emit(recordWithContext);
|
|
443
499
|
}
|
|
@@ -908,5 +964,5 @@ function renderMessage(template, values) {
|
|
|
908
964
|
}
|
|
909
965
|
|
|
910
966
|
//#endregion
|
|
911
|
-
export { LoggerImpl, getLogger };
|
|
967
|
+
export { LoggerImpl, getLogger, isLazy, lazy };
|
|
912
968
|
//# sourceMappingURL=logger.js.map
|