npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-mw-bunyan

v4.0.0

Published

express middleware that implements bunyan as a logger

Downloads

2,746

Readme

express-mw-bunyan

express middleware that implements bunyan as a logger to be used with req.log

Build StatusCoverage StatusISC LicenseNodeJS

JavaScript Style Guide

api

const logger = require('express-mw-bunyan')

logger(
  bunyan createLogger object[,
  origin = default 'request'][,
  headerName = default 'X-Request-ID']
)

for more options to configure bunyan.createLogger check with bunyan

Notes:

  • should use a middleware like express-mw-correlation-id to generate the req.id if not express-mw-bunyan will generate the req.id itself by using uuid.v4
  • if you want to set a different headerName but still use the default origin you should pass undefined in the origin place
  • by default Bunyan intentionally do not include the request/response body if you want to include that you need to change the req and res serializers

example

const express = require('express')
const bunyan  = require('bunyan')

// instead of using `express-mw-correlation-id` can use the internal uuid
// {now}:{hostname}:{pid}:{conn creation(aprox.) base36}:{8 digit counter}
const setRequestId = require('express-mw-correlation-id')

// to capture the body/payload
const bodyParser = require('body-parser')



const logger = require('express-mw-bunyan')

// middlewares
app.use(bodyParser.json())
app.use(setRequestId())
app.use(logger(bunyan.createLogger({ name: 'test' })), 'request_from_my_code')

// `req.log` will be exposed with all log methods from bunyan

app.get('/', (req, res) => {
  req.log.info('getting Hello World')
  res.send('Hello World')
})

app.post('/', (req, res) => {
  req.log.info('Posting something')
  res.status(201).send()
})

app.listen(3000)

example of the initial request

{
  "name": "test",
  "hostname": "m-quim.local",
  "pid": 2695,
  "origin": "request_from_my_code",
  "req_id": "825efe65-a27d-4dca-936b-e74249095fb7",// REQUEST ID
  "level": 30,
  "req": {
    "method": "POST",
    "url": "/",
    "headers": {
      "host": "127.0.0.1:63135",
      "accept-encoding": "gzip, deflate",
      "user-agent": "node-superagent/2.3.0",
      "content-type": "application/json",
      "content-length": "7",
      "connection": "close"
    },
    "remoteAddress": "::ffff:127.0.0.1",
    "remotePort": 63136
  },
  "msg": "start of the request",
  "time": "2016-12-10T13:22:56.639Z",
  "v": 0
}

example of the end reponse

{
  "name": "test",
  "hostname": "m-quim.local",
  "pid": 2695,
  "origin": "request_from_my_code",
  "req_id": "825efe65-a27d-4dca-936b-e74249095fb7",// REQUEST ID
  "level": 30,
  "res": {
    "statusCode": 201,
    "header": "HTTP/1.1 201 Created\r\nX-Powered-By: Express\r\nX-Request-ID: 1481376176639:127.0.0.1:2695:iwj94lbz:10000002\r\nDate: Sat, 10 Dec 2016 13:22:56 GMT\r\nConnection: close\r\nContent-Length: 0\r\n\r\n"
  },
  "duration": 1.337593,// DURATION APROX. OF THE OPERATION IN MS
  "msg": "end of the response",
  "time": "2016-12-10T13:22:56.640Z",
  "v": 0
}

ISC License (ISC)