@@ -24,7 +24,7 @@ import type {
24
24
LogItemMessage ,
25
25
LoggerInterface ,
26
26
PowertoolsLogData ,
27
- CustomReplacerFn ,
27
+ CustomJsonReplacerFn ,
28
28
} from './types/Logger.js' ;
29
29
30
30
/**
@@ -115,10 +115,6 @@ import type {
115
115
* @see https://docs.powertools.aws.dev/lambda/typescript/latest/core/logger/
116
116
*/
117
117
class Logger extends Utility implements LoggerInterface {
118
- /**
119
- * Replacer function used to serialize the log items.
120
- */
121
- protected jsonReplacerFn ?: CustomReplacerFn ;
122
118
/**
123
119
* Console instance used to print logs.
124
120
*
@@ -205,6 +201,10 @@ class Logger extends Utility implements LoggerInterface {
205
201
* We keep this value to be able to reset the log level to the initial value when the sample rate is refreshed.
206
202
*/
207
203
#initialLogLevel = 12 ;
204
+ /**
205
+ * Replacer function used to serialize the log items.
206
+ */
207
+ #jsonReplacerFn?: CustomJsonReplacerFn ;
208
208
209
209
/**
210
210
* Log level used by the current instance of Logger.
@@ -314,7 +314,7 @@ class Logger extends Utility implements LoggerInterface {
314
314
environment : this . powertoolsLogData . environment ,
315
315
persistentLogAttributes : this . persistentLogAttributes ,
316
316
temporaryLogAttributes : this . temporaryLogAttributes ,
317
- jsonReplacerFn : this . jsonReplacerFn ,
317
+ jsonReplacerFn : this . # jsonReplacerFn,
318
318
} ,
319
319
options
320
320
)
@@ -680,6 +680,38 @@ class Logger extends Utility implements LoggerInterface {
680
680
return new Logger ( options ) ;
681
681
}
682
682
683
+ /**
684
+ * When the data added in the log item contains object references or BigInt values,
685
+ * `JSON.stringify()` can't handle them and instead throws errors:
686
+ * `TypeError: cyclic object value` or `TypeError: Do not know how to serialize a BigInt`.
687
+ * To mitigate these issues, this method will find and remove all cyclic references and convert BigInt values to strings.
688
+ *
689
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions
690
+ * @private
691
+ */
692
+ protected getJsonReplacer ( ) : ( key : string , value : unknown ) => void {
693
+ const references = new WeakSet ( ) ;
694
+
695
+ return ( key , value ) => {
696
+ if ( this . #jsonReplacerFn) value = this . #jsonReplacerFn?.( key , value ) ;
697
+
698
+ if ( value instanceof Error ) {
699
+ value = this . getLogFormatter ( ) . formatError ( value ) ;
700
+ }
701
+ if ( typeof value === 'bigint' ) {
702
+ return value . toString ( ) ;
703
+ }
704
+ if ( typeof value === 'object' && value !== null ) {
705
+ if ( references . has ( value ) ) {
706
+ return ;
707
+ }
708
+ references . add ( value ) ;
709
+ }
710
+
711
+ return value ;
712
+ } ;
713
+ }
714
+
683
715
/**
684
716
* It stores information that is printed in all log items.
685
717
*
@@ -789,40 +821,6 @@ class Logger extends Utility implements LoggerInterface {
789
821
return this . customConfigService ;
790
822
}
791
823
792
- /**
793
- * When the data added in the log item contains object references or BigInt values,
794
- * `JSON.stringify()` can't handle them and instead throws errors:
795
- * `TypeError: cyclic object value` or `TypeError: Do not know how to serialize a BigInt`.
796
- * To mitigate these issues, this method will find and remove all cyclic references and convert BigInt values to strings.
797
- *
798
- * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions
799
- * @private
800
- */
801
- private getDefaultReplacer ( ) : (
802
- key : string ,
803
- value : LogAttributes | Error | bigint
804
- ) => void {
805
- const references = new WeakSet ( ) ;
806
-
807
- return ( key , value ) => {
808
- let item = value ;
809
- if ( item instanceof Error ) {
810
- item = this . getLogFormatter ( ) . formatError ( item ) ;
811
- }
812
- if ( typeof item === 'bigint' ) {
813
- return item . toString ( ) ;
814
- }
815
- if ( typeof item === 'object' && value !== null ) {
816
- if ( references . has ( item ) ) {
817
- return ;
818
- }
819
- references . add ( item ) ;
820
- }
821
-
822
- return item ;
823
- } ;
824
- }
825
-
826
824
/**
827
825
* It returns the instance of a service that fetches environment variables.
828
826
*
@@ -926,7 +924,7 @@ class Logger extends Utility implements LoggerInterface {
926
924
this . console [ consoleMethod ] (
927
925
JSON . stringify (
928
926
log . getAttributes ( ) ,
929
- this . jsonReplacerFn ,
927
+ this . getJsonReplacer ( ) ,
930
928
this . logIndentation
931
929
)
932
930
) ;
@@ -1150,7 +1148,7 @@ class Logger extends Utility implements LoggerInterface {
1150
1148
this . setLogFormatter ( logFormatter ) ;
1151
1149
this . setConsole ( ) ;
1152
1150
this . setLogIndentation ( ) ;
1153
- this . #setJsonReplacerFn ( jsonReplacerFn ) ;
1151
+ this . #jsonReplacerFn = jsonReplacerFn ;
1154
1152
1155
1153
return this ;
1156
1154
}
@@ -1183,16 +1181,6 @@ class Logger extends Utility implements LoggerInterface {
1183
1181
} ) ;
1184
1182
this . appendPersistentKeys ( persistentLogAttributes ) ;
1185
1183
}
1186
-
1187
- /**
1188
- * It sets the JSON replacer function which is used to serialize the log items.
1189
- * @private
1190
- * @param customerReplacerFn
1191
- */
1192
- #setJsonReplacerFn( customerReplacerFn ?: CustomReplacerFn ) : void {
1193
- this . jsonReplacerFn =
1194
- customerReplacerFn ?? ( this . getDefaultReplacer ( ) as CustomReplacerFn ) ;
1195
- }
1196
1184
}
1197
1185
1198
1186
export { Logger } ;
0 commit comments