Code Monkey home page Code Monkey logo

bunyan-middleware's Introduction

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

bunyan-middleware's People

Contributors

ch2ch3 avatar dmaretskyi avatar iphands avatar joshhunt avatar kingcody avatar msmol avatar mylesrankin avatar orthographic-pedant avatar paroga avatar phillipj avatar tellnes avatar tikotzky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

bunyan-middleware's Issues

Feat: Option to include headers

I would like to have an option for specifically including headers. This would give better control over the headers that are logged.

If you like, i can file a PR for this.

Removing obsureHeaders from log at all

Hi Christian,
wouldn't be good idea to completely remove headers defined in obsureHeaders from the request? At this time, they are set to null, which creates unnecessary long and not-so-good readable bunyan-cli logs:

GET /js/homeController.min.js HTTP/1.1
    host: localhost:3030
    connection: null
    cache-control: null
    accept: */*
    if-none-match: null
    if-modified-since: Tue, 17 Feb 2015 19:56:25 GMT
    user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
    referer: http://localhost:3030/
    accept-encoding: gzip, deflate, sdch
    accept-language: null
    x-powered-by: null
    x-request-id: null

This output is produced by line:

headers[ obscureHeaders[i] ] = null

I suggest to replace it with:

delete headers[ obscureHeaders[i] ]

Best regards,
Jiri

Is this package working?

I've made a very simple example and I'm failling to make it work.

"express": "4.14.0",
"bunyan": "1.8.12",
"bunyan-middleware": "0.8.0",
bunyan.createLogger({ name: 'my-app'})
app.use(bunyanMiddleware({ logger }))

When I run the app i get:


TypeError: Cannot read property 'res' of undefined
    at module.exports (/code/test/node_modules/bunyan-middleware/index.js:77:33)
    at Object.<anonymous> (/code/test/server/server.js:22:9)
    at Module._compile (module.js:573:30)
    at loader (/code/test/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/code/test/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Module.require (module.js:517:17)
    at require (internal/module.js:11:18)

I can see the error is here:

, res: logger.serializers && logger.serializers.res || logger.constructor.stdSerializers.res

And from what I see my logger has:

logger.default.serializer: null

So the bunyan-middleware package is not counting on that default key.
I'm doing something wrong? Is bunyan-middleware not expected to work with this version of bunyan?

Reasoning behind switching from UUID v4 to UUID v1?

In the move from node-uuid to uuid there was also the move from v4 to v1 UUID:s โ€“ what was the reasoning behind this? Doesn't uuid support v4 very well? To me v4 seems generally preferable to v1.

A quick googling shows others prefering v4 over v1 as well, like http://stackoverflow.com/a/20342413/20667 and also Wikipedia mentioning critique against v1: https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_1_.28date-time_and_MAC_address.29

Plan for 1.0.0 release?

With the two latest version being backwards compatible with just some new features it feels like this project is getting fairly mature and that it may be time for a 1.0.0 release?

As any 0.x release from a Semantic Versioning perspective is considered to be breaking the release of a 1.0.0 would better communicate the nature of new releases and make it easier to automatically keep up to date with this module.

TypeScript: no default export

I'm new to TypeScript, so please bear with me a little. I can't seem to figure out what's going on, but when trying to compile a Node app with bunyan-middlewareare (under TSC 2.5.3) I'm getting the following error:

error TS1192: Module '"/path/to/project/node_modules/bunyan-middleware/index"' has no default export.

Any idea why that might be? I've been digging through TS definition docs and it seems like this should work just fine, but the TSC disagrees.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.