@@ -156,8 +156,6 @@ class Logger extends Utility implements ClassThatLogs {
156
156
SILENT : 28 ,
157
157
} ;
158
158
159
- private logsSampled = false ;
160
-
161
159
private persistentLogAttributes ?: LogAttributes = { } ;
162
160
163
161
private powertoolLogData : PowertoolLogData = < PowertoolLogData > { } ;
@@ -236,6 +234,7 @@ class Logger extends Utility implements ClassThatLogs {
236
234
logLevel : this . getLevelName ( ) ,
237
235
customConfigService : this . getCustomConfigService ( ) ,
238
236
logFormatter : this . getLogFormatter ( ) ,
237
+ sampleRateValue : this . powertoolLogData . sampleRateValue ,
239
238
} ;
240
239
const parentsPowertoolsLogData = this . getPowertoolLogData ( ) ;
241
240
const childLogger = this . createLogger (
@@ -309,15 +308,6 @@ class Logger extends Utility implements ClassThatLogs {
309
308
return this . logEvent ;
310
309
}
311
310
312
- /**
313
- * It returns a boolean value, if true all the logs will be printed.
314
- *
315
- * @returns {boolean }
316
- */
317
- public getLogsSampled ( ) : boolean {
318
- return this . logsSampled ;
319
- }
320
-
321
311
/**
322
312
* It returns the persistent log attributes, which are the attributes
323
313
* that will be logged in all log items.
@@ -457,15 +447,14 @@ class Logger extends Utility implements ClassThatLogs {
457
447
}
458
448
459
449
/**
460
- * If the sample rate feature is enabled, the calculation that determines whether the logs
461
- * will actually be printed or not for this invocation is done when the Logger class is
462
- * initialized.
463
- * This method will repeat that calculation (with possible different outcome).
450
+ * This method allows recalculating the initial sampling decision for changing
451
+ * the log level to DEBUG based on a sample rate value used during initialization,
452
+ * potentially yielding a different outcome.
464
453
*
465
454
* @returns {void }
466
455
*/
467
456
public refreshSampleRateCalculation ( ) : void {
468
- this . setLogsSampled ( ) ;
457
+ this . setInitialSampleRate ( this . powertoolLogData . sampleRateValue ) ;
469
458
}
470
459
471
460
/**
@@ -520,19 +509,6 @@ class Logger extends Utility implements ClassThatLogs {
520
509
this . persistentLogAttributes = attributes ;
521
510
}
522
511
523
- /**
524
- * It sets the user-provided sample rate value.
525
- *
526
- * @param {number } [sampleRateValue]
527
- * @returns {void }
528
- */
529
- public setSampleRateValue ( sampleRateValue ?: number ) : void {
530
- this . powertoolLogData . sampleRateValue =
531
- sampleRateValue ||
532
- this . getCustomConfigService ( ) ?. getSampleRateValue ( ) ||
533
- this . getEnvVarsService ( ) . getSampleRateValue ( ) ;
534
- }
535
-
536
512
/**
537
513
* It checks whether the current Lambda invocation event should be printed in the logs or not.
538
514
*
@@ -560,36 +536,31 @@ class Logger extends Utility implements ClassThatLogs {
560
536
}
561
537
562
538
/**
563
- * Creates a new Logger instance.
539
+ * Factory method for instantiating logger instances. Used by `createChild` method.
540
+ * Important for customization and subclassing. It allows subclasses, like `MyOwnLogger`,
541
+ * to override its behavior while keeping the main business logic in `createChild` intact.
564
542
*
565
- * @param {ConstructorOptions } [options]
566
- * @returns {Logger }
543
+ * @example
544
+ * ```typescript
545
+ * // MyOwnLogger subclass
546
+ * class MyOwnLogger extends Logger {
547
+ * protected createLogger(options?: ConstructorOptions): MyOwnLogger {
548
+ * return new MyOwnLogger(options);
549
+ * }
550
+ * // No need to re-implement business logic from `createChild` and keep track on changes
551
+ * public createChild(options?: ConstructorOptions): MyOwnLogger {
552
+ * return super.createChild(options) as MyOwnLogger;
553
+ * }
554
+ * }
555
+ * ```
556
+ *
557
+ * @param {ConstructorOptions } [options] Logger configuration options.
558
+ * @returns {Logger } A new logger instance.
567
559
*/
568
560
protected createLogger ( options ?: ConstructorOptions ) : Logger {
569
561
return new Logger ( options ) ;
570
562
}
571
563
572
- /**
573
- * Decides whether the current log item should be printed or not.
574
- *
575
- * The decision is based on the log level and the sample rate value.
576
- * A log item will be printed if:
577
- * 1. The log level is greater than or equal to the Logger's log level.
578
- * 2. The log level is less than the Logger's log level, but the
579
- * current sampling value is set to `true`.
580
- *
581
- * @param {number } logLevel
582
- * @returns {boolean }
583
- * @protected
584
- */
585
- protected shouldPrint ( logLevel : number ) : boolean {
586
- if ( logLevel >= this . logLevel ) {
587
- return true ;
588
- }
589
-
590
- return this . getLogsSampled ( ) ;
591
- }
592
-
593
564
/**
594
565
* It stores information that is printed in all log items.
595
566
*
@@ -779,20 +750,6 @@ class Logger extends Utility implements ClassThatLogs {
779
750
} ;
780
751
}
781
752
782
- /**
783
- * It returns the numeric sample rate value.
784
- *
785
- * @private
786
- * @returns {number }
787
- */
788
- private getSampleRateValue ( ) : number {
789
- if ( ! this . powertoolLogData . sampleRateValue ) {
790
- this . setSampleRateValue ( ) ;
791
- }
792
-
793
- return this . powertoolLogData . sampleRateValue as number ;
794
- }
795
-
796
753
/**
797
754
* It returns true and type guards the log level if a given log level is valid.
798
755
*
@@ -806,6 +763,23 @@ class Logger extends Utility implements ClassThatLogs {
806
763
return typeof logLevel === 'string' && logLevel in this . logLevelThresholds ;
807
764
}
808
765
766
+ /**
767
+ * It returns true and type guards the sample rate value if a given value is valid.
768
+ *
769
+ * @param sampleRateValue
770
+ * @private
771
+ * @returns {boolean }
772
+ */
773
+ private isValidSampleRate (
774
+ sampleRateValue ?: number
775
+ ) : sampleRateValue is number {
776
+ return (
777
+ typeof sampleRateValue === 'number' &&
778
+ 0 <= sampleRateValue &&
779
+ sampleRateValue <= 1
780
+ ) ;
781
+ }
782
+
809
783
/**
810
784
* It prints a given log with given log level.
811
785
*
@@ -846,13 +820,12 @@ class Logger extends Utility implements ClassThatLogs {
846
820
input : LogItemMessage ,
847
821
extraInput : LogItemExtraInput
848
822
) : void {
849
- if ( ! this . shouldPrint ( logLevel ) ) {
850
- return ;
823
+ if ( logLevel >= this . logLevel ) {
824
+ this . printLog (
825
+ logLevel ,
826
+ this . createAndPopulateLogItem ( logLevel , input , extraInput )
827
+ ) ;
851
828
}
852
- this . printLog (
853
- logLevel ,
854
- this . createAndPopulateLogItem ( logLevel , input , extraInput )
855
- ) ;
856
829
}
857
830
858
831
/**
@@ -938,6 +911,37 @@ class Logger extends Utility implements ClassThatLogs {
938
911
}
939
912
}
940
913
914
+ /**
915
+ * It sets sample rate value with the following prioprity:
916
+ * 1. Constructor value
917
+ * 2. Custom config service value
918
+ * 3. Environment variable value
919
+ * 4. Default value (zero)
920
+ *
921
+ * @private
922
+ * @param {number } [sampleRateValue]
923
+ * @returns {void }
924
+ */
925
+ private setInitialSampleRate ( sampleRateValue ?: number ) : void {
926
+ this . powertoolLogData . sampleRateValue = 0 ;
927
+ const constructorValue = sampleRateValue ;
928
+ const customConfigValue =
929
+ this . getCustomConfigService ( ) ?. getSampleRateValue ( ) ;
930
+ const envVarsValue = this . getEnvVarsService ( ) . getSampleRateValue ( ) ;
931
+ for ( const value of [ constructorValue , customConfigValue , envVarsValue ] ) {
932
+ if ( this . isValidSampleRate ( value ) ) {
933
+ this . powertoolLogData . sampleRateValue = value ;
934
+
935
+ if ( value && randomInt ( 0 , 100 ) / 100 <= value ) {
936
+ this . setLogLevel ( 'DEBUG' ) ;
937
+ this . debug ( 'Setting log level to DEBUG due to sampling rate' ) ;
938
+ }
939
+
940
+ return ;
941
+ }
942
+ }
943
+ }
944
+
941
945
/**
942
946
* If the log event feature is enabled via env variable, it sets a property that tracks whether
943
947
* the event passed to the Lambda function handler should be logged or not.
@@ -976,20 +980,6 @@ class Logger extends Utility implements ClassThatLogs {
976
980
}
977
981
}
978
982
979
- /**
980
- * If the sample rate feature is enabled, it sets a property that tracks whether this Lambda function invocation
981
- * will print logs or not.
982
- *
983
- * @private
984
- * @returns {void }
985
- */
986
- private setLogsSampled ( ) : void {
987
- const sampleRateValue = this . getSampleRateValue ( ) ;
988
- this . logsSampled =
989
- sampleRateValue !== undefined &&
990
- ( sampleRateValue === 1 || randomInt ( 0 , 100 ) / 100 <= sampleRateValue ) ;
991
- }
992
-
993
983
/**
994
984
* It configures the Logger instance settings that will affect the Logger's behaviour
995
985
* and the content of all logs.
@@ -1014,10 +1004,9 @@ class Logger extends Utility implements ClassThatLogs {
1014
1004
this . setConsole ( ) ;
1015
1005
this . setCustomConfigService ( customConfigService ) ;
1016
1006
this . setInitialLogLevel ( logLevel ) ;
1017
- this . setSampleRateValue ( sampleRateValue ) ;
1018
- this . setLogsSampled ( ) ;
1019
1007
this . setLogFormatter ( logFormatter ) ;
1020
1008
this . setPowertoolLogData ( serviceName , environment ) ;
1009
+ this . setInitialSampleRate ( sampleRateValue ) ;
1021
1010
this . setLogEvent ( ) ;
1022
1011
this . setLogIndentation ( ) ;
1023
1012
@@ -1047,7 +1036,6 @@ class Logger extends Utility implements ClassThatLogs {
1047
1036
environment ||
1048
1037
this . getCustomConfigService ( ) ?. getCurrentEnvironment ( ) ||
1049
1038
this . getEnvVarsService ( ) . getCurrentEnvironment ( ) ,
1050
- sampleRateValue : this . getSampleRateValue ( ) ,
1051
1039
serviceName :
1052
1040
serviceName ||
1053
1041
this . getCustomConfigService ( ) ?. getServiceName ( ) ||
0 commit comments