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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@cork-labs/http-middleware-logger

v0.5.0

Published

Express middleware, adds a configurable child logger to each request.

Downloads

37

Readme

HTTP Middleware Logger

Express middleware, adds a child logger to each request.

Getting Started

npm install --save @cork-labs/http-middleware-logger
const bunyan = require('bunyan');
const Logger = require('@cork-labs/monkfish-adapter-logger').Logger;

// your application setup
const logger = Logger.createLogger({ name: 'my-app', streams: [{ type:'console' }] });
const httpLogger = require('@cork-labs/http-middleware-logger');
const options = {};
this._express.use(httpLogger(config, logger));
// use a middleware such as onHeaders to log responses
this._express.use((req, res, next) => {
  onHeaders(res, () => res.log());
  next();
});

// your route
app.get('/path', (req, res, next) => {
  req.logger.info(data, 'message');
})

Provide an instance of @cork-labs/monkfish-adapter-logger.

It supports:

  • child loggers (child contexts)
  • multiple streams at once (e.g.: console, bunyan, files, ...)

API

A child logger is created per request, and trace information is added to the logger.

The request is immediately logged.

> my-app | Thu, 10 Jan 2019 23:51:35 GMT | INFO | HttpApi::request()
  trace: {
    "uuid": "5b42547e-bd2f-4090-896c-18edfd79f39b",
    "current": "5efc8d65-2dd3-458c-8a46-96aa37985a4f"
  }
  --
  request: {
    "method": "GET",
    "path": "/account/5a9578f91e0d4724ed57ffe3"
  }

The child logger is exposed in req.logger and you can log further messages.

All messages contain the same trace object plus whatever data you provide to them.

16:29:19.279Z  INFO az.auth: Service::handle()
  trace: {
    "uuid": "5b42547e-bd2f-4090-896c-18edfd79f39b",
    "current": "5efc8d65-2dd3-458c-8a46-96aa37985a4f"
  }
  --
  event: {
    "type": "account.get",
    "params": {
      "id": "5a9578f91e0d4724ed57ffe3"
    }
  }

The essential monkfish-adapter-logger API:

  • req.logger.child(data = {}) returns a new logger, containing all the parent context plus the provided optional data
  • req.logger.set(key, value = undefined) - adds key to context data (avoid adding objects, keep it flat!)
  • req.logger.debug('message', data = {}) - logs debug level message, with adittional data (optional)
  • req.logger.info('message', data = {}) - same, info level
  • req.logger.warn('message', data = {}) - same, warning level
  • req.logger.error('message', data = {}, err = ...) - same, error level, plus optional error (ideally a subclass of Error)
  • req.logger.flat(prefix = '', obj) flattens the object for logging

res.log()

When the response is sent, invoke res.log().

You can use a middleware such as onHeaders to be notified when the response is sent.

this._express.use(httpLogger(config, logger));
// use a middleware such as onHeaders to log responses
this._express.use((req, res, next) => {
  onHeaders(res, () => res.log());
  next();
});

The response log contains a number of configurable.

The severity of this message can be configured via options.severityMap.

> my-app | Thu, 10 Jan 2019 23:51:35 GMT | INFO | HttpApi::response()
  trace: {
    "uuid": "5b42547e-bd2f-4090-896c-18edfd79f39b",
    "current": "5efc8d65-2dd3-458c-8a46-96aa37985a4f"
  }
  --
  response: {
    "status": 200
  }
  --
  timing: {
    "total": 20,
    "routed": 5,
    "handled": 15
  }

Configuration

The middleware can be configured via an options object when calling its factory function.

const options = {
  headers: {
    message: 'http.request.received'
  }
};
app.use(httpLogger(options));

requestMessage (default: 'http request')

requestKey (default: 'request')

requestFields (default: { method: 'method', path: 'path' })

responsetMessage (default: 'http response')

responseKey (default: 'response')

responseTimingKey (default: 'timing')

responseFields (default: { status: 'statusCode' })

traceKey (default: 'trace')

traceFields (default: { uuid: 'uuid', current: 'path' })

severityMap (default: { })

Develop

# lint and fix
npm run lint

# run test suite
npm test

# lint and test
npm run build

# serve test coverage
npm run coverage

# publish a minor version
node_modules/.bin/npm-bump minor

Contributing

We'd love for you to contribute to our source code and to make it even better than it is today!

Check CONTRIBUTING before submitting issues and PRs.

Links

MIT License

Copyright (c) 2018 Cork Labs