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 support to replace default serialalizers unwrapped #167

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
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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