From 5546a781e635b2680672eb3ea6582d44dfd45e42 Mon Sep 17 00:00:00 2001 From: Lautaro Petaccio Date: Wed, 8 Mar 2023 15:36:38 -0300 Subject: [PATCH] feat: Log the query parameters of a request as well --- README.md | 4 ++-- src/component.ts | 8 ++++++-- test/component.spec.ts | 12 ++++++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index d91ba7a..a507c92 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,8 @@ This set up, alongside the default configurations, will produce the following lo The component allows the following configurations via a config parameter: - verbosity: The verbosity on which the logs will be outputted. Defaults to INFO. -- inputLog: A customizable function that defines how the input log will be outputted. Defaults to outputting `[$method: $path]`. -- outputLog: A customizable function that defines how the output log will be outputted. Defaults to outputting `[$method: $path][$status]`. +- inputLog: A customizable function that defines how the input log will be outputted. Defaults to outputting `[$method: $path$search$hash]`. +- outputLog: A customizable function that defines how the output log will be outputted. Defaults to outputting `[$method: $path$search$hash][$status]`. - skipInput: A flag to disable the outputting of the input log. - skipOutput: A flag to disable the outputting of the output log. - skip: A flexible parameter to define how to skip the logging of endpoints. Defaults to skipping the `/health/live` endpoint. diff --git a/src/component.ts b/src/component.ts index 125fb69..75f159a 100644 --- a/src/component.ts +++ b/src/component.ts @@ -21,7 +21,9 @@ export function instrumentHttpServerWithRequestLogger( // Skip health checks by default const skip = shouldSkip(ctx, config?.skip ?? [HEALTH_LIVE, HEALTH_READY]) - const inLog = config?.inputLog ? config.inputLog(ctx.request) : `[${ctx.request.method}: ${ctx.url.pathname}]` + const inLog = config?.inputLog + ? config.inputLog(ctx.request) + : `[${ctx.request.method}: ${ctx.url.pathname}${ctx.url.search}${ctx.url.hash}]` if (!skipInput && !skip) { inLogger[verbosity](inLog) } @@ -47,7 +49,9 @@ export function instrumentHttpServerWithRequestLogger( } finally { if (!skipOutput && !skip && response) { outLogger[verbosity]( - config?.outputLog ? config.outputLog(ctx.request, response) : `[${ctx.request.method}: ${ctx.url.pathname}][${response.status}]` + config?.outputLog + ? config.outputLog(ctx.request, response) + : `[${ctx.request.method}: ${ctx.url.pathname}${ctx.url.search}${ctx.url.hash}][${response.status}]` ) } } diff --git a/test/component.spec.ts b/test/component.spec.ts index 3625ce0..ea31d7b 100644 --- a/test/component.spec.ts +++ b/test/component.spec.ts @@ -38,9 +38,9 @@ beforeEach(() => { mockedContext = { request: { method: 'GET', - url: 'http://localhost/some-endpoint' + url: 'http://localhost/some-endpoint?someParameter=1' } as IHttpServerComponent.DefaultContext['request'], - url: new URL('http://localhost/some-endpoint') + url: new URL('http://localhost/some-endpoint?someParameter=1') } mockedResponse = { headers: { @@ -284,7 +284,9 @@ describe('when any of the following middlewares fail', () => { it('should log the output correctly based on the status code of the exception and propagate the error', async () => { await expect(storedMiddleware(mockedContext, mockedNext)).rejects.toEqual(error) - expect(loggers[1]?.info).toHaveBeenCalledWith(`[${mockedContext.request.method}: ${mockedContext.url.pathname}][400]`) + expect(loggers[1]?.info).toHaveBeenCalledWith( + `[${mockedContext.request.method}: ${mockedContext.url.pathname}${mockedContext.url.search}${mockedContext.url.hash}][400]` + ) }) }) @@ -297,7 +299,9 @@ describe('when any of the following middlewares fail', () => { it('should log the output correctly using the status code as 200', async () => { await expect(storedMiddleware(mockedContext, mockedNext)).rejects.toEqual(error) - expect(loggers[1]?.info).toHaveBeenCalledWith(`[${mockedContext.request.method}: ${mockedContext.url.pathname}][200]`) + expect(loggers[1]?.info).toHaveBeenCalledWith( + `[${mockedContext.request.method}: ${mockedContext.url.pathname}${mockedContext.url.search}${mockedContext.url.hash}][200]` + ) }) }) })