@@ -187,6 +187,13 @@ class Logger extends Utility implements LoggerInterface {
187
187
* Flag used to determine if the logger is initialized.
188
188
*/
189
189
#isInitialized = false ;
190
+ /**
191
+ * Map used to hold the list of keys and their type.
192
+ *
193
+ * Because keys of different types can be overwritten, we keep a list of keys that were added and their last
194
+ * type. We then use this map at log preparation time to pick the last one.
195
+ */
196
+ #keys: Map < string , 'temp' | 'persistent' > = new Map ( ) ;
190
197
191
198
/**
192
199
* Log level used by the current instance of Logger.
@@ -255,7 +262,10 @@ class Logger extends Utility implements LoggerInterface {
255
262
* @param {LogAttributes } attributes
256
263
* @returns {void }
257
264
*/
258
- public appendKeys ( attributes ?: LogAttributes ) : void {
265
+ public appendKeys ( attributes : LogAttributes ) : void {
266
+ for ( const attributeKey of Object . keys ( attributes ) ) {
267
+ this . #keys. set ( attributeKey , 'temp' ) ;
268
+ }
259
269
merge ( this . temporaryLogAttributes , attributes ) ;
260
270
}
261
271
@@ -265,6 +275,9 @@ class Logger extends Utility implements LoggerInterface {
265
275
* @param attributes - The attributes to add to all log items.
266
276
*/
267
277
public appendPersistentKeys ( attributes : LogAttributes ) : void {
278
+ for ( const attributeKey of Object . keys ( attributes ) ) {
279
+ this . #keys. set ( attributeKey , 'persistent' ) ;
280
+ }
268
281
merge ( this . persistentLogAttributes , attributes ) ;
269
282
}
270
283
@@ -508,8 +521,12 @@ class Logger extends Utility implements LoggerInterface {
508
521
*/
509
522
public removeKeys ( keys : string [ ] ) : void {
510
523
for ( const key of keys ) {
511
- if ( this . temporaryLogAttributes && key in this . temporaryLogAttributes ) {
512
- delete this . temporaryLogAttributes [ key ] ;
524
+ this . temporaryLogAttributes [ key ] = undefined ;
525
+
526
+ if ( this . persistentLogAttributes [ key ] ) {
527
+ this . #keys. set ( key , 'persistent' ) ;
528
+ } else {
529
+ this . #keys. delete ( key ) ;
513
530
}
514
531
}
515
532
}
@@ -522,18 +539,27 @@ class Logger extends Utility implements LoggerInterface {
522
539
*/
523
540
public removePersistentLogAttributes ( keys : string [ ] ) : void {
524
541
for ( const key of keys ) {
525
- if ( this . persistentLogAttributes && key in this . persistentLogAttributes ) {
526
- delete this . persistentLogAttributes [ key ] ;
542
+ this . persistentLogAttributes [ key ] = undefined ;
543
+
544
+ if ( this . temporaryLogAttributes [ key ] ) {
545
+ this . #keys. set ( key , 'temp' ) ;
546
+ } else {
547
+ this . #keys. delete ( key ) ;
527
548
}
528
549
}
529
550
}
530
551
531
552
/**
532
553
* It resets the state, by removing all temporary log attributes added with `appendKeys()` method.
533
- *
534
- * @returns {void }
535
554
*/
536
555
public resetState ( ) : void {
556
+ for ( const key of Object . keys ( this . temporaryLogAttributes ) ) {
557
+ if ( this . persistentLogAttributes [ key ] ) {
558
+ this . #keys. set ( key , 'persistent' ) ;
559
+ } else {
560
+ this . #keys. delete ( key ) ;
561
+ }
562
+ }
537
563
this . temporaryLogAttributes = { } ;
538
564
}
539
565
@@ -688,13 +714,18 @@ class Logger extends Utility implements LoggerInterface {
688
714
...this . getPowertoolsLogData ( ) ,
689
715
} ;
690
716
691
- // gradually merge additional attributes starting from customer-provided persistent & temporary attributes
692
- let additionalLogAttributes = {
693
- ...this . temporaryLogAttributes ,
694
- ...this . getPersistentLogAttributes ( ) ,
695
- } ;
717
+ const additionalAttributes : LogAttributes = { } ;
718
+ // gradually add additional attributes picking only the last added for each key
719
+ for ( const [ key , type ] of this . #keys) {
720
+ if ( type === 'persistent' ) {
721
+ additionalAttributes [ key ] = this . persistentLogAttributes [ key ] ;
722
+ } else {
723
+ additionalAttributes [ key ] = this . temporaryLogAttributes [ key ] ;
724
+ }
725
+ }
726
+
696
727
// if the main input is not a string, then it's an object with additional attributes, so we merge it
697
- additionalLogAttributes = merge ( additionalLogAttributes , otherInput ) ;
728
+ merge ( additionalAttributes , otherInput ) ;
698
729
// then we merge the extra input attributes (if any)
699
730
for ( const item of extraInput ) {
700
731
const attributes : LogAttributes =
@@ -704,12 +735,12 @@ class Logger extends Utility implements LoggerInterface {
704
735
? { extra : item }
705
736
: item ;
706
737
707
- additionalLogAttributes = merge ( additionalLogAttributes , attributes ) ;
738
+ merge ( additionalAttributes , attributes ) ;
708
739
}
709
740
710
741
return this . getLogFormatter ( ) . formatAttributes (
711
742
unformattedBaseAttributes ,
712
- additionalLogAttributes
743
+ additionalAttributes
713
744
) ;
714
745
}
715
746
@@ -1106,7 +1137,7 @@ class Logger extends Utility implements LoggerInterface {
1106
1137
this . getEnvVarsService ( ) . getServiceName ( ) ||
1107
1138
this . getDefaultServiceName ( ) ,
1108
1139
} ) ;
1109
- this . addPersistentLogAttributes ( persistentLogAttributes ) ;
1140
+ this . appendPersistentKeys ( persistentLogAttributes ) ;
1110
1141
}
1111
1142
}
1112
1143
0 commit comments