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

apply default serializer.req before using configured serializer.req #34

Closed
wants to merge 4 commits into from
Closed
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
9 changes: 8 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ const levelTags = {

async function register (server, options) {
options.serializers = options.serializers || {}
options.serializers.req = options.serializers.req || asReqValue
if (options.serializers.req) {
const origReqSerializer = options.serializers.req
options.serializers.req = (req) => {
return origReqSerializer(asReqValue(req))
}
} else if (options.serializers.req !== null) {
options.serializers.req = asReqValue
}
options.serializers.res = options.serializers.res || pino.stdSerializers.res
options.serializers.err = options.serializers.err || pino.stdSerializers.err

Expand Down
32 changes: 31 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,37 @@ experiment('logging with overridden serializer', () => {
options: {
instance: logger,
serializers: {
req: (req) => ({ uri: req.raw.req.url })
req: (req) => ({ uri: req.url })
}
}
}

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

test('with req serializer set to null', async () => {
const server = getServer()
let done
const finish = new Promise(function (resolve, reject) {
done = resolve
})
const stream = sink((data) => {
expect(data.req.uri).to.not.equal('/')
expect(data.req.raw).to.be.an.object()
done()
})
const logger = require('pino')(stream)
const plugin = {
plugin: Pino,
options: {
instance: logger,
serializers: {
req: null
}
}
}
Expand Down