Skip to content

Commit

Permalink
chore(formatter): beautify request formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Sep 22, 2023
1 parent 74ed672 commit 4be2246
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 80 deletions.
132 changes: 66 additions & 66 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/logger",
"version": "4.4.0",
"version": "4.5.0",
"description": "The Athenna logging solution. Log in stdout, files and buckets.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -59,7 +59,7 @@
"cls-rtracer": "^2.6.3"
},
"devDependencies": {
"@athenna/common": "^4.10.1",
"@athenna/common": "^4.13.1",
"@athenna/config": "^4.4.0",
"@athenna/ioc": "^4.3.0",
"@athenna/test": "^4.7.0",
Expand Down
16 changes: 6 additions & 10 deletions src/formatters/RequestFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,22 @@ export class RequestFormatter extends Formatter {
return ctx
}

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

if (!this.configs.asJson) {
return this.clean(
`${methodAndStatus} - [${ip}] - ${new Date().toISOString()} - ${
ctx.request.baseUrl
} ${responseTimeMs}`
`${method}${status} ${ctx.request.baseUrl} - ${date} - ${responseTimeMs}`
)
}

const metadata = {
method: ctx.request.method,
duration: responseTimeMs,
status: status <= 399 ? 'SUCCESS' : 'ERROR',
statusCode: status,
status: ctx.status <= 399 ? 'SUCCESS' : 'ERROR',
statusCode: ctx.status,
url: ctx.request.hostUrl,
path: ctx.request.baseUrl,
createdAt: Date.now(),
Expand Down
3 changes: 1 addition & 2 deletions tests/unit/formatters/RequestFormatterTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ export default class RequestFormatterTest {

const message = formatter.format(ctx)

assert.isTrue(message.includes('127.0.0.1'))
assert.isTrue(message.includes('[GET::200]'))
assert.isTrue(message.includes('GET'))
assert.isTrue(message.includes('http://localhost:1335/:id'))
assert.isTrue(message.includes('ms'))
}
Expand Down

0 comments on commit 4be2246

Please sign in to comment.