@@ -172,16 +172,16 @@ 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
*
178
182
* Sometimes we need to log warnings before the logger is fully initialized, however we can't log them
179
183
* immediately because the logger is not ready yet. This buffer stores those logs until the logger is ready.
180
184
*/
181
- /**
182
- * Temporary log attributes that can be appended with `appendKeys()` method.
183
- */
184
- private temporaryLogAttributes : LogAttributes = { } ;
185
185
#buffer: [ number , Parameters < Logger [ 'createAndPopulateLogItem' ] > ] [ ] = [ ] ;
186
186
/**
187
187
* Flag used to determine if the logger is initialized.
@@ -257,15 +257,6 @@ class Logger extends Utility implements LoggerInterface {
257
257
merge ( this . temporaryLogAttributes , attributes ) ;
258
258
}
259
259
260
- /**
261
- * It clears temporary log attributes.
262
- *
263
- * @returns {void }
264
- */
265
- public clearState ( ) : void {
266
- this . temporaryLogAttributes = { } ;
267
- }
268
-
269
260
/**
270
261
* It creates a separate Logger instance, identical to the current one
271
262
* It's possible to overwrite the new instance options by passing them.
@@ -368,16 +359,6 @@ class Logger extends Utility implements LoggerInterface {
368
359
return this . persistentLogAttributes ;
369
360
}
370
361
371
- /**
372
- * It returns the temporary log attributes, added with `appendKeys()` method.
373
- *
374
- * @private
375
- * @returns {LogAttributes }
376
- */
377
- public getTemporaryLogAttributes ( ) : LogAttributes {
378
- return this . temporaryLogAttributes ;
379
- }
380
-
381
362
/**
382
363
* It prints a log item with level INFO.
383
364
*
@@ -449,26 +430,24 @@ class Logger extends Utility implements LoggerInterface {
449
430
} catch ( error ) {
450
431
throw error ;
451
432
} finally {
452
- Logger . injectLambdaContextAfterOrOnError (
453
- loggerRef ,
454
- loggerRef . getPersistentLogAttributes ( ) ,
455
- options
456
- ) ;
433
+ Logger . injectLambdaContextAfterOrOnError ( loggerRef , { } , options ) ;
457
434
}
458
435
459
436
return result ;
460
437
} ;
461
438
} ;
462
439
}
463
440
441
+ /**
442
+ * @deprecated This method is deprecated and will be removed in the future major versions.
443
+ */
464
444
public static injectLambdaContextAfterOrOnError (
465
445
logger : Logger ,
466
- persistentAttributes : LogAttributes ,
446
+ _persistentAttributes : LogAttributes ,
467
447
options ?: InjectLambdaContextOptions
468
448
) : void {
469
449
if ( options && options . clearState === true ) {
470
- logger . clearState ( ) ;
471
- logger . addPersistentLogAttributes ( persistentAttributes ) ;
450
+ logger . resetState ( ) ;
472
451
}
473
452
}
474
453
@@ -511,31 +490,40 @@ class Logger extends Utility implements LoggerInterface {
511
490
}
512
491
513
492
/**
514
- * It removes attributes based on provided keys to all log items generated by this Logger instance.
493
+ * It removes temporary attributes based on provided keys to all log items generated by this Logger instance.
515
494
*
516
495
* @param {string[] } keys
517
496
* @returns {void }
518
497
*/
519
498
public removeKeys ( keys : string [ ] ) : void {
520
- const removeKey = ( attributes : LogAttributes , key : string ) : void => {
521
- if ( attributes && key in attributes ) {
522
- delete attributes [ key ] ;
523
- }
524
- } ;
525
499
for ( const key of keys ) {
526
- removeKey ( this . persistentLogAttributes , key ) ;
527
- removeKey ( this . temporaryLogAttributes , key ) ;
500
+ if ( this . temporaryLogAttributes && key in this . temporaryLogAttributes ) {
501
+ delete this . temporaryLogAttributes [ key ] ;
502
+ }
528
503
}
529
504
}
530
505
531
506
/**
532
- * Alias for removeKeys() .
507
+ * It removes persistent attributes based on provided keys to all log items generated by this Logger instance .
533
508
*
534
509
* @param {string[] } keys
535
510
* @returns {void }
536
511
*/
537
512
public removePersistentLogAttributes ( keys : string [ ] ) : void {
538
- this . removeKeys ( keys ) ;
513
+ for ( const key of keys ) {
514
+ if ( this . persistentLogAttributes && key in this . persistentLogAttributes ) {
515
+ delete this . persistentLogAttributes [ key ] ;
516
+ }
517
+ }
518
+ }
519
+
520
+ /**
521
+ * It resets the state, by removing all temporary log attributes added with `appendKeys()` method.
522
+ *
523
+ * @returns {void }
524
+ */
525
+ public resetState ( ) : void {
526
+ this . temporaryLogAttributes = { } ;
539
527
}
540
528
541
529
/**
@@ -692,7 +680,7 @@ class Logger extends Utility implements LoggerInterface {
692
680
// gradually merge additional attributes starting from customer-provided persistent & temporary attributes
693
681
let additionalLogAttributes = {
694
682
...this . getPersistentLogAttributes ( ) ,
695
- ...this . getTemporaryLogAttributes ( ) ,
683
+ ...this . temporaryLogAttributes ,
696
684
} ;
697
685
// if the main input is not a string, then it's an object with additional attributes, so we merge it
698
686
additionalLogAttributes = merge ( additionalLogAttributes , otherInput ) ;
0 commit comments