JSPM

  • Created
  • Published
  • Downloads 75449
  • Score
    100M100P100Q156146F
  • License MIT

Hapi plugin for the Pino logger

Package Exports

  • hapi-pino

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hapi-pino) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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 25% factor, which increases to 40% when using extreme mode). Using hapi-pino in extreme mode allows the "hello world" example to handle 40% more throughput than good.

Install

npm i hapi-pino --save

Usage

'use strict'

const Hapi = require('hapi')

// Create a server with a host and port
const server = new Hapi.Server()
server.connection({
  host: 'localhost',
  port: 3000
})

// Add the route
server.route({
  method: 'GET',
  path: '/',
  handler: function (request, reply) {
    // request.log is HAPI standard way of logging
    request.log(['a', 'b'], 'Request into hello world')

    // you can also use a pino instance, which will be faster
    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)
  }

  // the logger is available in server.app
  server.app.logger.warn('Pino is registered')

  // also as a decorated API
  server.logger().info('another way for accessing it')

  // and through Hapi standard logging system
  server.log(['subsystem'], 'third way for accessing it')

  // Start the server
  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

  • [stream] - the binary stream to write stuff to, defaults to process.stdout.
  • [tags] - a map to specify pairs of Hapi log tags and levels.
  • [allTags] - the logging level to apply to all tags not matched by tags, defaults to 'info'.

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
  • 'onPostStopt', to log when the server is stopped

Acknowledgements

This project was kindly sponsored by nearForm.

License

MIT