@@ -9,7 +9,7 @@ import type {
99import type { Context , Handler } from 'aws-lambda' ;
1010import merge from 'lodash.merge' ;
1111import { EnvironmentVariablesService } from './config/EnvironmentVariablesService.js' ;
12- import { LogJsonIndent } from './constants.js' ;
12+ import { LogJsonIndent , LogLevelThreshold } from './constants.js' ;
1313import type { LogFormatter } from './formatter/LogFormatter.js' ;
1414import type { LogItem } from './formatter/LogItem.js' ;
1515import { PowertoolsLogFormatter } from './formatter/PowertoolsLogFormatter.js' ;
@@ -23,7 +23,6 @@ import type {
2323 LogItemExtraInput ,
2424 LogItemMessage ,
2525 LogLevel ,
26- LogLevelThresholds ,
2726 LoggerInterface ,
2827} from './types/Logger.js' ;
2928import type { PowertoolsLogData } from './types/logKeys.js' ;
@@ -120,21 +119,7 @@ class Logger extends Utility implements LoggerInterface {
120119 /**
121120 * Log level used internally by the current instance of Logger.
122121 */
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 ;
138123 /**
139124 * Persistent log attributes that will be logged in all log items.
140125 */
@@ -170,7 +155,7 @@ class Logger extends Utility implements LoggerInterface {
170155 *
171156 * We keep this value to be able to reset the log level to the initial value when the sample rate is refreshed.
172157 */
173- #initialLogLevel = this . logLevelThresholds . INFO ;
158+ #initialLogLevel: number = LogLevelThreshold . INFO ;
174159 /**
175160 * Replacer function used to serialize the log items.
176161 */
@@ -298,7 +283,7 @@ class Logger extends Utility implements LoggerInterface {
298283 input : LogItemMessage ,
299284 ...extraInput : LogItemExtraInput
300285 ) : void {
301- this . processLogItem ( this . logLevelThresholds . CRITICAL , input , extraInput ) ;
286+ this . processLogItem ( LogLevelThreshold . CRITICAL , input , extraInput ) ;
302287 }
303288
304289 /**
@@ -308,7 +293,7 @@ class Logger extends Utility implements LoggerInterface {
308293 * @param extraInput - The extra input to log.
309294 */
310295 public debug ( input : LogItemMessage , ...extraInput : LogItemExtraInput ) : void {
311- this . processLogItem ( this . logLevelThresholds . DEBUG , input , extraInput ) ;
296+ this . processLogItem ( LogLevelThreshold . DEBUG , input , extraInput ) ;
312297 }
313298
314299 /**
@@ -318,7 +303,7 @@ class Logger extends Utility implements LoggerInterface {
318303 * @param extraInput - The extra input to log.
319304 */
320305 public error ( input : LogItemMessage , ...extraInput : LogItemExtraInput ) : void {
321- this . processLogItem ( this . logLevelThresholds . ERROR , input , extraInput ) ;
306+ this . processLogItem ( LogLevelThreshold . ERROR , input , extraInput ) ;
322307 }
323308
324309 /**
@@ -354,7 +339,7 @@ class Logger extends Utility implements LoggerInterface {
354339 * @param extraInput - The extra input to log.
355340 */
356341 public info ( input : LogItemMessage , ...extraInput : LogItemExtraInput ) : void {
357- this . processLogItem ( this . logLevelThresholds . INFO , input , extraInput ) ;
342+ this . processLogItem ( LogLevelThreshold . INFO , input , extraInput ) ;
358343 }
359344
360345 /**
@@ -561,7 +546,7 @@ class Logger extends Utility implements LoggerInterface {
561546 public setLogLevel ( logLevel : LogLevel ) : void {
562547 if ( this . awsLogLevelShortCircuit ( logLevel ) ) return ;
563548 if ( this . isValidLogLevel ( logLevel ) ) {
564- this . logLevel = this . logLevelThresholds [ logLevel ] ;
549+ this . logLevel = LogLevelThreshold [ logLevel ] ;
565550 } else {
566551 throw new Error ( `Invalid log level: ${ logLevel } ` ) ;
567552 }
@@ -599,7 +584,7 @@ class Logger extends Utility implements LoggerInterface {
599584 * @param extraInput - The extra input to log.
600585 */
601586 public trace ( input : LogItemMessage , ...extraInput : LogItemExtraInput ) : void {
602- this . processLogItem ( this . logLevelThresholds . TRACE , input , extraInput ) ;
587+ this . processLogItem ( LogLevelThreshold . TRACE , input , extraInput ) ;
603588 }
604589
605590 /**
@@ -609,7 +594,7 @@ class Logger extends Utility implements LoggerInterface {
609594 * @param extraInput - The extra input to log.
610595 */
611596 public warn ( input : LogItemMessage , ...extraInput : LogItemExtraInput ) : void {
612- this . processLogItem ( this . logLevelThresholds . WARN , input , extraInput ) ;
597+ this . processLogItem ( LogLevelThreshold . WARN , input , extraInput ) ;
613598 }
614599
615600 /**
@@ -684,11 +669,11 @@ class Logger extends Utility implements LoggerInterface {
684669 private awsLogLevelShortCircuit ( selectedLogLevel ?: string ) : boolean {
685670 const awsLogLevel = this . getEnvVarsService ( ) . getAwsLogLevel ( ) ;
686671 if ( this . isValidLogLevel ( awsLogLevel ) ) {
687- this . logLevel = this . logLevelThresholds [ awsLogLevel ] ;
672+ this . logLevel = LogLevelThreshold [ awsLogLevel ] ;
688673
689674 if (
690675 this . isValidLogLevel ( selectedLogLevel ) &&
691- this . logLevel > this . logLevelThresholds [ selectedLogLevel ]
676+ this . logLevel > LogLevelThreshold [ selectedLogLevel ]
692677 ) {
693678 this . warn (
694679 `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 {
800785 */
801786 private getLogLevelNameFromNumber ( logLevel : number ) : Uppercase < LogLevel > {
802787 let found : Uppercase < LogLevel > | undefined ;
803- for ( const [ key , value ] of Object . entries ( this . logLevelThresholds ) ) {
788+ for ( const [ key , value ] of Object . entries ( LogLevelThreshold ) ) {
804789 if ( value === logLevel ) {
805790 found = key as Uppercase < LogLevel > ;
806791 break ;
@@ -826,7 +811,7 @@ class Logger extends Utility implements LoggerInterface {
826811 private isValidLogLevel (
827812 logLevel ?: LogLevel | string
828813 ) : logLevel is Uppercase < LogLevel > {
829- return typeof logLevel === 'string' && logLevel in this . logLevelThresholds ;
814+ return typeof logLevel === 'string' && logLevel in LogLevelThreshold ;
830815 }
831816
832817 /**
@@ -854,7 +839,7 @@ class Logger extends Utility implements LoggerInterface {
854839 log . prepareForPrint ( ) ;
855840
856841 const consoleMethod =
857- logLevel === this . logLevelThresholds . CRITICAL
842+ logLevel === LogLevelThreshold . CRITICAL
858843 ? 'error'
859844 : ( this . getLogLevelNameFromNumber ( logLevel ) . toLowerCase ( ) as keyof Omit <
860845 LogFunction ,
@@ -947,7 +932,7 @@ class Logger extends Utility implements LoggerInterface {
947932 if ( this . awsLogLevelShortCircuit ( constructorLogLevel ) ) return ;
948933
949934 if ( this . isValidLogLevel ( constructorLogLevel ) ) {
950- this . logLevel = this . logLevelThresholds [ constructorLogLevel ] ;
935+ this . logLevel = LogLevelThreshold [ constructorLogLevel ] ;
951936 this . #initialLogLevel = this . logLevel ;
952937
953938 return ;
@@ -956,14 +941,14 @@ class Logger extends Utility implements LoggerInterface {
956941 ?. getLogLevel ( )
957942 ?. toUpperCase ( ) ;
958943 if ( this . isValidLogLevel ( customConfigValue ) ) {
959- this . logLevel = this . logLevelThresholds [ customConfigValue ] ;
944+ this . logLevel = LogLevelThreshold [ customConfigValue ] ;
960945 this . #initialLogLevel = this . logLevel ;
961946
962947 return ;
963948 }
964949 const envVarsValue = this . getEnvVarsService ( ) ?. getLogLevel ( ) ?. toUpperCase ( ) ;
965950 if ( this . isValidLogLevel ( envVarsValue ) ) {
966- this . logLevel = this . logLevelThresholds [ envVarsValue ] ;
951+ this . logLevel = LogLevelThreshold [ envVarsValue ] ;
967952 this . #initialLogLevel = this . logLevel ;
968953
969954 return ;
@@ -990,7 +975,7 @@ class Logger extends Utility implements LoggerInterface {
990975 this . powertoolsLogData . sampleRateValue = value ;
991976
992977 if (
993- this . logLevel > this . logLevelThresholds . DEBUG &&
978+ this . logLevel > LogLevelThreshold . DEBUG &&
994979 value &&
995980 randomInt ( 0 , 100 ) / 100 <= value
996981 ) {
0 commit comments