express-pino-logger

2019-10-09

An express middleware to log with pino

express-pino-logger  Build Status

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

To our knowledge, express-pino-loggeris the fastestexpresslogger in town.

Benchmarks

Benchmarks log each request/response pair while returning 'hello world', using autocannonwith 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-loggeragainst 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 evalto achieve high speed logging. Whilst probably safe, using evalat all tends to cause concern, particular when it comes to server-side JavaScript.

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

With express-pino-loggeryou can have features, safety andspeed.

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
[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
    }

API

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

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

'use strict'

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

License

MIT

HomePage

https://github.com/pinojs/express-pino-logger#readme

Repository

https+https://github.com/pinojs/express-pino-logger


上一篇:js处理数据
下一篇:hot-shots
相关教程
关注微信

扫码加入 JavaScript 社区

相关文章

首次访问,需要验证
微信扫码,关注即可
(仅需验证一次)

欢迎加入 JavaScript 社区

号内回复关键字:

回到顶部