Skip to content

Commit

Permalink
Fix #42
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Dec 2, 2017
1 parent c0851b5 commit 765c6b9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
14 changes: 13 additions & 1 deletion logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function pinoLogger (opts, stream) {

opts = opts || {}
opts.serializers = opts.serializers || {}
opts.serializers.req = opts.serializers.req || asReqValue
opts.serializers.req = wrapReqSerializer(opts.serializers.req || asReqValue)
opts.serializers.res = opts.serializers.res || pino.stdSerializers.res
opts.serializers.err = opts.serializers.err || pino.stdSerializers.err

Expand Down Expand Up @@ -64,6 +64,18 @@ function pinoLogger (opts, stream) {
}
}

function wrapReqSerializer (serializer) {
if (serializer === asReqValue) return asReqValue
return function wrappedReqSerializer (req) {
var _req = asReqValue(req)
Object.defineProperty(_req, 'raw', {
enumerable: false,
value: req
})
return serializer(_req)
}
}

function asReqValue (req) {
var connection = req.connection
return {
Expand Down
31 changes: 31 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,34 @@ test('does not crash when no request connection object', function (t) {
res.end()
}
})

// https://github.com/pinojs/pino-http/issues/42
test('does not return excessively log object', function (t) {
var dest = split(JSON.parse)
var logger = pinoHttp({
logger: pino(dest),
serializers: {
req: function (req) {
delete req.connection
return req
}
}
})
t.plan(1)

var server = http.createServer(handler)
server.unref()
server.listen(0, () => {
const port = server.address().port
http.get(`http://127.0.0.1:${port}`, () => {})
})

function handler (req, res) {
logger(req, res)
res.end()
}

dest.on('data', function (obj) {
t.is(Object.keys(obj.req).length, 6)
})
})

0 comments on commit 765c6b9

Please sign in to comment.