@@ -9,7 +9,7 @@ import type {
9
9
import type { Context , Handler } from 'aws-lambda' ;
10
10
import merge from 'lodash.merge' ;
11
11
import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js' ;
12
- import { LogJsonIndent } from './constants.js' ;
12
+ import { LogJsonIndent , LogLevelThreshold } from './constants.js' ;
13
13
import type { LogFormatter } from './formatter/LogFormatter.js' ;
14
14
import type { LogItem } from './formatter/LogItem.js' ;
15
15
import { PowertoolsLogFormatter } from './formatter/PowertoolsLogFormatter.js' ;
@@ -23,7 +23,6 @@ import type {
23
23
LogItemExtraInput ,
24
24
LogItemMessage ,
25
25
LogLevel ,
26
- LogLevelThresholds ,
27
26
LoggerInterface ,
28
27
} from './types/Logger.js' ;
29
28
import type { PowertoolsLogData } from './types/logKeys.js' ;
@@ -120,21 +119,7 @@ class Logger extends Utility implements LoggerInterface {
120
119
/**
121
120
* Log level used internally by the current instance of Logger.
122
121
*/
123
- private logLevel = 12 ;
124
- /**
125
- * Log level thresholds used internally by the current instance of Logger.
126
- *
127
- * The levels are in ascending order from the most verbose to the least verbose (no logs).
128
- */
129
- private readonly logLevelThresholds : LogLevelThresholds = {
130
- TRACE : 6 ,
131
- DEBUG : 8 ,
132
- INFO : 12 ,
133
- WARN : 16 ,
134
- ERROR : 20 ,
135
- CRITICAL : 24 ,
136
- SILENT : 28 ,
137
- } ;
122
+ private logLevel : number = LogLevelThreshold . INFO ;
138
123
/**
139
124
* Persistent log attributes that will be logged in all log items.
140
125
*/
@@ -170,7 +155,7 @@ class Logger extends Utility implements LoggerInterface {
170
155
*
171
156
* We keep this value to be able to reset the log level to the initial value when the sample rate is refreshed.
172
157
*/
173
- #initialLogLevel = this . logLevelThresholds . INFO ;
158
+ #initialLogLevel: number = LogLevelThreshold . INFO ;
174
159
/**
175
160
* Replacer function used to serialize the log items.
176
161
*/
@@ -298,7 +283,7 @@ class Logger extends Utility implements LoggerInterface {
298
283
input : LogItemMessage ,
299
284
...extraInput : LogItemExtraInput
300
285
) : void {
301
- this . processLogItem ( this . logLevelThresholds . CRITICAL , input , extraInput ) ;
286
+ this . processLogItem ( LogLevelThreshold . CRITICAL , input , extraInput ) ;
302
287
}
303
288
304
289
/**
@@ -308,7 +293,7 @@ class Logger extends Utility implements LoggerInterface {
308
293
* @param extraInput - The extra input to log.
309
294
*/
310
295
public debug ( input : LogItemMessage , ...extraInput : LogItemExtraInput ) : void {
311
- this . processLogItem ( this . logLevelThresholds . DEBUG , input , extraInput ) ;
296
+ this . processLogItem ( LogLevelThreshold . DEBUG , input , extraInput ) ;
312
297
}
313
298
314
299
/**
@@ -318,7 +303,7 @@ class Logger extends Utility implements LoggerInterface {
318
303
* @param extraInput - The extra input to log.
319
304
*/
320
305
public error ( input : LogItemMessage , ...extraInput : LogItemExtraInput ) : void {
321
- this . processLogItem ( this . logLevelThresholds . ERROR , input , extraInput ) ;
306
+ this . processLogItem ( LogLevelThreshold . ERROR , input , extraInput ) ;
322
307
}
323
308
324
309
/**
@@ -354,7 +339,7 @@ class Logger extends Utility implements LoggerInterface {
354
339
* @param extraInput - The extra input to log.
355
340
*/
356
341
public info ( input : LogItemMessage , ...extraInput : LogItemExtraInput ) : void {
357
- this . processLogItem ( this . logLevelThresholds . INFO , input , extraInput ) ;
342
+ this . processLogItem ( LogLevelThreshold . INFO , input , extraInput ) ;
358
343
}
359
344
360
345
/**
@@ -561,7 +546,7 @@ class Logger extends Utility implements LoggerInterface {
561
546
public setLogLevel ( logLevel : LogLevel ) : void {
562
547
if ( this . awsLogLevelShortCircuit ( logLevel ) ) return ;
563
548
if ( this . isValidLogLevel ( logLevel ) ) {
564
- this . logLevel = this . logLevelThresholds [ logLevel ] ;
549
+ this . logLevel = LogLevelThreshold [ logLevel ] ;
565
550
} else {
566
551
throw new Error ( `Invalid log level: ${ logLevel } ` ) ;
567
552
}
@@ -599,7 +584,7 @@ class Logger extends Utility implements LoggerInterface {
599
584
* @param extraInput - The extra input to log.
600
585
*/
601
586
public trace ( input : LogItemMessage , ...extraInput : LogItemExtraInput ) : void {
602
- this . processLogItem ( this . logLevelThresholds . TRACE , input , extraInput ) ;
587
+ this . processLogItem ( LogLevelThreshold . TRACE , input , extraInput ) ;
603
588
}
604
589
605
590
/**
@@ -609,7 +594,7 @@ class Logger extends Utility implements LoggerInterface {
609
594
* @param extraInput - The extra input to log.
610
595
*/
611
596
public warn ( input : LogItemMessage , ...extraInput : LogItemExtraInput ) : void {
612
- this . processLogItem ( this . logLevelThresholds . WARN , input , extraInput ) ;
597
+ this . processLogItem ( LogLevelThreshold . WARN , input , extraInput ) ;
613
598
}
614
599
615
600
/**
@@ -684,11 +669,11 @@ class Logger extends Utility implements LoggerInterface {
684
669
private awsLogLevelShortCircuit ( selectedLogLevel ?: string ) : boolean {
685
670
const awsLogLevel = this . getEnvVarsService ( ) . getAwsLogLevel ( ) ;
686
671
if ( this . isValidLogLevel ( awsLogLevel ) ) {
687
- this . logLevel = this . logLevelThresholds [ awsLogLevel ] ;
672
+ this . logLevel = LogLevelThreshold [ awsLogLevel ] ;
688
673
689
674
if (
690
675
this . isValidLogLevel ( selectedLogLevel ) &&
691
- this . logLevel > this . logLevelThresholds [ selectedLogLevel ]
676
+ this . logLevel > LogLevelThreshold [ selectedLogLevel ]
692
677
) {
693
678
this . warn (
694
679
`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.`
@@ -800,7 +785,7 @@ class Logger extends Utility implements LoggerInterface {
800
785
*/
801
786
private getLogLevelNameFromNumber ( logLevel : number ) : Uppercase < LogLevel > {
802
787
let found : Uppercase < LogLevel > | undefined ;
803
- for ( const [ key , value ] of Object . entries ( this . logLevelThresholds ) ) {
788
+ for ( const [ key , value ] of Object . entries ( LogLevelThreshold ) ) {
804
789
if ( value === logLevel ) {
805
790
found = key as Uppercase < LogLevel > ;
806
791
break ;
@@ -826,7 +811,7 @@ class Logger extends Utility implements LoggerInterface {
826
811
private isValidLogLevel (
827
812
logLevel ?: LogLevel | string
828
813
) : logLevel is Uppercase < LogLevel > {
829
- return typeof logLevel === 'string' && logLevel in this . logLevelThresholds ;
814
+ return typeof logLevel === 'string' && logLevel in LogLevelThreshold ;
830
815
}
831
816
832
817
/**
@@ -854,7 +839,7 @@ class Logger extends Utility implements LoggerInterface {
854
839
log . prepareForPrint ( ) ;
855
840
856
841
const consoleMethod =
857
- logLevel === this . logLevelThresholds . CRITICAL
842
+ logLevel === LogLevelThreshold . CRITICAL
858
843
? 'error'
859
844
: ( this . getLogLevelNameFromNumber ( logLevel ) . toLowerCase ( ) as keyof Omit <
860
845
LogFunction ,
@@ -947,7 +932,7 @@ class Logger extends Utility implements LoggerInterface {
947
932
if ( this . awsLogLevelShortCircuit ( constructorLogLevel ) ) return ;
948
933
949
934
if ( this . isValidLogLevel ( constructorLogLevel ) ) {
950
- this . logLevel = this . logLevelThresholds [ constructorLogLevel ] ;
935
+ this . logLevel = LogLevelThreshold [ constructorLogLevel ] ;
951
936
this . #initialLogLevel = this . logLevel ;
952
937
953
938
return ;
@@ -956,14 +941,14 @@ class Logger extends Utility implements LoggerInterface {
956
941
?. getLogLevel ( )
957
942
?. toUpperCase ( ) ;
958
943
if ( this . isValidLogLevel ( customConfigValue ) ) {
959
- this . logLevel = this . logLevelThresholds [ customConfigValue ] ;
944
+ this . logLevel = LogLevelThreshold [ customConfigValue ] ;
960
945
this . #initialLogLevel = this . logLevel ;
961
946
962
947
return ;
963
948
}
964
949
const envVarsValue = this . getEnvVarsService ( ) ?. getLogLevel ( ) ?. toUpperCase ( ) ;
965
950
if ( this . isValidLogLevel ( envVarsValue ) ) {
966
- this . logLevel = this . logLevelThresholds [ envVarsValue ] ;
951
+ this . logLevel = LogLevelThreshold [ envVarsValue ] ;
967
952
this . #initialLogLevel = this . logLevel ;
968
953
969
954
return ;
@@ -990,7 +975,7 @@ class Logger extends Utility implements LoggerInterface {
990
975
this . powertoolsLogData . sampleRateValue = value ;
991
976
992
977
if (
993
- this . logLevel > this . logLevelThresholds . DEBUG &&
978
+ this . logLevel > LogLevelThreshold . DEBUG &&
994
979
value &&
995
980
randomInt ( 0 , 100 ) / 100 <= value
996
981
) {
0 commit comments