Skip to content

Commit 4090ea1

Browse files
committed
refactor: rename getReplacer to getDefaultReplacer function
1 parent 3e13759 commit 4090ea1

File tree

1 file changed

+35
-35
lines changed

1 file changed

+35
-35
lines changed

packages/logger/src/Logger.ts

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,40 @@ class Logger extends Utility implements LoggerInterface {
789789
return this.customConfigService;
790790
}
791791

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+
792826
/**
793827
* It returns the instance of a service that fetches environment variables.
794828
*
@@ -841,40 +875,6 @@ class Logger extends Utility implements LoggerInterface {
841875
return this.powertoolsLogData;
842876
}
843877

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-
878878
/**
879879
* It returns true and type guards the log level if a given log level is valid.
880880
*
@@ -1191,7 +1191,7 @@ class Logger extends Utility implements LoggerInterface {
11911191
*/
11921192
#setJsonReplacerFn(customerReplacerFn?: CustomReplacerFn): void {
11931193
this.jsonReplacerFn =
1194-
customerReplacerFn ?? (this.getReplacer() as CustomReplacerFn);
1194+
customerReplacerFn ?? (this.getDefaultReplacer() as CustomReplacerFn);
11951195
}
11961196
}
11971197

0 commit comments

Comments
 (0)