• Stars
    star
    139
  • Rank 262,954 (Top 6 %)
  • Language
    JavaScript
  • Created over 11 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

bunyan logger middleware for express

Express-bunyan-logger

A express logger middleware powered by bunyan.

Build Status dependencies

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 [email protected] 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:

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

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('express-bunyan-logger')({
    genReqId: function(req) {
       return req.id;
    }
});

License

(The BSD License)

Copyright (c) 2013, Villa.Gao <[email protected]>;

More Repositories

1

express-http-proxy

Proxy middleware for express/connect
JavaScript
1,221
star
2

cayley.js

Nodejs Client for Cayley
JavaScript
57
star
3

helium-cli

A tool to detect unused CSS across multiple web pages
JavaScript
45
star
4

node-hessian

Hessian Client for nodejs
JavaScript
44
star
5

multi-map

Map that can contains multiple values for the same key
JavaScript
17
star
6

node-buffer-reader

A Buffer Reader to make reading data from binary buffer easily
JavaScript
14
star
7

node-couchdb

An extendable couch client lib for nodejs
JavaScript
10
star
8

polymer-builder

Desktop version of polymer-designer via atom-shell https://github.com/polymer/designer
CSS
7
star
9

DeobfuscateJs

JavaScript
7
star
10

semver.version

semantic versioning lib for java
Java
6
star
11

java-class-parser

a wrapper of 'javap' to read java class info from '.class' file
JavaScript
6
star
12

node-obj-validator

Build validator to validate JSON Object
JavaScript
3
star
13

jsr

A javascript compilation framework
JavaScript
2
star
14

google-auth-cli

Get google auth from command line
JavaScript
2
star
15

emacs-config

emacs config
Emacs Lisp
2
star
16

jsdoc-i18n-plugin

Plugin for jsdoc to handle i18n in in-file documents.
JavaScript
2
star
17

node-fixVer

Fix versions in package.json
JavaScript
2
star
18

require2commonjs

Convert your RequireJS file to CommonJS module format
JavaScript
2
star
19

cairngorm3

cairngorm3 for parsley-spicelib 3.0 (based on cairngorm3-3.0.21)
ActionScript
1
star
20

express-courier

A express add-on to let you manage your route files
JavaScript
1
star
21

read-only

JavaScript
1
star
22

gen-graph

JavaScript
1
star
23

hexrgb

JavaScript
1
star
24

dotfiles

My dotfiles configuration
Shell
1
star
25

ranks

Find the best package from the registry!
JavaScript
1
star
26

connect-composite

Composite middlewares
JavaScript
1
star
27

balance

A simple game for remote control via cell phone, built on socket.io and device motion
JavaScript
1
star
28

gcal

Command Line Tool for google calendar
JavaScript
1
star