Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/logger",
"version": "5.5.0",
"version": "5.6.0",
"description": "The Athenna logging solution. Log in stdout, files and buckets.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
2 changes: 1 addition & 1 deletion src/drivers/FileDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
* file that was distributed with this source code.
*/

import { debug } from '#src/debug'
import { File } from '@athenna/common'
import { Driver } from '#src/drivers/Driver'
import { debug } from '#src/debug'

export class FileDriver extends Driver {
public async transport(level: string, message: any): Promise<any> {
Expand Down
28 changes: 27 additions & 1 deletion src/formatters/Formatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,32 @@ export abstract class Formatter {
return new Date(Date.now()).toLocaleString(undefined, localeStringOptions)
}

/**
* Get the circular replacer function to be used in
* JSON.stringify().
*/
public getCircularReplacer() {
const ancestors = []

return function (key, value) {
if (!Is.Object(value) || value === null) {
return value
}

while (ancestors.length > 0 && ancestors.at(-1) !== this) {
ancestors.pop()
}

if (ancestors.includes(value)) {
return '[Circular]'
}

ancestors.push(value)

return value
}
}

/**
* Transform the message to string.
*/
Expand All @@ -85,7 +111,7 @@ export abstract class Formatter {
}

if (Is.Object(message)) {
message = JSON.stringify(message)
message = JSON.stringify(message, this.getCircularReplacer())
}

return `${message}`
Expand Down
4 changes: 2 additions & 2 deletions src/formatters/JsonFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ export class JsonFormatter extends Formatter {
if (Is.String(message)) {
base.msg = message

return JSON.stringify(base)
return JSON.stringify(base, this.getCircularReplacer())
}

return JSON.stringify({ ...base, ...message })
return JSON.stringify({ ...base, ...message }, this.getCircularReplacer())
}
}
5 changes: 4 additions & 1 deletion src/formatters/RequestFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export class RequestFormatter extends Formatter {
headers: ctx.response.headers
}

return JSON.stringify({ request, response, metadata })
return JSON.stringify(
{ request, response, metadata },
this.getCircularReplacer()
)
}
}