Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional support to log path params #164

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ events"](#hapievents) section.

When enabled, add the request query as `queryParams` to the `response` event log.

### `options.logPathParams: boolean`

**Default**: `false`

When enabled, add the request params as `pathParams` to the `response` event log.

### `options.logRouteTags: boolean`

**Default**: `false`
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ declare namespace HapiPino {
interface Options extends pino.LoggerOptions {
timestamp?: boolean | (() => string) | undefined;
logQueryParams?: boolean | undefined;
logPathParams?: boolean | undefined;
logPayload?: boolean | undefined;
logRouteTags?: boolean | undefined;
logRequestStart?: boolean | ((req: Request) => boolean) | undefined;
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ async function register (server, options) {
{
payload: options.logPayload ? request.payload : undefined,
queryParams: options.logQueryParams ? request.query : undefined,
pathParams: options.logPathParams ? request.params : undefined,
tags: options.logRouteTags ? request.route.settings.tags : undefined,
res: request.raw.res,
responseTime
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const server = new Server();
const options: HapiPino.Options = {
timestamp: () => `,"time":"${new Date(Date.now()).toISOString()}"`,
logQueryParams: false,
logPathParams: false,
logPayload: false,
logRouteTags: false,
logRequestStart: false,
Expand Down
36 changes: 36 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ function getServer () {
path: '/',
handler: async (request, h) => 'ok'
},
{
method: 'POST',
path: '/{foo}-{bar}',
handler: async (request, h) => 'ok'
},
{
method: 'GET',
path: '/error',
Expand Down Expand Up @@ -2405,3 +2410,34 @@ experiment('logging with request queryParams', () => {
await done
})
})

experiment('logging with request pathParams', () => {
test('with pre-defined req serializer', async () => {
const server = getServer()
let resolver
const done = new Promise((resolve, reject) => {
resolver = resolve
})
const stream = sink(data => {
expect(data.pathParams).to.equal({ foo: '42', bar: '43' })
resolver()
})
const logger = require('pino')(stream)
const plugin = {
plugin: Pino,
options: {
instance: logger,
logPathParams: true
}
}

await server.register(plugin)

await server.inject({
method: 'POST',
url: '/42-43'
})

await done
})
})