diff --git a/logger.js b/logger.js index 5c081b8..ade12f4 100644 --- a/logger.js +++ b/logger.js @@ -1,6 +1,6 @@ 'use strict' -const pino = require('pino') +const { pino, symbols: { stringifySym, chindingsSym } } = require('pino') const serializers = require('pino-std-serializers') const getCallerFile = require('get-caller-file') const startTime = Symbol('startTime') @@ -103,7 +103,11 @@ function pinoLogger (opts, stream) { const customPropBindings = (typeof customProps === 'function') ? customProps(req, res) : customProps if (customPropBindings) { - log = logger.child(customPropBindings) + const customPropBindingStr = logger[stringifySym](customPropBindings).replace(/[{}]/g, '') + const customPropBindingsStr = logger[chindingsSym] + if (!customPropBindingsStr.includes(customPropBindingStr)) { + log = logger.child(customPropBindings) + } } if (err || res.err || res.statusCode >= 500) { diff --git a/test/test.js b/test/test.js index 29bd6a7..35f6291 100644 --- a/test/test.js +++ b/test/test.js @@ -1285,6 +1285,30 @@ test('uses custom request properties to log additional attributes; custom props }) }) +test('uses custom request properties and once customProps', function (t) { + const dest = split() + + function customPropsHandler (req, res) { + return { + key1: 'value1' + } + } + + const logger = pinoHttp({ + customProps: customPropsHandler + }, dest) + + setup(t, logger, function (err, server) { + t.error(err) + doGet(server) + }) + + dest.on('data', function (line) { + t.equal(line.match(/key1/g).length, 1, 'once customProps') + t.end() + }) +}) + test('dont pass custom request properties to log additional attributes', function (t) { const dest = split(JSON.parse) const logger = pinoHttp({