Skip to content

Commit

Permalink
Merge pull request #4 from well-known-components/feat/log-query-param…
Browse files Browse the repository at this point in the history
…eters-and-path

feat: Log the query parameters of a request as well
  • Loading branch information
LautaroPetaccio authored Mar 8, 2023
2 parents 256d1c8 + 5546a78 commit 3bde0df
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
8 changes: 6 additions & 2 deletions src/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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}]`
)
}
}
Expand Down
12 changes: 8 additions & 4 deletions test/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ beforeEach(() => {
mockedContext = {
request: {
method: 'GET',
url: 'http://localhost/some-endpoint'
url: 'http://localhost/some-endpoint?someParameter=1'
} as IHttpServerComponent.DefaultContext<object>['request'],
url: new URL('http://localhost/some-endpoint')
url: new URL('http://localhost/some-endpoint?someParameter=1')
}
mockedResponse = {
headers: {
Expand Down Expand Up @@ -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]`
)
})
})

Expand All @@ -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]`
)
})
})
})

0 comments on commit 3bde0df

Please sign in to comment.