Skip to content

Commit

Permalink
fix: twice customProps (#288)
Browse files Browse the repository at this point in the history
* fix: twice customPropBindings

* Add unit test for twice customPropBindings

* Remove case insensitive in match regex
  • Loading branch information
youngkiu committed Aug 4, 2023
1 parent c142685 commit bea64cc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
8 changes: 6 additions & 2 deletions logger.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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) {
Expand Down
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit bea64cc

Please sign in to comment.