hapi-pino

Hapi plugin for the Pino logger. It logs in JSON for easy
post-processing.
It is faster than good console logger by a 20%
factor, which increases to 30% when using extreme
mode. Using hapi-pino in
extreme mode allows the "hello world" example to handle 30% more
throughput than good.
hapi-pino v2.x.x supports Hapi v16.
Install
npm i hapi-pino@2 --save
Usage
'use strict'
const Hapi = require('hapi')
const server = new Hapi.Server()
server.connection({
host: 'localhost',
port: 3000
})
server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
request.log(['a', 'b'], 'Request into hello world')
request.logger.info('In handler %s', request.path)
return reply('hello world')
}
})
server.register(require('hapi-pino'), (err) => {
if (err) {
console.error(err)
process.exit(1)
}
server.logger().info('another way for accessing it')
server.log(['subsystem'], 'third way for accessing it')
server.start((err) => {
if (err) {
console.error(err)
process.exit(1)
}
})
})
API
hapi-pino goal is to enable Hapi applications to log via pino. To enable this, it decorates both the server and the request. Moreover, hapi-pino
binds to the Hapi events system as described in the "Hapi
events" section.
Options
-
[logPayload]
– when enabled, add the request payload as payload
to the response
event log. Defaults to false
.
-
[stream]
- the binary stream to write stuff to, defaults to
process.stdout
.
-
[prettyPrint]
- pretty print the logs (same as node server | pino
), disable in production. Default is false
, enable in
development by passing true
.
-
[tags]
- a map to specify pairs of Hapi log tags and levels. By default,
the tags trace, debug, info, warn, and error map to their
corresponding level. Any mappings you supply take precedence over the default
mappings. The default level tags are exposed via hapi-pino.levelTags
.
-
[allTags]
- the logging level to apply to all tags not matched by
tags
, defaults to 'info'
.
[serializers]
- an object to overwrite the default serializers. You can but don't have to overwrite all of them. E.g. to redact the authorization header in the logs:
{
req: require('pino-noir')(['req.headers.authorization']).req
res: ...
err: ...
}
-
[instance]
- uses a previously created Pino instance as the logger.
The instance's stream
and serializers
take precedence.
-
[logEvents]
- Takes an array of strings with the events to log. Default is to
log all events e.g. ['onPostStart', 'onPostStop', 'response', 'request-error']
.
Set to false/null
to disable all events.
-
[mergeHapiLogData]
- When enabled, Hapi-pino will merge the data received
from Hapi's logging interface (server.log(tags, data)
or request.log(tags, data)
)
into Pino's logged attributes at root level. If data is a string, it will be used as
the value for the msg
key. Default is false
, in which case data will be logged under
a data
key.
E.g.
server.log(['info'], {hello: 'world'})
{ level: 30, hello: 'world', ...}
{ level: 30, data: { hello: 'world' }}
Server Decorations
hapi-pino decorates the Hapi server with:
server.logger()
, which is a function that returns the current instance of
pino, see its doc for the way to actual log.server.app.logger
, same as before, but the logger it is also
attached to the server.app
object.
Request Decorations
hapi-pino decorates the Hapi request with:
request.logger
, which is an instance of pino bound to the current request, so you can trace all the logs of a given request. See pino doc for the way to actual log.
Hapi Events
hapi-pino listens to some Hapi events:
'onRequest'
, to create a request-specific child logger'response'
, to log at 'info'
level when a request is completed'response-error'
, to log at 'warn'
level when a request errors'log'
, to support logging via the Hapi server.log()
and
request.log()
methods, see tags
and allTags
options.'onPostStart'
, to log when the server is started'onPostStop'
, to log when the server is stopped
Acknowledgements
This project was kindly sponsored by nearForm.
License
MIT