Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use pino-std-serializers #46

Merged
merged 1 commit into from
Feb 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 6 additions & 113 deletions logger.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict'

var pino = require('pino')
var serializers = require('pino-std-serializers')

var startTime = Symbol('startTime')

Expand All @@ -12,9 +13,9 @@ function pinoLogger (opts, stream) {

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

var useLevel = opts.useLevel || 'info'
delete opts.useLevel
Expand Down Expand Up @@ -64,114 +65,6 @@ function pinoLogger (opts, stream) {
}
}

var rawSymbol = Symbol.for('pino-raw-ref')
var pinoReqProto = Object.create({}, {
id: {
enumerable: true,
writable: true,
value: ''
},
method: {
enumerable: true,
writable: true,
value: ''
},
url: {
enumerable: true,
writable: true,
value: ''
},
headers: {
enumerable: true,
writable: true,
value: {}
},
remoteAddress: {
enumerable: true,
writable: true,
value: ''
},
remotePort: {
enumerable: true,
writable: true,
value: ''
},
raw: {
enumerable: false,
get: function () {
return this[rawSymbol]
},
set: function (val) {
this[rawSymbol] = val
}
}
})
Object.defineProperty(pinoReqProto, rawSymbol, {
writable: true,
value: {}
})

function wrapReqSerializer (serializer) {
if (serializer === asReqValue) return asReqValue
return function wrappedReqSerializer (req) {
return serializer(asReqValue(req))
}
}

function asReqValue (req) {
var connection = req.connection
const _req = Object.create(pinoReqProto)
_req.id = typeof req.id === 'function' ? req.id() : req.id
_req.method = req.method
_req.url = req.url
_req.headers = req.headers
_req.remoteAddress = connection && connection.remoteAddress
_req.remotePort = connection && connection.remotePort
_req.raw = req
return _req
}

var pinoResProto = Object.create({}, {
statusCode: {
enumerable: true,
writable: true,
value: 0
},
header: {
enumerable: true,
writable: true,
value: ''
},
raw: {
enumerable: false,
get: function () {
return this[rawSymbol]
},
set: function (val) {
this[rawSymbol] = val
}
}
})
Object.defineProperty(pinoResProto, rawSymbol, {
writable: true,
value: {}
})

function asResValue (res) {
const _res = Object.create(pinoResProto)
_res.statusCode = res.statusCode
_res.header = res._header
_res.raw = res
return _res
}

function wrapResSerializer (serializer) {
if (serializer === asResValue) return asResValue
return function wrappedResSerializer (res) {
return serializer(asResValue(res))
}
}

function wrapChild (opts, stream) {
var prevLogger = opts.logger
var prevGenReqId = opts.genReqId
Expand Down Expand Up @@ -201,7 +94,7 @@ function reqIdGenFactory (func) {

module.exports = pinoLogger
module.exports.stdSerializers = {
req: asReqValue,
res: asResValue
req: serializers.req,
res: serializers.res
}
module.exports.startTime = startTime
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"description": "High-speed HTTP logger for Node.js",
"main": "logger.js",
"dependencies": {
"pino": "^4.10.2"
"pino": "^4.10.2",
"pino-std-serializers": "^1.0.0"
},
"devDependencies": {
"autocannon": "^0.16.5",
Expand Down