Skip to content

Commit

Permalink
add option to log 4xx error details in request logs (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffmhastings committed May 8, 2023
1 parent 34f1721 commit 06b4613
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 14 deletions.
6 changes: 6 additions & 0 deletions README.md
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
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
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
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
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

0 comments on commit 06b4613

Please sign in to comment.