- jsonReplacer(key, value) {
- let replaced;
-
- // Try applying all our replacers, until one does something.
- this.jsonReplacers.every((replacer) => {
- ({ replaced, value } = replacer(key, value));
- return !replaced;
- });
- return value;
+ getReplacer() {
+ const ancestors = [];
+ const loggerReplacers = this.jsonReplacers;
+ return function cycleReplacer(key, value) {
+ loggerReplacers.every((replacer) => {
+ const oldValue = value;
+ value = replacer(key, value);
+ return oldValue === value;
+ });
+ if (typeof value === 'object' && value !== null) {
+ // 'this' is object where key/value came from
+ while (ancestors.length > 0 && ancestors.at(-1) !== this) {
+ ancestors.pop();
+ }
+ if (ancestors.includes(value)) {
+ return '[Circular]';
+ } else {
+ ancestors.push(value);
+ }
+ }
+
+ return value;
+ };