Skip to content

Commit

Permalink
Add support to replace default serialalizers unwrapped (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathansamines committed Jun 10, 2022
1 parent d313bdb commit 3f9d036
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
23 changes: 23 additions & 0 deletions README.md
Expand Up @@ -192,6 +192,29 @@ events"](#hapievents) section.
}
```

### `options.wrapSerializers: boolean`

**Default**: `true`

When `false`, custom serializers will be passed the raw value directly.

**Example**:
If you prefer to work with the raw value directly, or you want to honor the custom serializers already defined by `options.instance`, you can pass in `options.wrapSerializers` as `false`:

```js
{
wrapSerializers: false,
serializers: {
req (req) {
// `req` is the raw hapi's `Request` object, not the already serialized request from `pino.stdSerializers.req`.
return {
message: req.foo
};
}
}
}
```

### `options.instance: Pino`

Uses a previously created Pino instance as the logger.
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Expand Up @@ -31,6 +31,7 @@ declare namespace HapiPino {
allTags?: pino.Level | undefined;
instance?: pino.Logger | undefined;
logEvents?: string[] | false | null | undefined;
wrapSerializers: boolean | undefined;
mergeHapiLogData?: boolean | undefined;
ignorePaths?: string[] | undefined;
ignoreTags?: string[] | undefined;
Expand Down
9 changes: 7 additions & 2 deletions index.js
Expand Up @@ -29,9 +29,14 @@ async function register (server, options) {
})

options.serializers = options.serializers || {}
options.serializers.req = stdSerializers.wrapRequestSerializer(options.serializers.req || stdSerializers.req)
options.serializers.res = stdSerializers.wrapResponseSerializer(options.serializers.res || stdSerializers.res)

const wrapSerializers = 'wrapSerializers' in options ? options.wrapSerializers : true
const reqSerializer = options.serializers.req || stdSerializers.req
const resSerializer = options.serializers.res || stdSerializers.res

options.serializers.err = options.serializers.err || pino.stdSerializers.err
options.serializers.req = wrapSerializers ? stdSerializers.wrapRequestSerializer(reqSerializer) : reqSerializer
options.serializers.res = wrapSerializers ? stdSerializers.wrapResponseSerializer(resSerializer) : resSerializer

if (options.logEvents === undefined) {
options.logEvents = ['onPostStart', 'onPostStop', 'response', 'request-error']
Expand Down
1 change: 1 addition & 0 deletions index.test-d.ts
Expand Up @@ -26,6 +26,7 @@ const options: HapiPino.Options = {
fatal: 'fatal',
},
allTags: 'info',
wrapSerializers: false,
serializers: {
req: (req: any) => console.log(req),
},
Expand Down
69 changes: 69 additions & 0 deletions test.js
Expand Up @@ -1665,6 +1665,40 @@ experiment('custom serializers', () => {
await finish
})

test('logging with configured req serializer (unwrapped)', async () => {
const server = getServer()
let done
const finish = new Promise(function (resolve, reject) {
done = resolve
})
const stream = sink(data => {
expect(data.req.path).to.equal('/')
expect(data.req.raw).to.be.an.object()

expect(data.res).to.be.an.object()
expect(data.res.statusCode).to.be.equal(404)
done()
})
const logger = require('pino')(stream)
const plugin = {
plugin: Pino,
options: {
instance: logger,
wrapSerializers: false,
serializers: {
req: req => ({ path: req.path, raw: req })
}
}
}

await server.register(plugin)
await server.inject({
method: 'GET',
url: '/'
})
await finish
})

test('logging with configured res serializer', async () => {
const server = getServer()
let done
Expand Down Expand Up @@ -1695,6 +1729,41 @@ experiment('custom serializers', () => {
await finish
})

test('logging with configured res serializer (unwrapped)', async () => {
const server = getServer()
let done
const finish = new Promise(function (resolve, reject) {
done = resolve
})
const stream = sink(data => {
expect(data.req).to.be.an.object()
expect(data.req.url).to.be.equal('/')

expect(data.res.code).to.equal(404)
expect(data.res.headersFlushed).to.equal(true)
expect(data.res.raw).to.be.an.object()
done()
})
const logger = require('pino')(stream)
const plugin = {
plugin: Pino,
options: {
instance: logger,
wrapSerializers: false,
serializers: {
res: res => ({ code: res.statusCode, headersFlushed: res.headersSent, raw: res })
}
}
}

await server.register(plugin)
await server.inject({
method: 'GET',
url: '/'
})
await finish
})

test('logging with pre-defined err serializer', async () => {
const server = getServer()
let done
Expand Down

0 comments on commit 3f9d036

Please sign in to comment.