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 option to log 4xx error details in request logs #182

Merged
merged 1 commit into from
May 8, 2023
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ events"](#hapievents) section.

When enabled, add the request route tags (as configured in hapi `route.options.tags`) `tags` to the `response` event log.

### `options.log4xxResponseErrors: boolean`

**Default**: `false`

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.logRequestStart: boolean | (Request) => boolean`

**Default**: false
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ declare namespace HapiPino {
logPathParams?: boolean | undefined;
logPayload?: boolean | undefined;
logRouteTags?: boolean | undefined;
log4xxResponseErrors?: boolean | undefined;
logRequestStart?: boolean | ((req: Request) => boolean) | undefined;
logRequestComplete?: boolean | ((req: Request) => boolean) | undefined;
customRequestStartMessage?: ((req: Request) => string) | undefined;
Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ async function register (server, options) {

if (shouldLogRequestComplete(request)) {
const info = request.info
const statusCode = request.response.statusCode
if (!request.logger) {
const childBindings = getChildBindings(request)
request.logger = logger.child(childBindings)
Expand All @@ -184,6 +185,7 @@ async function register (server, options) {
queryParams: options.logQueryParams ? request.query : undefined,
pathParams: options.logPathParams ? request.params : undefined,
tags: options.logRouteTags ? request.route.settings.tags : undefined,
err: options.log4xxResponseErrors && (statusCode >= 400 && statusCode < 500) ? request.response.source : undefined,
res: request.raw.res,
responseTime
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
],
"license": "MIT",
"devDependencies": {
"@hapi/boom": "^10.0.1",
"@hapi/code": "^9.0.0",
"@hapi/hapi": "^21.0.0",
"@hapi/lab": "^25.0.0",
Expand Down
53 changes: 39 additions & 14 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const afterEach = lab.afterEach
const expect = Code.expect

const Hapi = require('@hapi/hapi')
const Boom = require('@hapi/boom')
const Pino = require('.')

function getServer () {
Expand Down Expand Up @@ -61,31 +62,25 @@ function sink (func) {
return result
}

async function registerWithSink (server, level, func) {
async function registerWithOptionsSink (server, options, func) {
const stream = sink(func)
const plugin = {
plugin: Pino,
options: {
stream,
level
...options,
stream
}
}

await server.register(plugin)
}

async function tagsWithSink (server, tags, func) {
const stream = sink(func)
const plugin = {
plugin: Pino,
options: {
stream,
level: 'trace',
tags
}
}
async function registerWithSink (server, level, func) {
await registerWithOptionsSink(server, { level }, func)
}

await server.register(plugin)
async function tagsWithSink (server, tags, func) {
await registerWithOptionsSink(server, { level: 'trace', tags }, func)
}

function onHelloWorld (data) {
Expand Down Expand Up @@ -549,6 +544,36 @@ experiment('logs each request', () => {
await server.inject('/')
await finish
})

test('logs 4xx level error details', 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 Boom.badRequest('invalid request')
}
})

await registerWithOptionsSink(server, { level: 'info', log4xxResponseErrors: true }, data => {
expect(data.res.statusCode).to.equal(400)
expect(data.err.stack).to.not.be.undefined()
expect(data.err.error).to.match(/Bad Request/)
expect(data.err.statusCode).to.equal(400)
expect(data.err.message).to.match(/invalid request/)
done()
})

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

experiment('logs through server.log', () => {
Expand Down