@@ -155,8 +155,6 @@ class Logger extends Utility implements ClassThatLogs {
155
155
SILENT : 28 ,
156
156
} ;
157
157
158
- private logsSampled = false ;
159
-
160
158
private persistentLogAttributes ?: LogAttributes = { } ;
161
159
162
160
private powertoolLogData : PowertoolLogData = < PowertoolLogData > { } ;
@@ -235,6 +233,7 @@ class Logger extends Utility implements ClassThatLogs {
235
233
logLevel : this . getLevelName ( ) ,
236
234
customConfigService : this . getCustomConfigService ( ) ,
237
235
logFormatter : this . getLogFormatter ( ) ,
236
+ sampleRateValue : this . powertoolLogData . sampleRateValue ,
238
237
} ;
239
238
const parentsPowertoolsLogData = this . getPowertoolLogData ( ) ;
240
239
const childLogger = this . createLogger (
@@ -308,15 +307,6 @@ class Logger extends Utility implements ClassThatLogs {
308
307
return this . logEvent ;
309
308
}
310
309
311
- /**
312
- * It returns a boolean value, if true all the logs will be printed.
313
- *
314
- * @returns {boolean }
315
- */
316
- public getLogsSampled ( ) : boolean {
317
- return this . logsSampled ;
318
- }
319
-
320
310
/**
321
311
* It returns the persistent log attributes, which are the attributes
322
312
* that will be logged in all log items.
@@ -456,15 +446,14 @@ class Logger extends Utility implements ClassThatLogs {
456
446
}
457
447
458
448
/**
459
- * If the sample rate feature is enabled, the calculation that determines whether the logs
460
- * will actually be printed or not for this invocation is done when the Logger class is
461
- * initialized.
462
- * This method will repeat that calculation (with possible different outcome).
449
+ * This method allows recalculating the initial sampling decision for changing
450
+ * the log level to DEBUG based on a sample rate value used during initialization,
451
+ * potentially yielding a different outcome.
463
452
*
464
453
* @returns {void }
465
454
*/
466
455
public refreshSampleRateCalculation ( ) : void {
467
- this . setLogsSampled ( ) ;
456
+ this . setInitialSampleRate ( this . powertoolLogData . sampleRateValue ) ;
468
457
}
469
458
470
459
/**
@@ -515,19 +504,6 @@ class Logger extends Utility implements ClassThatLogs {
515
504
this . persistentLogAttributes = attributes ;
516
505
}
517
506
518
- /**
519
- * It sets the user-provided sample rate value.
520
- *
521
- * @param {number } [sampleRateValue]
522
- * @returns {void }
523
- */
524
- public setSampleRateValue ( sampleRateValue ?: number ) : void {
525
- this . powertoolLogData . sampleRateValue =
526
- sampleRateValue ||
527
- this . getCustomConfigService ( ) ?. getSampleRateValue ( ) ||
528
- this . getEnvVarsService ( ) . getSampleRateValue ( ) ;
529
- }
530
-
531
507
/**
532
508
* It checks whether the current Lambda invocation event should be printed in the logs or not.
533
509
*
@@ -555,36 +531,31 @@ class Logger extends Utility implements ClassThatLogs {
555
531
}
556
532
557
533
/**
558
- * Creates a new Logger instance.
534
+ * Factory method for instantiating logger instances. Used by `createChild` method.
535
+ * Important for customization and subclassing. It allows subclasses, like `MyOwnLogger`,
536
+ * to override its behavior while keeping the main business logic in `createChild` intact.
559
537
*
560
- * @param {ConstructorOptions } [options]
561
- * @returns {Logger }
538
+ * @example
539
+ * ```typescript
540
+ * // MyOwnLogger subclass
541
+ * class MyOwnLogger extends Logger {
542
+ * protected createLogger(options?: ConstructorOptions): MyOwnLogger {
543
+ * return new MyOwnLogger(options);
544
+ * }
545
+ * // No need to re-implement business logic from `createChild` and keep track on changes
546
+ * public createChild(options?: ConstructorOptions): MyOwnLogger {
547
+ * return super.createChild(options) as MyOwnLogger;
548
+ * }
549
+ * }
550
+ * ```
551
+ *
552
+ * @param {ConstructorOptions } [options] Logger configuration options.
553
+ * @returns {Logger } A new logger instance.
562
554
*/
563
555
protected createLogger ( options ?: ConstructorOptions ) : Logger {
564
556
return new Logger ( options ) ;
565
557
}
566
558
567
- /**
568
- * Decides whether the current log item should be printed or not.
569
- *
570
- * The decision is based on the log level and the sample rate value.
571
- * A log item will be printed if:
572
- * 1. The log level is greater than or equal to the Logger's log level.
573
- * 2. The log level is less than the Logger's log level, but the
574
- * current sampling value is set to `true`.
575
- *
576
- * @param {number } logLevel
577
- * @returns {boolean }
578
- * @protected
579
- */
580
- protected shouldPrint ( logLevel : number ) : boolean {
581
- if ( logLevel >= this . logLevel ) {
582
- return true ;
583
- }
584
-
585
- return this . getLogsSampled ( ) ;
586
- }
587
-
588
559
/**
589
560
* It stores information that is printed in all log items.
590
561
*
@@ -750,20 +721,6 @@ class Logger extends Utility implements ClassThatLogs {
750
721
} ;
751
722
}
752
723
753
- /**
754
- * It returns the numeric sample rate value.
755
- *
756
- * @private
757
- * @returns {number }
758
- */
759
- private getSampleRateValue ( ) : number {
760
- if ( ! this . powertoolLogData . sampleRateValue ) {
761
- this . setSampleRateValue ( ) ;
762
- }
763
-
764
- return this . powertoolLogData . sampleRateValue as number ;
765
- }
766
-
767
724
/**
768
725
* It returns true and type guards the log level if a given log level is valid.
769
726
*
@@ -777,6 +734,23 @@ class Logger extends Utility implements ClassThatLogs {
777
734
return typeof logLevel === 'string' && logLevel in this . logLevelThresholds ;
778
735
}
779
736
737
+ /**
738
+ * It returns true and type guards the sample rate value if a given value is valid.
739
+ *
740
+ * @param sampleRateValue
741
+ * @private
742
+ * @returns {boolean }
743
+ */
744
+ private isValidSampleRate (
745
+ sampleRateValue ?: number
746
+ ) : sampleRateValue is number {
747
+ return (
748
+ typeof sampleRateValue === 'number' &&
749
+ 0 <= sampleRateValue &&
750
+ sampleRateValue <= 1
751
+ ) ;
752
+ }
753
+
780
754
/**
781
755
* It prints a given log with given log level.
782
756
*
@@ -817,13 +791,12 @@ class Logger extends Utility implements ClassThatLogs {
817
791
input : LogItemMessage ,
818
792
extraInput : LogItemExtraInput
819
793
) : void {
820
- if ( ! this . shouldPrint ( logLevel ) ) {
821
- return ;
794
+ if ( logLevel >= this . logLevel ) {
795
+ this . printLog (
796
+ logLevel ,
797
+ this . createAndPopulateLogItem ( logLevel , input , extraInput )
798
+ ) ;
822
799
}
823
- this . printLog (
824
- logLevel ,
825
- this . createAndPopulateLogItem ( logLevel , input , extraInput )
826
- ) ;
827
800
}
828
801
829
802
/**
@@ -905,6 +878,37 @@ class Logger extends Utility implements ClassThatLogs {
905
878
}
906
879
}
907
880
881
+ /**
882
+ * It sets sample rate value with the following prioprity:
883
+ * 1. Constructor value
884
+ * 2. Custom config service value
885
+ * 3. Environment variable value
886
+ * 4. Default value (zero)
887
+ *
888
+ * @private
889
+ * @param {number } [sampleRateValue]
890
+ * @returns {void }
891
+ */
892
+ private setInitialSampleRate ( sampleRateValue ?: number ) : void {
893
+ this . powertoolLogData . sampleRateValue = 0 ;
894
+ const constructorValue = sampleRateValue ;
895
+ const customConfigValue =
896
+ this . getCustomConfigService ( ) ?. getSampleRateValue ( ) ;
897
+ const envVarsValue = this . getEnvVarsService ( ) . getSampleRateValue ( ) ;
898
+ for ( const value of [ constructorValue , customConfigValue , envVarsValue ] ) {
899
+ if ( this . isValidSampleRate ( value ) ) {
900
+ this . powertoolLogData . sampleRateValue = value ;
901
+
902
+ if ( value && randomInt ( 0 , 100 ) / 100 <= value ) {
903
+ this . setLogLevel ( 'DEBUG' ) ;
904
+ this . debug ( 'Setting log level to DEBUG due to sampling rate' ) ;
905
+ }
906
+
907
+ return ;
908
+ }
909
+ }
910
+ }
911
+
908
912
/**
909
913
* If the log event feature is enabled via env variable, it sets a property that tracks whether
910
914
* the event passed to the Lambda function handler should be logged or not.
@@ -943,20 +947,6 @@ class Logger extends Utility implements ClassThatLogs {
943
947
}
944
948
}
945
949
946
- /**
947
- * If the sample rate feature is enabled, it sets a property that tracks whether this Lambda function invocation
948
- * will print logs or not.
949
- *
950
- * @private
951
- * @returns {void }
952
- */
953
- private setLogsSampled ( ) : void {
954
- const sampleRateValue = this . getSampleRateValue ( ) ;
955
- this . logsSampled =
956
- sampleRateValue !== undefined &&
957
- ( sampleRateValue === 1 || randomInt ( 0 , 100 ) / 100 <= sampleRateValue ) ;
958
- }
959
-
960
950
/**
961
951
* It configures the Logger instance settings that will affect the Logger's behaviour
962
952
* and the content of all logs.
@@ -981,10 +971,9 @@ class Logger extends Utility implements ClassThatLogs {
981
971
this . setConsole ( ) ;
982
972
this . setCustomConfigService ( customConfigService ) ;
983
973
this . setInitialLogLevel ( logLevel ) ;
984
- this . setSampleRateValue ( sampleRateValue ) ;
985
- this . setLogsSampled ( ) ;
986
974
this . setLogFormatter ( logFormatter ) ;
987
975
this . setPowertoolLogData ( serviceName , environment ) ;
976
+ this . setInitialSampleRate ( sampleRateValue ) ;
988
977
this . setLogEvent ( ) ;
989
978
this . setLogIndentation ( ) ;
990
979
@@ -1014,7 +1003,6 @@ class Logger extends Utility implements ClassThatLogs {
1014
1003
environment ||
1015
1004
this . getCustomConfigService ( ) ?. getCurrentEnvironment ( ) ||
1016
1005
this . getEnvVarsService ( ) . getCurrentEnvironment ( ) ,
1017
- sampleRateValue : this . getSampleRateValue ( ) ,
1018
1006
serviceName :
1019
1007
serviceName ||
1020
1008
this . getCustomConfigService ( ) ?. getServiceName ( ) ||
0 commit comments