@@ -789,6 +789,40 @@ class Logger extends Utility implements LoggerInterface {
789
789
return this . customConfigService ;
790
790
}
791
791
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
+
792
826
/**
793
827
* It returns the instance of a service that fetches environment variables.
794
828
*
@@ -841,40 +875,6 @@ class Logger extends Utility implements LoggerInterface {
841
875
return this . powertoolsLogData ;
842
876
}
843
877
844
- /**
845
- * When the data added in the log item contains object references or BigInt values,
846
- * `JSON.stringify()` can't handle them and instead throws errors:
847
- * `TypeError: cyclic object value` or `TypeError: Do not know how to serialize a BigInt`.
848
- * To mitigate these issues, this method will find and remove all cyclic references and convert BigInt values to strings.
849
- *
850
- * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#exceptions
851
- * @private
852
- */
853
- private getReplacer ( ) : (
854
- key : string ,
855
- value : LogAttributes | Error | bigint
856
- ) => void {
857
- const references = new WeakSet ( ) ;
858
-
859
- return ( key , value ) => {
860
- let item = value ;
861
- if ( item instanceof Error ) {
862
- item = this . getLogFormatter ( ) . formatError ( item ) ;
863
- }
864
- if ( typeof item === 'bigint' ) {
865
- return item . toString ( ) ;
866
- }
867
- if ( typeof item === 'object' && value !== null ) {
868
- if ( references . has ( item ) ) {
869
- return ;
870
- }
871
- references . add ( item ) ;
872
- }
873
-
874
- return item ;
875
- } ;
876
- }
877
-
878
878
/**
879
879
* It returns true and type guards the log level if a given log level is valid.
880
880
*
@@ -1191,7 +1191,7 @@ class Logger extends Utility implements LoggerInterface {
1191
1191
*/
1192
1192
#setJsonReplacerFn( customerReplacerFn ?: CustomReplacerFn ) : void {
1193
1193
this . jsonReplacerFn =
1194
- customerReplacerFn ?? ( this . getReplacer ( ) as CustomReplacerFn ) ;
1194
+ customerReplacerFn ?? ( this . getDefaultReplacer ( ) as CustomReplacerFn ) ;
1195
1195
}
1196
1196
}
1197
1197
0 commit comments