Comparing version 3.0.1 to 3.1.0
20
index.js
@@ -21,3 +21,3 @@ 'use strict' | ||
if (options.logEvents === undefined) { | ||
options.logEvents = ['onPostStart', 'onPostStop', 'response', 'request'] | ||
options.logEvents = ['onPostStart', 'onPostStop', 'response', 'request-error'] | ||
} | ||
@@ -68,4 +68,5 @@ | ||
// log when a request completes with an error | ||
tryAddEvent(server, options, 'on', 'request', function (request, event, tags) { | ||
// log via `request.log()` and optionally when an internal `accept-encoding` | ||
// error occurs or request completes with an error | ||
server.events.on('request', function (request, event, tags) { | ||
if (event.channel === 'internal' && !tags['accept-encoding']) { | ||
@@ -76,7 +77,8 @@ return | ||
request.logger = request.logger || logger.child({ req: request }) | ||
if (event.error) { | ||
if (event.error && isEnabledLogEvent(options, 'request-error')) { | ||
request.logger.warn({ | ||
err: event.error | ||
}, 'request error') | ||
} else { | ||
} else if (event.channel === 'app') { | ||
logEvent(request.logger, event) | ||
@@ -104,5 +106,9 @@ } | ||
function isEnabledLogEvent (options, name) { | ||
return options.logEvents && options.logEvents.indexOf(name) !== -1 | ||
} | ||
function tryAddEvent (server, options, type, event, cb) { | ||
var name = typeof event === 'string' ? event : event.name | ||
if (options.logEvents && options.logEvents.indexOf(name) !== -1) { | ||
if (isEnabledLogEvent(options, name)) { | ||
if (type === 'on') { | ||
@@ -113,3 +119,3 @@ server.events.on(event, cb) | ||
} else { | ||
throw new Error(`unsupporte type ${type}`) | ||
throw new Error(`unsupported type ${type}`) | ||
} | ||
@@ -116,0 +122,0 @@ } |
{ | ||
"name": "hapi-pino", | ||
"version": "3.0.1", | ||
"version": "3.1.0", | ||
"description": "Hapi plugin for the Pino logger ", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -99,3 +99,3 @@ # hapi-pino [](https://travis-ci.org/pinojs/hapi-pino) [](https://coveralls.io/github/pinojs/hapi-pino?branch=master) | ||
log all events e.g. `['onPostStart', 'onPostStop', 'response', 'request-error']`. | ||
Set to `false/null` to disable all events. | ||
Set to `false/null` to disable all events. Even though there is no `request-error` [Hapi Event](#hapievents), the options enables the logging of failed requests. | ||
- `[mergeHapiLogData]` - When enabled, Hapi-pino will merge the data received | ||
@@ -138,6 +138,4 @@ from Hapi's logging interface (`server.log(tags, data)` or `request.log(tags, data)`) | ||
* `'response'`, to log at `'info'` level when a request is completed | ||
* `'request'`, to log at `'warn'` level when a request errors for | ||
`internal` and `accept-encoding` tags | ||
* `'log'`, to support logging via the Hapi `server.log()` and | ||
`request.log()` methods, see `tags` and `allTags` options. | ||
* `'request'`, to support logging via the Hapi `request.log()` method and to log at `'warn'` level when a request errors or when request received contains an invalid `accept-encoding` header, see `tags` and `allTags` options. | ||
* `'log'`, to support logging via the Hapi `server.log()` method and to log in case of an internal server event, see `tags` and `allTags` options. | ||
* `'onPostStart'`, to log when the server is started | ||
@@ -144,0 +142,0 @@ * `'onPostStop'`, to log when the server is stopped |
58
test.js
@@ -561,2 +561,60 @@ 'use strict' | ||
experiment('logging with `request` event listener', () => { | ||
test('with enabled `request-error`', async () => { | ||
const server = getServer() | ||
let done | ||
const finish = new Promise(function (resolve, reject) { | ||
done = resolve | ||
}) | ||
const stream = sink((data) => { | ||
expect(data.err.stack).to.not.be.undefined() | ||
expect(data.err.isBoom).to.be.true() | ||
expect(data.err.output.statusCode).to.be.equal(500) | ||
done() | ||
}) | ||
const logger = require('pino')(stream) | ||
const plugin = { | ||
plugin: Pino, | ||
options: { | ||
instance: logger, | ||
logEvents: ['request-error'] | ||
} | ||
} | ||
await server.register(plugin) | ||
await server.inject({ | ||
method: 'GET', | ||
url: '/error' | ||
}) | ||
await finish | ||
}) | ||
test('with disabled `request-error`', async () => { | ||
const server = getServer() | ||
let called = false | ||
const stream = sink(() => { | ||
called = true | ||
}) | ||
const plugin = { | ||
plugin: Pino, | ||
options: { | ||
stream: stream, | ||
logEvents: false | ||
} | ||
} | ||
await server.register(plugin) | ||
await server.inject({ | ||
method: 'GET', | ||
url: '/error' | ||
}) | ||
expect(called).to.be.false() | ||
}) | ||
}) | ||
experiment('uses a prior pino instance', () => { | ||
@@ -563,0 +621,0 @@ test('without pre-defined serializers', async () => { |
33579
1027
151