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 logResponsePayload option to log response returned value #188

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -120,6 +120,12 @@ events"](#hapievents) section.

When enabled, responses with status codes in the 400-500 range will have the value returned by the hapi lifecycle method added to the `response` event log as `err`.

### `options.logResponsePayload: boolean`

**Default**: `false`

When enabled, add the response returned value (Hapi `request.response.source`) as `responsePayload` to the `response` event log.

### `options.logRequestStart: boolean | (Request) => boolean`

**Default**: false
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ async function register (server, options) {
tags: options.logRouteTags ? request.route.settings.tags : undefined,
err: options.log4xxResponseErrors && (statusCode >= 400 && statusCode < 500) ? request.response.source : undefined,
res: request.raw.res,
responsePayload: options.logResponsePayload && request.response && request.response.source !== undefined ? request.response.source : undefined,
responseTime
},
requestCompleteMessage(request, responseTime)
Expand Down
54 changes: 54 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1587,6 +1587,60 @@ experiment('options.logRequestComplete', () => {
})
})

experiment('options.logResponsePayload', () => {
test('when options.logResponsePayload is true, the response returned value should be added to the log as "responsePayload"', async () => {
const server = getServer()

let done

const finish = new Promise(function (resolve, reject) {
done = resolve
})

server.route({
path: '/',
method: 'GET',
handler: async (req, h) => {
return { foo: 100 }
}
})

await registerWithOptionsSink(server, { level: 'info', logResponsePayload: true }, data => {
expect(data.responsePayload).to.be.equals({ foo: 100 })
done()
})

await server.inject('/')
await finish
})

test('when options.logResponsePayload is omitted, "responsePayload" should not be added to the response event log', async () => {
const server = getServer()

let done

const finish = new Promise(function (resolve, reject) {
done = resolve
})

server.route({
path: '/',
method: 'GET',
handler: async (req, h) => {
return { foo: 101 }
}
})

await registerWithOptionsSink(server, { level: 'info' }, data => {
expect(data.responsePayload).to.be.undefined()
done()
})

await server.inject('/')
await finish
})
})

experiment('logging with mergeHapiLogData option enabled', () => {
test("log event data is merged into pino's log object", async () => {
const server = getServer()
Expand Down