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-pino-logger

v7.0.0

Published

An express middleware to log with pino

Downloads

316,625

Readme

express-pino-logger

npm version Build Status Known Vulnerabilities js-standard-style

An express middleware to log with pino. Incidentally, it also works without express.

To our knowledge, express-pino-logger is the fastest express logger in town.

Notice: This is a "meta-package" that only exists for search purposes and internally just exports pino-http under a different name without any additional changes or features (see #41).

Benchmarks

Benchmarks log each request/response pair while returning 'hello world', using autocannon with 100 connections and 10 pipelined requests (autocannon -c 100 -p 10 http://localhost:3000).

  • express-bunyan-logger: 2702 req/sec
  • express-winston: 5953 req/sec
  • morgan: 8570 req/sec
  • express-pino-logger: 9807 req/sec
  • express-pino-logger (extreme): 10407 req/sec
  • express-pino-logger (without express): 22240.73 req/seq
  • express-pino-logger (without express and extreme): 25536 req/sec

All benchmarks where taken on a Macbook Pro 2013 (2.6GHZ i7, 16GB of RAM).

Whilst we're comparing express-pino-logger against morgan, this isn't really a fair contest.

Morgan doesn't support logging arbitrary data, nor does it output JSON. Further Morgan uses a form of eval to achieve high speed logging. Whilst probably safe, using eval at all tends to cause concern, particular when it comes to server-side JavaScript.

The fact that express-pino-logger achieves higher throughput with JSON logging and arbitrary data, without using eval, serves to emphasise the high-speed capabilities of express-pino-logger.

With express-pino-logger you can have features, safety and speed.

Install

npm i express-pino-logger --save

Example

'use strict'

var app = require('express')()
var pino = require('express-pino-logger')

app.use(pino)

app.get('/', function (req, res) {
  // each request has its own id
  // so you can track the log of each request
  // by using `req.log`
  // the ids are cycled every 2^31 - 2
  req.log.info('something else')
  res.send('hello world')
})

app.listen(3000)
$ node example.js | pino-pretty
[2016-03-31T16:53:21.079Z] INFO (46316 on MBP-di-Matteo): something else
    req: {
      "id": 1,
      "method": "GET",
      "url": "/",
      "headers": {
        "host": "localhost:3000",
        "user-agent": "curl/7.43.0",
        "accept": "*/*"
      },
      "remoteAddress": "::1",
      "remotePort": 64386
    }
[2016-03-31T16:53:21.087Z] INFO (46316 on MBP-di-Matteo): request completed
    res: {
      "statusCode": 200,
      "header": "HTTP/1.1 200 OK\r\nX-Powered-By: Express\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: 11\r\nETag: W/\"b-XrY7u+Ae7tCTyyK7j1rNww\"\r\nDate: Thu, 31 Mar 2016 16:53:21 GMT\r\nConnection: keep-alive\r\n\r\n"
    }
    responseTime: 10
    req: {
      "id": 1,
      "method": "GET",
      "url": "/",
      "headers": {
        "host": "localhost:3000",
        "user-agent": "curl/7.43.0",
        "accept": "*/*"
      },
      "remoteAddress": "::1",
      "remotePort": 64386
    }

Custom serializers

The req object for logging is constructed by pino-std-serializers and custom properties added to the req in previous middleware are not automatically included.

The original req is accessible in the custom serializer under req.raw.

'use strict'

var app = require('express')()
var ExpressPinoLogger = require('express-pino-logger')()

var pino = ExpressPinoLogger({
  serializers: {
    req: (req) => ({
      method: req.method,
      url: req.url,
      user: req.raw.user,
    }),
  },
})

// middleware that augments the req - must be added before the pino middleware
app.use((req, res, next) => {
  req.user = 'testing';
  next();
})

app.use(pino)

...

API

express-pino-logger has the same options of pino, look at them there. express-pino-logger attaches some listeners to the request, so that it will log when the request is completed.

You can also reuse an instance of pino by passing it in the constructor with:

'use strict'

const pino = require('pino')()
const expressPino = require('express-pino-logger')({
  logger: pino
})

License

MIT