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-winston-middleware

v0.1.0

Published

Winston log wrappers for Express.

Downloads

1,003

Readme

Winston Middleware for Express

Winston log wrappers and middleware for Express.

Usage

You can install express-winston-middleware using NPM:

$ npm install express-winston-middleware

From there, you can create a Log instance or use the request Express middleware.

Examples

See the examples directory for some basic use cases.

Logger

The Log logger class internally creates and wraps a Winston logger. You can create one with:

var winston = require("winston");
var Log = require("express-winston-middleware").Log;

// Create logger with Console transport and "foo" metadata item.
var log = new Log({
  transports: [
    new (winston.transports.Console)({ json: true })
  ]
}, {
  // Metadata to add to each log response.
  foo: "bar"
});

// Log something.
log.info("Hello World!");

which produces the following output:

{
  "date": "2013-12-01T23:29:48.035Z",
  "env": "development",
  "server": {
    "id": "m",
    "pid": 24638,
    "hostName": "titan.local"
  },
  "foo": "bar",
  "level": "info",
  "message": "Hello World!"
}

Express Middleware

The request middleware is added to your Express setup like:

var express = require("express");
var app = express(),
var winMid = require("express-winston-middleware");

/* ... */

// Same options and meta as for the `Log` class.
app.use(new winMid.request({
  transports: [
    new (winston.transports.Console)({ json: true })
  ]
}, {
  // Metadata to add to each log response.
  foo: "bar"
})));

and produces output for requests like:

{
  "date": "2013-12-01T23:32:54.759Z",
  "server": {
    "id": "m",
    "pid": 24653,
    "hostName": "titan.local"
  },
  "req": {
    "method": "GET",
    "host": "localhost:2000",
    "path": "/",
    "query": ""
  },
  "res": {
    "statusCode": 304
  },
  "foo": "bar",
  "level": "info",
  "message": "request"
}

The middleware attaches a logger to the response locals, available as res.locals._log, so that in addition to automatic request logging messages you can log extra messages with all of the current request metadata. E.g.:

app.get("/foo", function (req, res) {
  res.locals._log.info("This is an extra manual log message!", {
    extra: "metadata"
  });
  // Rest of your code here...
});

API

request(opts, baseMeta) - Express request middleware

Creates a middleware function using base metadata. Integration:

app.use(winMid.request({
  transports: [ new (winston.transports.Console)({ json: true }) ]
}, { foo: "bar" }));

Once integrated, a logger will be attached to the response locals, and available as res.locals._log. The logger will then be removed at the end of the request.

error(opts, baseMeta) - Express error middleware

Creates a middleware function for Express. Integration:

app.use(winMid.error({
  transports: [ new (winston.transports.Console)({ json: true }) ]
}, { foo: "bar" }));

uncaught(opts, baseMeta) - Global uncaught exception handler

Creates a handler function for any uncaught exception. Integration:

process.on("uncaughtException", winMid.uncaught({
  transports: [ new (winston.transports.Console)({ json: true }) ]
}, { foo: "bar" }));

Note: Terminates process at end.

Log(opts, baseMeta) - Logger class.

Wraps Winston logger with additional functionality.

var log = new winMid.Log({
  transports: [ new (winston.transports.Console)({ json: true }) ]
}, { foo: "bar" }));

Log.addMeta(meta)

Add arbitrary meta to all subsequent log statements.

Log.addReq(req)

Add request to meta.

Log.transformMeta(fn)

Set a delayed single transform function to mutate a copy of the metadata right before a logging event. You can only presently have one such function. And it is delayed so that for things like request end, you can effectively access all the metadata.

The transform is applied on each log call and passes a copy of the mutated metadata to the actual log call.

The function signature should be fn(existingMeta) and return mutated metadata.

Log.addRes(res)

Add response to meta.

Log.addError(err)

Add error to meta.

Contributions

Please see the Contributions Guide for how to help out with the plugin.

We test all changes with Travis CI. Here's our current build status:

Build Status

Licenses

All code is 2013-2016 Formidable Labs. Released under the MIT License.