You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

5.4 KiB

Table of Contents

How to use Pino with Express

npm install --save express-pino-logger
var app = require('express')()
var pino = require('express-pino-logger')()

app.use(pino)

app.get('/', function (req, res) {
  req.log.info('something')
  res.send('hello world')
})

app.listen(3000)

See the express-pino-logger readme for more info.

How to use Pino with Hapi

npm install --save hapi-pino
'use strict'

const Hapi = require('hapi')

const server = new Hapi.Server()
server.connection({ port: 3000 })

server.route({
  method: 'GET',
  path: '/',
  handler: function (request, reply) {
    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')

  // Start the server
  server.start((err) => {
    if (err) {
      console.error(err)
      process.exit(1)
    }
  })
})

See the hapi-pino readme for more info.

How to use Pino with Restify

npm install --save restify-pino-logger
var server = require('restify').createServer({name: 'server'})
var pino = require('restify-pino-logger')()

server.use(pino)

server.get('/', function (req, res) {
  req.log.info('something')
  res.send('hello world')
})

server.listen(3000)

See the restify-pino-logger readme for more info.

How to use Pino with koa

Koa v1

npm install --save koa-pino-logger@1
var app = require('koa')()
var pino = require('koa-pino-logger')()

app.use(pino)

app.use(function * () {
  this.log.info('something else')
  this.body = 'hello world'
})

app.listen(3000)

See the koa-pino-logger v1 readme for more info.

Koa v2

npm install --save koa-pino-logger@2
var Koa = require('koa')
var app = new Koa()
var pino = require('koa-pino-logger')()

app.use(pino)

app.use((ctx) => {
  ctx.log.info('something else')
  ctx.body = 'hello world'
})

app.listen(3000)

See the koa-pino-logger v2 readme for more info.

How to use Pino with debug

Capture debug logs in JSON format, at 10x-20x the speed: The popular debug which used in many modules accross the ecosystem.

The pino-debug can captures calls to the debug loggers and run them through pino instead. This results in a 10x (20x in extreme mode) performance improvement, while logging out more information, in the usual JSON format.

The quick start way to enable this is simply to install pino-debug and preload it with the -r flag, enabling any debug logs with the DEBUG environment variable:

$ npm i --save pino-debug
$ DEBUG=* node -r pino-debug app.js

pino-debug also offers fine grain control to map specific debug namespaces to pino log levels. See pino-debug for more.

How do I rotate log files?

Use a separate tool for log rotation: We recommend logrotate. Consider we output our logs to /var/log/myapp.log like so:

> node server.js > /var/log/myapp.log

We would rotate our log files with logrotate, by adding the following to /etc/logrotate.d/myapp:

/var/log/myapp.log {
       su root
       daily
       rotate 7
       delaycompress
       compress
       notifempty
       missingok
       copytruncate
}

How to save to multiple files?

Let's assume you want to store all error messages to a separate log file:

Install pino-tee with:

npm i pino-tee -g

The following writes the log output of app.js to ./all-logs, while writing only warnings and errors to `./warn-log:

node app.js | pino-tee warn ./warn-logs > ./all-logs

How do I redact sensitive information??

Use pino-noir for performant log redaction:

Install and require pino-noir, initialize with the key paths you wish to redact and pass the resulting instance in through the serializers option

var noir = require('pino-noir')
var pino = require('pino')({
  serializers: noir(['key', 'path.to.key'])
})

pino.info({
  key: 'will be redacted',
  path: {
    to: {key: 'sensitive', another: 'thing'}
  },
  more: 'stuff'
})

// {"pid":7306,"hostname":"x","level":30,"time":1475519922198,"key":"[Redacted]","path":{"to":{"key":"[Redacted]","another":"thing"}},"more":"stuff","v":1}

If you have other serializers simply extend:

var noir = require('pino-noir')
var pino = require('pino')({
  serializers: Object.assign(
    noir(['key', 'path.to.key']),
    {myCustomSerializer: () => {}}
})