From 5ed4f6f99cc30541d791bc341c6d10605ee32527 Mon Sep 17 00:00:00 2001 From: James Sumners Date: Thu, 22 Feb 2018 18:22:04 -0500 Subject: [PATCH] Refactor to use pino-std-serializers --- logger.js | 119 +++------------------------------------------------ package.json | 3 +- 2 files changed, 8 insertions(+), 114 deletions(-) diff --git a/logger.js b/logger.js index 3d9a077..7f0b714 100644 --- a/logger.js +++ b/logger.js @@ -1,6 +1,7 @@ 'use strict' var pino = require('pino') +var serializers = require('pino-std-serializers') var startTime = Symbol('startTime') @@ -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 @@ -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 @@ -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 diff --git a/package.json b/package.json index f7bec85..d9705d6 100644 --- a/package.json +++ b/package.json @@ -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",