@@ -236,6 +236,11 @@ class Logger extends Utility implements LoggerInterface {
236
236
refreshedTimes : 0 ,
237
237
} ;
238
238
239
+ /**
240
+ * Map used to store the warning messages that have already been logged.
241
+ */
242
+ readonly #warnOnceMap = new Map < string , boolean > ( ) ;
243
+
239
244
/**
240
245
* Log level used by the current instance of Logger.
241
246
*
@@ -714,6 +719,17 @@ class Logger extends Utility implements LoggerInterface {
714
719
this . processLogItem ( LogLevelThreshold . WARN , input , extraInput ) ;
715
720
}
716
721
722
+ /**
723
+ * Log a warning message once per unique message.
724
+ *
725
+ * @param message - The log message.
726
+ */
727
+ #warnOnce( message : string ) : void {
728
+ if ( this . #warnOnceMap. has ( message ) ) return ;
729
+ this . #warnOnceMap. set ( message , true ) ;
730
+ this . warn ( message ) ;
731
+ }
732
+
717
733
/**
718
734
* Factory method for instantiating logger instances. Used by `createChild` method.
719
735
* Important for customization and subclassing. It allows subclasses, like `MyOwnLogger`,
@@ -811,7 +827,7 @@ class Logger extends Utility implements LoggerInterface {
811
827
this . isValidLogLevel ( selectedLogLevel ) &&
812
828
this . logLevel > LogLevelThreshold [ selectedLogLevel ]
813
829
) {
814
- this . warn (
830
+ this . #warnOnce (
815
831
`Current log level (${ selectedLogLevel } ) does not match AWS Lambda Advanced Logging Controls minimum log level (${ awsLogLevel } ). This can lead to data loss, consider adjusting them.`
816
832
) ;
817
833
}
@@ -1153,7 +1169,10 @@ class Logger extends Utility implements LoggerInterface {
1153
1169
private setInitialLogLevel ( logLevel ?: ConstructorOptions [ 'logLevel' ] ) : void {
1154
1170
const constructorLogLevel = logLevel ?. toUpperCase ( ) ;
1155
1171
1156
- if ( this . awsLogLevelShortCircuit ( constructorLogLevel ) ) return ;
1172
+ if ( this . awsLogLevelShortCircuit ( constructorLogLevel ) ) {
1173
+ this . #initialLogLevel = this . logLevel ;
1174
+ return ;
1175
+ }
1157
1176
1158
1177
if ( this . isValidLogLevel ( constructorLogLevel ) ) {
1159
1178
this . logLevel = LogLevelThreshold [ constructorLogLevel ] ;
@@ -1469,6 +1488,7 @@ class Logger extends Utility implements LoggerInterface {
1469
1488
* logger.setCorrelationId('my-correlation-id'); // sets the correlation ID directly with the first argument as value
1470
1489
* ```
1471
1490
*
1491
+ * @example
1472
1492
* ```typescript
1473
1493
* import { Logger } from '@aws-lambda-powertools/logger';
1474
1494
* import { search } from '@aws-lambda-powertools/logger/correlationId';
@@ -1483,7 +1503,7 @@ class Logger extends Utility implements LoggerInterface {
1483
1503
public setCorrelationId ( value : unknown , correlationIdPath ?: string ) : void {
1484
1504
if ( typeof correlationIdPath === 'string' ) {
1485
1505
if ( ! this . #correlationIdSearchFn) {
1486
- this . warn (
1506
+ this . #warnOnce (
1487
1507
'correlationIdPath is set but no search function was provided. The correlation ID will not be added to the log attributes.'
1488
1508
) ;
1489
1509
return ;
0 commit comments