New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

bunyan-middleware

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bunyan-middleware

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

  • 1.0.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

bunyan-middleware

Dependency Status devDependency Status

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.v1())
  • 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 (configurable with obscureHeaders)
  • TypeScript support

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

  • 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.

additionalRequestFinishData Default: undefined

  • A function receiving req and res as arguments returning an object. The elements in the returned object will be added to the fields in the request finish message.

filter Default: undefined

  • A function receiving req and res as arguments returning a boolean. If this functions return value is truthy it will skip all logging for this request/response.

logName Default: 'req_id'

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

level Default: 'info'

  • At which log level request start and request finish should be logged.

obscureHeaders Default: null

  • Set to an array with header names to hide header values from log output. The output will still show header names, with value set to null.

  • Eg: [ 'Authorization' ]

excludeHeaders Default: null

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

  • Eg: [ 'Authorization' ]

requestStart Default: false

  • Log the start of the request.

verbose Default: false

  • Log req and res for request start and request finish.

License

MIT. See the LICENCE file.

See Also

Keywords

FAQs

Package last updated on 22 Jun 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc