Skip to content

Commit

Permalink
Merge 27b2751 into 2780650
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathansamines committed May 26, 2022
2 parents 2780650 + 27b2751 commit ef46149
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
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
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
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
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
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
})
})

0 comments on commit ef46149

Please sign in to comment.