Skip to content

Commit 406a6c6

Browse files
authored
Merge pull request #115 from AthennaIO/develop
chore: add circular replacer to json stringify
2 parents e43972c + ebcb316 commit 406a6c6

File tree

6 files changed

+37
-8
lines changed

6 files changed

+37
-8
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@athenna/logger",
3-
"version": "5.5.0",
3+
"version": "5.6.0",
44
"description": "The Athenna logging solution. Log in stdout, files and buckets.",
55
"license": "MIT",
66
"author": "João Lenon <[email protected]>",

src/drivers/FileDriver.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
* file that was distributed with this source code.
88
*/
99

10+
import { debug } from '#src/debug'
1011
import { File } from '@athenna/common'
1112
import { Driver } from '#src/drivers/Driver'
12-
import { debug } from '#src/debug'
1313

1414
export class FileDriver extends Driver {
1515
public async transport(level: string, message: any): Promise<any> {

src/formatters/Formatter.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,32 @@ export abstract class Formatter {
7676
return new Date(Date.now()).toLocaleString(undefined, localeStringOptions)
7777
}
7878

79+
/**
80+
* Get the circular replacer function to be used in
81+
* JSON.stringify().
82+
*/
83+
public getCircularReplacer() {
84+
const ancestors = []
85+
86+
return function (key, value) {
87+
if (!Is.Object(value) || value === null) {
88+
return value
89+
}
90+
91+
while (ancestors.length > 0 && ancestors.at(-1) !== this) {
92+
ancestors.pop()
93+
}
94+
95+
if (ancestors.includes(value)) {
96+
return '[Circular]'
97+
}
98+
99+
ancestors.push(value)
100+
101+
return value
102+
}
103+
}
104+
79105
/**
80106
* Transform the message to string.
81107
*/
@@ -85,7 +111,7 @@ export abstract class Formatter {
85111
}
86112

87113
if (Is.Object(message)) {
88-
message = JSON.stringify(message)
114+
message = JSON.stringify(message, this.getCircularReplacer())
89115
}
90116

91117
return `${message}`

src/formatters/JsonFormatter.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ export class JsonFormatter extends Formatter {
2323
if (Is.String(message)) {
2424
base.msg = message
2525

26-
return JSON.stringify(base)
26+
return JSON.stringify(base, this.getCircularReplacer())
2727
}
2828

29-
return JSON.stringify({ ...base, ...message })
29+
return JSON.stringify({ ...base, ...message }, this.getCircularReplacer())
3030
}
3131
}

src/formatters/RequestFormatter.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ export class RequestFormatter extends Formatter {
5454
headers: ctx.response.headers
5555
}
5656

57-
return JSON.stringify({ request, response, metadata })
57+
return JSON.stringify(
58+
{ request, response, metadata },
59+
this.getCircularReplacer()
60+
)
5861
}
5962
}

0 commit comments

Comments
 (0)