Skip to content

Commit

Permalink
perf(default-handler): always log using fn
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Jun 7, 2023
1 parent 78b9f18 commit 36950b3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
21 changes: 11 additions & 10 deletions src/handlers/default/default.handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
},
}),
);
}

Expand All @@ -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,
}),
);
}

/**
Expand All @@ -197,12 +198,12 @@ export class DefaultHandler<
) {
log.debug(
'SERVERLESS_ADAPTER:FORWARD_RESPONSE:EVENT_SOURCE_RESPONSE_PARAMS',
{
() => ({
statusCode,
body,
headers,
isBase64Encoded,
},
}),
);
}

Expand Down
17 changes: 9 additions & 8 deletions test/handlers/default.handler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 36950b3

Please sign in to comment.