Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(formatter): change request shape #99

Merged
merged 1 commit into from
Apr 20, 2024
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": "4.19.0",
"version": "4.20.0",
"description": "The Athenna logging solution. Log in stdout, files and buckets.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
13 changes: 7 additions & 6 deletions src/formatters/RequestFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ export class RequestFormatter extends Formatter {
return ctx
}

const responseTimeMs = `${Math.round(ctx.responseTime)}ms`
const status = Color.statusCode(ctx.status)
const statusCode = ctx.response.statusCode
const responseTimeMs = `${Math.round(ctx.response.responseTime)}ms`
const status = Color.statusCode(ctx.response.statusCode)
const method = Color.httpMethod(ctx.request.method)
const date = new Date().toISOString()

Expand All @@ -30,8 +31,8 @@ export class RequestFormatter extends Formatter {
const metadata = {
method: ctx.request.method,
duration: responseTimeMs,
status: ctx.status <= 399 ? 'SUCCESS' : 'ERROR',
statusCode: ctx.status,
status: statusCode <= 399 ? 'SUCCESS' : 'ERROR',
statusCode,
url: ctx.request.hostUrl,
path: ctx.request.baseUrl,
createdAt: Date.now(),
Expand All @@ -49,8 +50,8 @@ export class RequestFormatter extends Formatter {
}

const response = {
body: ctx.body,
headers: ctx.headers
body: ctx.response.body,
headers: ctx.response.headers
}

return JSON.stringify({ request, response, metadata })
Expand Down
30 changes: 20 additions & 10 deletions tests/unit/formatters/RequestFormatterTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ export default class RequestFormatterTest {
const formatter = new RequestFormatter().config({ level: 'info' })

const ctx = {
status: 200,
responseTime: 1,
body: {
hello: 'world'
},
Expand All @@ -36,6 +34,16 @@ export default class RequestFormatterTest {
headers: {
'Content-Type': 'application/json'
}
},
response: {
statusCode: 200,
responseTime: 1,
body: {
hello: 'world'
},
headers: {
'Content-Type': 'application/json'
}
}
}

Expand All @@ -51,14 +59,6 @@ export default class RequestFormatterTest {
const formatter = new RequestFormatter().config({ level: 'info', asJson: true })

const ctx = {
status: 200,
responseTime: 1,
body: {
hello: 'world'
},
headers: {
'Content-Type': 'application/json'
},
request: {
ip: '127.0.0.1',
method: 'GET',
Expand All @@ -71,6 +71,16 @@ export default class RequestFormatterTest {
headers: {
'Content-Type': 'application/json'
}
},
response: {
statusCode: 200,
responseTime: 1,
body: {
hello: 'world'
},
headers: {
'Content-Type': 'application/json'
}
}
}

Expand Down
Loading