From 36950b36e246a43dc45d2d9ef2d989402eef916b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Sat, 13 May 2023 14:39:09 -0300 Subject: [PATCH] perf(default-handler): always log using fn --- src/handlers/default/default.handler.ts | 21 +++++++++++---------- test/handlers/default.handler.spec.ts | 17 +++++++++-------- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/handlers/default/default.handler.ts b/src/handlers/default/default.handler.ts index dd1cde9f..410dfcfa 100644 --- a/src/handlers/default/default.handler.ts +++ b/src/handlers/default/default.handler.ts @@ -151,16 +151,14 @@ export class DefaultHandler< log: ILogger, requestValues: AdapterRequest, ): void { - const body = requestValues.body?.toString(); - log.debug( 'SERVERLESS_ADAPTER:FORWARD_REQUEST_TO_FRAMEWORK:REQUEST_VALUES', - { + () => ({ requestValues: { ...requestValues, - body, + body: requestValues.body?.toString(), }, - }, + }), ); } @@ -174,9 +172,12 @@ export class DefaultHandler< log: ILogger, response: ServerlessResponse, ): void { - log.debug('SERVERLESS_ADAPTER:FORWARD_REQUEST_TO_FRAMEWORK:RESPONSE', { - response, - }); + log.debug( + 'SERVERLESS_ADAPTER:FORWARD_REQUEST_TO_FRAMEWORK:RESPONSE', + () => ({ + response, + }), + ); } /** @@ -197,12 +198,12 @@ export class DefaultHandler< ) { log.debug( 'SERVERLESS_ADAPTER:FORWARD_RESPONSE:EVENT_SOURCE_RESPONSE_PARAMS', - { + () => ({ statusCode, body, headers, isBase64Encoded, - }, + }), ); } diff --git a/test/handlers/default.handler.spec.ts b/test/handlers/default.handler.spec.ts index 130b665c..f00af6bb 100644 --- a/test/handlers/default.handler.spec.ts +++ b/test/handlers/default.handler.spec.ts @@ -16,12 +16,13 @@ describe('DefaultHandler', () => { const resolver = new PromiseResolver(); const binarySettings = { contentEncodings: [], contentTypes: [] }; const respondWithErrors = true; + const executeLog = (m, fn) => typeof fn === 'function' && fn(); const logger: ILogger = { - debug: vitest.fn(), - error: vitest.fn(), - verbose: vitest.fn(), - info: vitest.fn(), - warn: vitest.fn(), + debug: vitest.fn(executeLog), + error: vitest.fn(executeLog), + verbose: vitest.fn(executeLog), + info: vitest.fn(executeLog), + warn: vitest.fn(executeLog), }; it('should forward and return the response from a request with different status', async () => { @@ -63,19 +64,19 @@ describe('DefaultHandler', () => { expect(logger.debug).toHaveBeenNthCalledWith( 3, 'SERVERLESS_ADAPTER:FORWARD_REQUEST_TO_FRAMEWORK:REQUEST_VALUES', - expect.any(Object), + expect.any(Function), ); expect(logger.debug).toHaveBeenNthCalledWith( 4, 'SERVERLESS_ADAPTER:FORWARD_REQUEST_TO_FRAMEWORK:RESPONSE', - expect.any(Object), + expect.any(Function), ); expect(logger.debug).toHaveBeenNthCalledWith( 5, 'SERVERLESS_ADAPTER:FORWARD_RESPONSE:EVENT_SOURCE_RESPONSE_PARAMS', - expect.any(Object), + expect.any(Function), ); expect(logger.debug).toHaveBeenNthCalledWith(