@@ -172,6 +172,10 @@ class Logger extends Utility implements LoggerInterface {
172
172
* Standard attributes managed by Powertools that will be logged in all log items.
173
173
*/
174
174
private powertoolsLogData : PowertoolsLogData = < PowertoolsLogData > { } ;
175
+ /**
176
+ * Temporary log attributes that can be appended with `appendKeys()` method.
177
+ */
178
+ private temporaryLogAttributes : LogAttributes = { } ;
175
179
/**
176
180
* Buffer used to store logs until the logger is initialized.
177
181
*
@@ -183,6 +187,13 @@ class Logger extends Utility implements LoggerInterface {
183
187
* Flag used to determine if the logger is initialized.
184
188
*/
185
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 ( ) ;
186
197
187
198
/**
188
199
* Log level used by the current instance of Logger.
@@ -234,23 +245,40 @@ class Logger extends Utility implements LoggerInterface {
234
245
}
235
246
236
247
/**
237
- * It adds the given attributes (key-value pairs) to all log items generated by this Logger instance.
248
+ * It adds the given persistent attributes (key-value pairs) to all log items generated by this Logger instance.
249
+ *
250
+ * @deprecated This method is deprecated and will be removed in the future major versions, please use {@link appendPersistentKeys()} instead.
238
251
*
239
252
* @param {LogAttributes } attributes
240
253
* @returns {void }
241
254
*/
242
- public addPersistentLogAttributes ( attributes ? : LogAttributes ) : void {
243
- merge ( this . persistentLogAttributes , attributes ) ;
255
+ public addPersistentLogAttributes ( attributes : LogAttributes ) : void {
256
+ this . appendPersistentKeys ( attributes ) ;
244
257
}
245
258
246
259
/**
247
- * Alias for addPersistentLogAttributes .
260
+ * It adds the given temporary attributes (key-value pairs) to all log items generated by this Logger instance .
248
261
*
249
262
* @param {LogAttributes } attributes
250
263
* @returns {void }
251
264
*/
252
- public appendKeys ( attributes ?: LogAttributes ) : void {
253
- this . addPersistentLogAttributes ( attributes ) ;
265
+ public appendKeys ( attributes : LogAttributes ) : void {
266
+ for ( const attributeKey of Object . keys ( attributes ) ) {
267
+ this . #keys. set ( attributeKey , 'temp' ) ;
268
+ }
269
+ merge ( this . temporaryLogAttributes , attributes ) ;
270
+ }
271
+
272
+ /**
273
+ * It adds the given persistent attributes (key-value pairs) to all log items generated by this Logger instance.
274
+ *
275
+ * @param attributes - The attributes to add to all log items.
276
+ */
277
+ public appendPersistentKeys ( attributes : LogAttributes ) : void {
278
+ for ( const attributeKey of Object . keys ( attributes ) ) {
279
+ this . #keys. set ( attributeKey , 'persistent' ) ;
280
+ }
281
+ merge ( this . persistentLogAttributes , attributes ) ;
254
282
}
255
283
256
284
/**
@@ -274,6 +302,7 @@ class Logger extends Utility implements LoggerInterface {
274
302
customConfigService : this . getCustomConfigService ( ) ,
275
303
environment : this . powertoolsLogData . environment ,
276
304
persistentLogAttributes : this . persistentLogAttributes ,
305
+ temporaryLogAttributes : this . temporaryLogAttributes ,
277
306
} ,
278
307
options
279
308
)
@@ -417,13 +446,6 @@ class Logger extends Utility implements LoggerInterface {
417
446
context ,
418
447
callback
419
448
) {
420
- let initialPersistentAttributes = { } ;
421
- if ( options && options . clearState === true ) {
422
- initialPersistentAttributes = {
423
- ...loggerRef . getPersistentLogAttributes ( ) ,
424
- } ;
425
- }
426
-
427
449
Logger . injectLambdaContextBefore ( loggerRef , event , context , options ) ;
428
450
429
451
let result : unknown ;
@@ -432,25 +454,25 @@ class Logger extends Utility implements LoggerInterface {
432
454
} catch ( error ) {
433
455
throw error ;
434
456
} finally {
435
- Logger . injectLambdaContextAfterOrOnError (
436
- loggerRef ,
437
- initialPersistentAttributes ,
438
- options
439
- ) ;
457
+ if ( options ?. clearState ) loggerRef . resetState ( ) ;
440
458
}
441
459
442
460
return result ;
443
461
} ;
444
462
} ;
445
463
}
446
464
465
+ /**
466
+ * @deprecated This method is deprecated and will be removed in the future major versions. Use {@link resetState()} instead.
467
+ */
468
+ /* istanbul ignore next */
447
469
public static injectLambdaContextAfterOrOnError (
448
470
logger : Logger ,
449
- initialPersistentAttributes : LogAttributes ,
471
+ _persistentAttributes : LogAttributes ,
450
472
options ?: InjectLambdaContextOptions
451
473
) : void {
452
474
if ( options && options . clearState === true ) {
453
- logger . setPersistentLogAttributes ( initialPersistentAttributes ) ;
475
+ logger . resetState ( ) ;
454
476
}
455
477
}
456
478
@@ -493,27 +515,53 @@ class Logger extends Utility implements LoggerInterface {
493
515
}
494
516
495
517
/**
496
- * Alias for removePersistentLogAttributes .
518
+ * It removes temporary attributes based on provided keys to all log items generated by this Logger instance .
497
519
*
498
520
* @param {string[] } keys
499
521
* @returns {void }
500
522
*/
501
523
public removeKeys ( keys : string [ ] ) : void {
502
- this . removePersistentLogAttributes ( keys ) ;
524
+ for ( const key of keys ) {
525
+ this . temporaryLogAttributes [ key ] = undefined ;
526
+
527
+ if ( this . persistentLogAttributes [ key ] ) {
528
+ this . #keys. set ( key , 'persistent' ) ;
529
+ } else {
530
+ this . #keys. delete ( key ) ;
531
+ }
532
+ }
503
533
}
504
534
505
535
/**
506
- * It removes attributes based on provided keys to all log items generated by this Logger instance.
536
+ * It removes persistent attributes based on provided keys to all log items generated by this Logger instance.
507
537
*
508
538
* @param {string[] } keys
509
539
* @returns {void }
510
540
*/
511
541
public removePersistentLogAttributes ( keys : string [ ] ) : void {
512
542
for ( const key of keys ) {
513
- if ( this . persistentLogAttributes && key in this . persistentLogAttributes ) {
514
- delete this . persistentLogAttributes [ key ] ;
543
+ this . persistentLogAttributes [ key ] = undefined ;
544
+
545
+ if ( this . temporaryLogAttributes [ key ] ) {
546
+ this . #keys. set ( key , 'temp' ) ;
547
+ } else {
548
+ this . #keys. delete ( key ) ;
549
+ }
550
+ }
551
+ }
552
+
553
+ /**
554
+ * It resets the state, by removing all temporary log attributes added with `appendKeys()` method.
555
+ */
556
+ public resetState ( ) : void {
557
+ for ( const key of Object . keys ( this . temporaryLogAttributes ) ) {
558
+ if ( this . persistentLogAttributes [ key ] ) {
559
+ this . #keys. set ( key , 'persistent' ) ;
560
+ } else {
561
+ this . #keys. delete ( key ) ;
515
562
}
516
563
}
564
+ this . temporaryLogAttributes = { } ;
517
565
}
518
566
519
567
/**
@@ -537,6 +585,8 @@ class Logger extends Utility implements LoggerInterface {
537
585
* It sets the given attributes (key-value pairs) to all log items generated by this Logger instance.
538
586
* Note: this replaces the pre-existing value.
539
587
*
588
+ * @deprecated This method is deprecated and will be removed in the future major versions, please use {@link appendPersistentKeys()} instead.
589
+ *
540
590
* @param {LogAttributes } attributes
541
591
* @returns {void }
542
592
*/
@@ -665,10 +715,18 @@ class Logger extends Utility implements LoggerInterface {
665
715
...this . getPowertoolsLogData ( ) ,
666
716
} ;
667
717
668
- // gradually merge additional attributes starting from customer-provided persistent attributes
669
- let additionalLogAttributes = { ...this . getPersistentLogAttributes ( ) } ;
718
+ const additionalAttributes : LogAttributes = { } ;
719
+ // gradually add additional attributes picking only the last added for each key
720
+ for ( const [ key , type ] of this . #keys) {
721
+ if ( type === 'persistent' ) {
722
+ additionalAttributes [ key ] = this . persistentLogAttributes [ key ] ;
723
+ } else {
724
+ additionalAttributes [ key ] = this . temporaryLogAttributes [ key ] ;
725
+ }
726
+ }
727
+
670
728
// if the main input is not a string, then it's an object with additional attributes, so we merge it
671
- additionalLogAttributes = merge ( additionalLogAttributes , otherInput ) ;
729
+ merge ( additionalAttributes , otherInput ) ;
672
730
// then we merge the extra input attributes (if any)
673
731
for ( const item of extraInput ) {
674
732
const attributes : LogAttributes =
@@ -678,12 +736,12 @@ class Logger extends Utility implements LoggerInterface {
678
736
? { extra : item }
679
737
: item ;
680
738
681
- additionalLogAttributes = merge ( additionalLogAttributes , attributes ) ;
739
+ merge ( additionalAttributes , attributes ) ;
682
740
}
683
741
684
742
return this . getLogFormatter ( ) . formatAttributes (
685
743
unformattedBaseAttributes ,
686
- additionalLogAttributes
744
+ additionalAttributes
687
745
) ;
688
746
}
689
747
@@ -1023,13 +1081,23 @@ class Logger extends Utility implements LoggerInterface {
1023
1081
serviceName,
1024
1082
sampleRateValue,
1025
1083
logFormatter,
1026
- persistentLogAttributes,
1084
+ persistentKeys,
1085
+ persistentLogAttributes, // deprecated in favor of persistentKeys
1027
1086
environment,
1028
1087
} = options ;
1029
1088
1089
+ if ( persistentLogAttributes && persistentKeys ) {
1090
+ this . warn (
1091
+ 'Both persistentLogAttributes and persistentKeys options were provided. Using persistentKeys as persistentLogAttributes is deprecated and will be removed in future releases'
1092
+ ) ;
1093
+ }
1094
+
1030
1095
// configurations that affect log content
1031
- this . setPowertoolsLogData ( serviceName , environment ) ;
1032
- this . addPersistentLogAttributes ( persistentLogAttributes ) ;
1096
+ this . setPowertoolsLogData (
1097
+ serviceName ,
1098
+ environment ,
1099
+ persistentKeys || persistentLogAttributes
1100
+ ) ;
1033
1101
1034
1102
// configurations that affect Logger behavior
1035
1103
this . setLogEvent ( ) ;
@@ -1070,7 +1138,7 @@ class Logger extends Utility implements LoggerInterface {
1070
1138
this . getEnvVarsService ( ) . getServiceName ( ) ||
1071
1139
this . getDefaultServiceName ( ) ,
1072
1140
} ) ;
1073
- this . addPersistentLogAttributes ( persistentLogAttributes ) ;
1141
+ this . appendPersistentKeys ( persistentLogAttributes ) ;
1074
1142
}
1075
1143
}
1076
1144
0 commit comments