Skip to content

Commit

Permalink
feat(formatter): change request shape
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Apr 20, 2024
1 parent d807701 commit a431813
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
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

0 comments on commit a431813

Please sign in to comment.