From a431813908eac3e212234c7716b847c6f35e707b Mon Sep 17 00:00:00 2001 From: jlenon7 Date: Sat, 20 Apr 2024 21:00:53 +0100 Subject: [PATCH] feat(formatter): change request shape --- package-lock.json | 4 +-- package.json | 2 +- src/formatters/RequestFormatter.ts | 13 ++++---- tests/unit/formatters/RequestFormatterTest.ts | 30 ++++++++++++------- 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/package-lock.json b/package-lock.json index 9710c22..8ce4bf5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/logger", - "version": "4.19.0", + "version": "4.20.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/logger", - "version": "4.19.0", + "version": "4.20.0", "license": "MIT", "dependencies": { "@aws-lambda-powertools/logger": "^1.18.1", diff --git a/package.json b/package.json index 986411c..a241c27 100644 --- a/package.json +++ b/package.json @@ -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 ", diff --git a/src/formatters/RequestFormatter.ts b/src/formatters/RequestFormatter.ts index b0b5987..7066db3 100644 --- a/src/formatters/RequestFormatter.ts +++ b/src/formatters/RequestFormatter.ts @@ -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() @@ -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(), @@ -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 }) diff --git a/tests/unit/formatters/RequestFormatterTest.ts b/tests/unit/formatters/RequestFormatterTest.ts index a0b2fee..a6b848f 100644 --- a/tests/unit/formatters/RequestFormatterTest.ts +++ b/tests/unit/formatters/RequestFormatterTest.ts @@ -16,8 +16,6 @@ export default class RequestFormatterTest { const formatter = new RequestFormatter().config({ level: 'info' }) const ctx = { - status: 200, - responseTime: 1, body: { hello: 'world' }, @@ -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' + } } } @@ -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', @@ -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' + } } }