@chaudhryjunaid/express-bunyan-logger

1.3.5 • Public • Published

Express-bunyan-logger

A fork of the express logger middleware powered by bunyan. This fork contains fixes and updates that have not been merged upstream because the parent project is unmaintained. In particular, it includes support for node 12 and later. From a functionality point of view, this fork is exactly identical to its parent project.

Build Status

Installation

npm install @chaudhryjunaid/express-bunyan-logger

Usage

To use the logger:

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

To use the errorLogger:

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

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

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

Change default format:

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

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

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

Configuration

options.logger

A custom instance of bunyan logger to use in the middleware. Useful if you already do all of the logging with bunyan and don't want this middleware to create a separate instance. Also useful if you want to pass a child logger into this middleware.

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

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.

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

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

License

(The BSD License)

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

Readme

Keywords

Package Sidebar

Install

npm i @chaudhryjunaid/express-bunyan-logger

Weekly Downloads

209

Version

1.3.5

License

BSD-2-Clause

Unpacked Size

24 kB

Total Files

8

Last publish

Collaborators

  • chaudhryjunaid