# express-bunyan-logger

> a bunyan logger middleware for express

Latest version **1.3.3** (published 2018-03-24) · BSD license · 0 weekly downloads

## Install

```sh
npm install express-bunyan-logger
pnpm add express-bunyan-logger
yarn add express-bunyan-logger
bun add express-bunyan-logger
```

## Health

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

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.3 |
| Published | 2018-03-24 |
| First published | 2013-08-23 |
| Weekly downloads | 0 |
| License | BSD |
| TypeScript types | separate (@types/express-bunyan-logger) |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 22.8 KB |
| Known vulnerabilities | 0 (+3 in 3 direct dependencies) |
| Install scripts | no |
| GitHub stars | 138 |
| Author | wei.gao |
| Maintainers | ecoutu, kevinburke, marcbachmann, oliversalzburg, sstk-admin, villadora |
| Keywords | express, logger, bunyan |

## Links

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

## Dependencies (5)

- [uuid](https://npm.io/package/uuid.md) ^3.1.0
- [bunyan](https://npm.io/package/bunyan.md) ^1.8.12
- [useragent](https://npm.io/package/useragent.md) ^2.2.1
- [lodash.has](https://npm.io/package/lodash.has.md) ^4.5.2
- [lodash.set](https://npm.io/package/lodash.set.md) ^4.3.2

## 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.3.3 (latest) — 2018-03-24
- 1.3.2 — 2017-05-07
- 1.3.1 — 2016-08-04
- 1.3.0 — 2016-06-01
- 1.2.0 — 2015-10-30
- 1.1.1 — 2015-02-05
- 1.1.0 — 2014-09-03
- 1.0.0 — 2014-08-11
- 0.2.2 — 2014-07-09
- 0.2.1 — 2014-07-07
- 0.2.0 — 2014-07-05
- 0.1.4 — 2014-07-01
- 0.1.3 — 2014-06-16
- 0.1.2 — 2014-05-06
- 0.1.1 — 2014-04-18
- … 5 more at https://npm.io/package/express-bunyan-logger/versions

## README

# Express-bunyan-logger

A express logger middleware powered by [bunyan](https://github.com/trentm/node-bunyan).

[![Build Status](https://travis-ci.org/villadora/express-bunyan-logger.svg?branch=master)](https://travis-ci.org/villadora/express-bunyan-logger) [![dependencies](https://david-dm.org/villadora/express-bunyan-logger.svg)](https://david-dm.org/villadora/express-bunyan-logger)


## Note

This year as work content change, I have no spare time to maintaining the node modules, if anyone want to take or keep maintaining, just contact me via jky239@gmail.com with Title contains: "Wanted: npm package xxxx". Thx.

## Installation

    npm install express-bunyan-logger

## Usage

To use the logger:

    app.use(require('express-bunyan-logger')());

To use the errorLogger:

    app.use(require('express-bunyan-logger').errorLogger());

And you can also pass bunyan logger options to the logger middleware:

    app.use(require('express-bunyan-logger')({
        name: 'logger',
        streams: [{
            level: 'info',
            stream: process.stdout
            }]
        }));

Change default format:

    app.use(require('express-bunyan-logger')({
        format: ":remote-address - :user-agent[major] custom logger"
    });

And a child logger will be attached to each request object:

```javascript
app.use(require('express-bunyan-logger')();
app.use(function(req, res, next) {
    req.log.debug('this is debug in middleware');
    next();
});
```

## Configuration

### options.format

Format string, please go the source code to the metadata. ":name" will print out meta.name; ":name[key]" will print out the property 'key' of meta.name.

Or you can pass a function to _options.format_. This function accept a object as argument and return string.

### options.parseUA

Whether to parse _user-agent_ in logger, default is =true=.

### options.levelFn

Function that translate statusCode into log level. The `meta` argument is an object consisting of all the fields gathered by bunyan-express-logger, before exclusions are applied.

```
function(status, err /* only will work in error logger */, meta) {
     // return string of level
     if (meta["response-time"] > 30000) {
         return "fatal";
     } else {
         return "info";
     }
}
```

### options.includesFn

Function that is passed `req` and `res`, and returns an object whose properties will be added to the meta object passed to bunyan

```javascript
function(req, res) {
    if (req.user) {
        return {
            _id: req.user._id,
            name: req.user.name
        }
    }
}
```

### options.excludes

Array of string, Those fields will be excluded from meta object which passed to bunyan

### options.obfuscate
Array of strings to obfuscate.
These strings can be in dotted notation, for instance `body.password`, and it will only replace that specific value.
This will replace the values in log messages with a [placeholder](#optionsobfuscateplaceholder).

### options.obfuscatePlaceholder

Placeholder to use when obfuscating values.
This is only applicable when there are values to obfuscate.
Default is `[HIDDEN]`.

### options.serializers

An object of [bunyan serializers](https://github.com/trentm/node-bunyan#serializers). They are passed on to bunyan.
The default serializers are defined as follows:
```
{
    req: bunyan.stdSerializers.req,
    res: bunyan.stdSerializers.res,
    err: bunyan.stdSerializers.err
}
```

### options.immediate

Write log line on request instead of response (for response times)

### options.genReqId

By default, `express-bunyan-logger` will generate an unique id for each request, and a field 'req_id' will be added to child logger in `request` object.

If you have already use other middleware/framework to generate request id, you can pass a function to retrieve it:

```javascript
// suppose connect-requestid middleware is already added.
app.use(require('express-bunyan-logger')({
    genReqId: function(req) {
       return req.id;
    }
});
```


## License

(The BSD License)

    Copyright (c) 2013, Villa.Gao <jky239@gmail.com>;

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