Home

Awesome

bunyan-middleware

Dependency Status devDependency Status

Request, response logger middleware for bunyan:

Install

yarn add bunyan-middleware

or

npm install bunyan-middleware --save

Usage

const bunyan = require('bunyan')
const bunyanMiddleware = require('bunyan-middleware')
const express = require('express')

const app = express()
const logger = bunyan.createLogger({ name: 'My App' })

app.use(bunyanMiddleware(
    { headerName: 'X-Request-Id'
    , propertyName: 'reqId'
    , logName: 'req_id'
    , obscureHeaders: []
    , logger: logger
    , additionalRequestFinishData: function(req, res) {
        return { example: true }
      }
    }
  )

app.get('/', function (req, res) {
  // now use `req.log` as your request-specialized bunyan logger
  req.log.info('YO DAWG!')
  res.send('ok')
})

Import using TypeScript

import bunyanMiddleware = require('bunyan-middleware')

X-Request-Id

Will use and forward X-Request-Id (case insensitive) header when present.

Otherwise it will generate a uuid.v1() and add it to the response headers.

The request id is also available as req.reqId.

Express and mounted apps

If you are using this with express and mounted app which rewrites req.url and you are using bunyan.serializers.req, then the url in the log output will be the rewritten url. To fix that bunyan-middleware is using its own request serializer instead of the default one which is using req.originalUrl instead.

Options

logger REQUIRED

headerName Default: 'X-Request-Id'

propertyName Default: 'reqId'

additionalRequestFinishData Default: undefined

filter Default: undefined

logName Default: 'req_id'

level Default: 'info'

obscureHeaders Default: null

excludeHeaders Default: null

requestStart Default: false

verbose Default: false

License

MIT. See the LICENCE file.

See Also