express-pino-logger
Advanced tools
Comparing version 0.2.3 to 1.0.0
@@ -6,5 +6,3 @@ 'use strict' | ||
var logger = require('../')({ | ||
extreme: true | ||
}) | ||
var logger = require('../')() | ||
@@ -11,0 +9,0 @@ function handle (req, res) { |
'use strict' | ||
var pino = require('pino') | ||
var maxInt = 2147483647 | ||
function pinoLogger (opts, stream) { | ||
if (opts && opts._writableState) { | ||
stream = opts | ||
opts = null | ||
} | ||
opts = opts || {} | ||
opts.serializers = opts.serializers || {} | ||
opts.serializers.req = opts.serializers.req || asReqValue | ||
opts.serializers.res = opts.serializers.res || pino.stdSerializers.res | ||
var logger = pino(opts, stream) | ||
loggingMiddleware.logger = logger | ||
var nextId = 0 | ||
stream = logger.stream | ||
return loggingMiddleware | ||
function onResFinished (err) { | ||
this.removeListener('finish', onResFinished) | ||
this.removeListener('error', onResFinished) | ||
var end = process.hrtime(this.startTime) | ||
var log = this.log | ||
var responseTime = Math.round(end[0] * 1e3 + end[1] / 1e6) | ||
if (err) { | ||
log.error({ | ||
res: this, | ||
err: err, | ||
responseTime: this.responseTime | ||
}, 'request errored') | ||
return | ||
} | ||
log.info({ | ||
res: this, | ||
responseTime: responseTime | ||
}, 'request completed') | ||
} | ||
function loggingMiddleware (req, res, next) { | ||
var startTime = process.hrtime() | ||
req.id = ++nextId | ||
nextId = nextId % maxInt | ||
var child = logger.child({ req: req }) | ||
req.log = child | ||
res.log = child | ||
res.startTime = startTime | ||
res.on('finish', onResFinished) | ||
res.on('error', onResFinished) | ||
if (next) { | ||
next() | ||
} | ||
} | ||
} | ||
function asReqValue (req) { | ||
return { | ||
id: req.id, | ||
method: req.method, | ||
url: req.url, | ||
headers: req.headers, | ||
remoteAddress: req.connection.remoteAddress, | ||
remotePort: req.connection.remotePort | ||
} | ||
} | ||
module.exports = pinoLogger | ||
module.exports = require('pino-http') |
{ | ||
"name": "express-pino-logger", | ||
"version": "0.2.3", | ||
"version": "1.0.0", | ||
"description": "An express middleware to log with pino", | ||
@@ -29,3 +29,3 @@ "main": "logger.js", | ||
"dependencies": { | ||
"pino": "^2.1.0" | ||
"pino-http": "^1.0.5" | ||
}, | ||
@@ -37,2 +37,3 @@ "devDependencies": { | ||
"morgan": "^1.7.0", | ||
"pino": "^2.1.6", | ||
"pre-commit": "^1.1.2", | ||
@@ -39,0 +40,0 @@ "split2": "^2.0.1", |
@@ -22,12 +22,13 @@ # express-pino-logger [![Build Status](https://travis-ci.org/mcollina/express-pino-logger.svg)](https://travis-ci.org/mcollina/express-pino-logger) | ||
[autocannon](https://github.com/mcollina/autocannon) with 100 | ||
connections and 10 pipelined requests. | ||
connections and 10 pipelined requests (`autocannon -c 100 -p 10 http://localhost:3000`). | ||
* `express-bunyan-logger`: 1950 req/sec | ||
* `express-winston`: 4739 req/sec | ||
* `morgan`: 5812 req/sec | ||
* `express-pino-logger`: 6108 req/sec | ||
* `express-pino-logger` (extreme): 6594 req/sec | ||
* `express-pino-logger` (without express and extreme): 11988 req/sec | ||
* `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 2014 (i7, 16GB of RAM). | ||
All benchmarks where taken on a Macbook Pro 2013 (2.6GHZ i7, 16GB of RAM). | ||
@@ -34,0 +35,0 @@ Whilst we're comparing `express-pino-logger` against [morgan](http://npm.im/morgan), this isn't really a fair contest. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
18
0
115
13619
11
271
11
+ Addedpino-http@^1.0.5
+ Addedpino-http@1.3.1(transitive)
- Removedpino@^2.1.0