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

feat: keep parent formatters bindings function option #1950

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

DamienGarrido
Copy link

Synopsis

I have the following code, which intent to make some property values upper case (it could also be stringified, etc.):

const _ = require('lodash')
const pino = require('pino')

function shapeObject(object) {
  const propertyPaths = ['logger', 'method', 'partner', 'service']
  propertyPaths.forEach(path => {
    const value = _.get(object, path)
    if (typeof value === 'string') _.set(object, path, value.toUpperCase())
  })

  return object
}

const parent = pino({
  formatters: {
    log: shapeObject,
    bindings: shapeObject
  },
})
const child = parent.child({})

parent.setBindings({ logger: 'parent' })
child.setBindings({ logger: 'child' })

function testScenario(logger) {
  const partner = 'apple'
  const method = 'get'
  const service = 'foo'

  logger.setBindings({ service })
  logger.info('one')
  const childLogger = logger.child({ partner })
  childLogger.info({ method }, 'two')
}

testScenario(parent)
testScenario(child)

When I run this code, I actually get the following output:

{"level":30,"time":1,"pid":7068,"hostname":"myMac","logger":"PARENT","service":"FOO","msg":"one"}
{"level":30,"time":1,"pid":7068,"hostname":"myMac","logger":"PARENT","service":"FOO","partner":"apple","method":"GET","msg":"two"}
{"level":30,"time":1,"pid":7068,"hostname":"myMac","logger":"child","service":"foo","msg":"one"}
{"level":30,"time":1,"pid":7068,"hostname":"myMac","logger":"child","service":"foo","partner":"apple","method":"GET","msg":"two"}

Shape the bindings passed to the child() method

There are currently functions to shape the log level, the log object, the bindings, but I cannot see any function to shape the bindings passed to a child logger through the child() method.

I would like to be able to apply my shaping function to the partner property:

{ "partner": "apple" }

Keep the parent bindings formatters function in children loggers

In case of a global bindings formatters function, I would like to be able to keep that bindings formatters function in the logger children (and without having to pass it at each child() call).

I would like to be able to apply my shaping function to all my propertyPaths properties in my logger children too, so I can get the following:

{"level":30,"time":1,"pid":7068,"hostname":"myMac","logger":"PARENT","service":"FOO","msg":"one"}
{"level":30,"time":1,"pid":7068,"hostname":"myMac","logger":"PARENT","service":"FOO","partner":"APPLE","method":"GET","msg":"two"}
{"level":30,"time":1,"pid":7068,"hostname":"myMac","logger":"CHILD","service":"FOO","msg":"one"}
{"level":30,"time":1,"pid":7068,"hostname":"myMac","logger":"CHILD","service":"FOO","partner":"APPLE","method":"GET","msg":"two"}

Proposal

Add an options object

Add a formatters options object, with a keepParentBindings option:

pino({
  formatters: {
    log: shapeObject,
    bindings: shapeObject
    options: { keepParentBindings: true },
  },
})

The formatters options object would be passed to the child loggers, except if a new options object is passed to the child() method.

The keepParentBindings option

If true, child loggers will keep their parent bindings formatters function.

If false or undefined, child loggers will have their bindings formatters function reset to a "no-op" formatter function, as today.

The child() method

In the child() method, the parent bindings function will be passed or not to the child logger according to the keepParentBindings option, except if a new bindings formatters function is passed to the child logger.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants