bunyan-middleware

Request, response logger middleware using bunyan. Also provides request<>response duration.

bunyan-middleware

npm install bunyan-middleware --save

Request, response logger middleware for bunyan:

  • log request as req
  • log response as res
  • log response close events as warnings
  • log request<>response duration in milliseconds as duration
  • creates, use and forward to response the x-request-id request header: get it if present, create it otherwise (uuid.v4())
  • log request id as req_id and exposes it as req.reqId
  • provides req.log and res.log as an id-specialized logger for you to track your request in your entire application, every time you access the request or response object
  • compatible with pure http server, express, connect and any http middleware system
  • uses serializers for req and res based on bunyan serializers if you do not already have a serializer defined.
  • obscure headers containing sensitive information in log outputs (configureable with obscureHeaders)
npm install bunyan-middleware --save
var bunyan = require('bunyan')
  , bunyanMiddleware = require('bunyan-middleware')
  , express = require('express')
 
var app = express()
var logger = bunyan.createLogger({ name: 'My App' })
 
app.use(bunyanMiddleware(
    { headerName: 'X-Request-Id'
    , propertyName: 'reqId'
    , logName: 'req_id'
    , obscureHeaders: []
    , logger: logger
    }
  )
 
app.get('/', function (reqres) {
  // now use `req.log` as your request-specialized bunyan logger 
  req.log.info('YO DAWG!')
  res.send('ok')
})

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

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

The request id is also available as req.reqId.

logger REQUIRED

  • The bunyan logger instance.

headerName Default: 'X-Request-Id'

  • The name of the HTTP header for the request id.

propertyName Default: 'reqId'

  • The name for the property on the request object to set the request id.

logName Default: 'req_id'

  • The name for the request id in the log output.

obscureHeaders Default: null

  • Set to an array with header nams to remove them from log output.

  • Eg: [ 'Authorization' ]

MIT. See the LICENCE file.