# bunyan-middleware

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

Latest version **1.0.2** (published 2022-06-22) · MIT license · 0 weekly downloads

## Install

```sh
npm install bunyan-middleware
pnpm add bunyan-middleware
yarn add bunyan-middleware
bun add bunyan-middleware
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2022-06-22 |
| First published | 2013-05-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 3 |
| Unpacked size | 11.8 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 38 |
| Author | Christian Tellnes |
| Maintainers | tellnes |
| Keywords | bunyan, logger, express, request, response, connect, http, duration |

## Links

- npm: https://www.npmjs.com/package/bunyan-middleware
- Repository: https://github.com/tellnes/bunyan-middleware
- Homepage: https://github.com/tellnes/bunyan-middleware#readme
- Issues: https://github.com/tellnes/bunyan-middleware/issues
- npm.io page: https://npm.io/package/bunyan-middleware

## Dependencies (3)

- [uuid](https://npm.io/package/uuid.md) ^8.3.2
- [@types/bunyan](https://npm.io/package/@types/bunyan.md) ^1.8.6
- [@types/express](https://npm.io/package/@types/express.md) ^4.0.35

## Alternatives

- [cli-color](https://npm.io/package/cli-color.md) — 3.4M weekly downloads
- [log](https://npm.io/package/log.md) — 1.3M weekly downloads
- [logstash-client](https://npm.io/package/logstash-client.md) — 4.5K weekly downloads
- [@nocobase/plugin-logger](https://npm.io/package/@nocobase/plugin-logger.md) — 2.0K weekly downloads
- [child-process-debug](https://npm.io/package/child-process-debug.md) — 695 weekly downloads

## Recent versions

- 1.0.2 (latest) — 2022-06-22
- 1.0.1 — 2020-12-08
- 1.0.0 — 2019-07-29
- 0.8.0 — 2017-05-12
- 0.7.0 — 2017-05-12
- 0.6.0 — 2016-12-07
- 0.5.1 — 2016-11-18
- 0.5.0 — 2016-11-04
- 0.4.0 — 2016-08-25
- 0.3.1 — 2016-02-24
- 0.3.0 — 2016-02-24
- 0.2.1 — 2015-09-19
- 0.2.0 — 2015-02-12
- 0.1.0 — 2014-10-15
- 0.0.1 — 2013-05-09

## README

# bunyan-middleware

[![Dependency Status](https://david-dm.org/tellnes/bunyan-middleware.png)](https://david-dm.org/tellnes/bunyan-middleware)
[![devDependency Status](https://david-dm.org/tellnes/bunyan-middleware/dev-status.png)](https://david-dm.org/tellnes/bunyan-middleware#info=devDependencies)

Request, response logger middleware for [bunyan](https://github.com/trentm/node-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()](https://www.npmjs.com/package/uuid#uuidv1options--buffer--offset))
- 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](http://nodejs.org/api/http.html#http_http_createserver_requestlistener), [express](https://github.com/strongloop/express), [connect](https://github.com/senchalabs/connect) and any http middleware system
- uses serializers for `req` and `res` based on [bunyan serializers](https://github.com/trentm/node-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

```shell
yarn add bunyan-middleware
```

or

```shell
npm install bunyan-middleware --save
```

## Usage

```js
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

```ts
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()](https://www.npmjs.com/package/uuid#uuidv1options--buffer--offset)
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

- [bunyan-request](https://github.com/vvo/bunyan-request) - a fork by [vvo](https://github.com/vvo).

- [express-bunyan-logger](https://github.com/villadora/express-bunyan-logger)

---
_Source: https://npm.io/package/bunyan-middleware · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
