Comparing version 2.6.1 to 2.6.2
@@ -5,2 +5,4 @@ 'use strict' | ||
var startTime = Symbol('startTime') | ||
function pinoLogger (opts, stream) { | ||
@@ -34,3 +36,3 @@ if (opts && opts._writableState) { | ||
var log = this.log | ||
var responseTime = Date.now() - this.startTime | ||
var responseTime = Date.now() - this[startTime] | ||
@@ -55,3 +57,3 @@ if (err) { | ||
req.log = res.log = logger.child({req: req}) | ||
res.startTime = Date.now() | ||
res[startTime] = res[startTime] || Date.now() | ||
if (!req.res) { req.res = res } | ||
@@ -69,2 +71,3 @@ | ||
function asReqValue (req) { | ||
var connection = req.connection | ||
return { | ||
@@ -75,4 +78,4 @@ id: req.id, | ||
headers: req.headers, | ||
remoteAddress: req.connection.remoteAddress, | ||
remotePort: req.connection.remotePort | ||
remoteAddress: connection && connection.remoteAddress, | ||
remotePort: connection && connection.remotePort | ||
} | ||
@@ -113,1 +116,2 @@ } | ||
} | ||
module.exports.startTime = startTime |
{ | ||
"name": "pino-http", | ||
"version": "2.6.1", | ||
"version": "2.6.2", | ||
"description": "High-speed HTTP logger for Node.js", | ||
@@ -10,2 +10,3 @@ "main": "logger.js", | ||
"devDependencies": { | ||
"autocannon": "^0.16.5", | ||
"coveralls": "^2.11.15", | ||
@@ -19,2 +20,3 @@ "http-ndjson": "^3.0.0", | ||
"scripts": { | ||
"benchmark": "./scripts/benchmark-all", | ||
"test": "standard && tap --no-cov test.js", | ||
@@ -21,0 +23,0 @@ "ci": "standard && tap --cov test.js" |
@@ -139,3 +139,23 @@ # pino-http [![Build Status](https://travis-ci.org/pinojs/pino-http.svg)](https://travis-ci.org/pinojs/pino-http)[![Coverage Status](https://coveralls.io/repos/github/pinojs/pino-http/badge.svg?branch=master)](https://coveralls.io/github/pinojs/pino-http?branch=master) | ||
##### pinoHttp.startTime (Symbol) | ||
The `pinoHttp` function has a property called `startTime` which contains a symbol | ||
that is used to attach and reference a start time on the HTTP `res` object. If the function | ||
returned from `pinoHttp` is not *the first* function to be called in an HTTP servers request | ||
listener function then the `responseTime` key in the log output will be offset by any | ||
processing that happens before a response is logged. This can be corrected by manually attaching | ||
the start time to the `res` object with the `pinoHttp.startTime` symbol, like so: | ||
```js | ||
var http = require('http') | ||
var logger = require('pino-http')() | ||
var someImportantThingThatHasToBeFirst = require('some-important-thing') | ||
http.createServer((req, res) => { | ||
res[logger.startTime] = Date.now() | ||
someImportantThingThatHasToBeFirst(req, res) | ||
logger(req, res) | ||
res.end('hello world') | ||
}).listen(3000) | ||
``` | ||
#### Default serializers | ||
@@ -142,0 +162,0 @@ |
46
test.js
@@ -167,2 +167,26 @@ 'use strict' | ||
test('startTime', function (t) { | ||
var dest = split(JSON.parse) | ||
var logger = pinoHttp(dest) | ||
var someStartTime = 56 | ||
t.equal(typeof pinoHttp.startTime, 'symbol') | ||
function loggerWithStartTime (req, res) { | ||
res[pinoHttp.startTime] = someStartTime | ||
logger(req, res) | ||
t.equal(res[pinoHttp.startTime], someStartTime) | ||
} | ||
setup(t, loggerWithStartTime, function (err, server) { | ||
t.error(err) | ||
doGet(server) | ||
}) | ||
dest.on('data', function (line) { | ||
t.equal(typeof line.responseTime, 'number') | ||
t.end() | ||
}) | ||
}) | ||
test('responseTime', function (t) { | ||
@@ -261,1 +285,23 @@ var dest = split(JSON.parse) | ||
}) | ||
test('does not crash when no request connection object', function (t) { | ||
var dest = split(JSON.parse) | ||
var logger = pinoHttp({ | ||
logger: pino(dest) | ||
}) | ||
t.plan(1) | ||
var server = http.createServer(handler) | ||
server.unref() | ||
server.listen(9999, () => { | ||
http.get('http://127.0.0.1:9999', (res) => { | ||
t.pass('made it through logic path without crashing') | ||
}) | ||
}) | ||
function handler (req, res) { | ||
delete req.connection | ||
logger(req, res) | ||
res.end() | ||
} | ||
}) |
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
21347
15
414
242
7