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

Global serializer invoked on child creation #679

Open
jsumners opened this issue Jul 8, 2019 · 6 comments
Open

Global serializer invoked on child creation #679

jsumners opened this issue Jul 8, 2019 · 6 comments

Comments

@jsumners
Copy link
Member

jsumners commented Jul 8, 2019

'use strict'

const pino = require('pino')

const logger = pino({
  useLevelLabels: true,
  messageKey: 'message',
  base: null,
  serializers: {
    [Symbol.for('pino.*')]: obj => {
      console.log('serializer')
      console.log(obj);
      return obj
    },
    
    err: (obj) => {
      console.log('err serializer')
      return obj
    }
  }
});

let l = logger.child({ app: {
  name: 'blah'
}})

This will output:

serializer
{ app: { name: 'blah' } }

Serializers should only be executed when a log function is invoked, e.g. log.info.

@mcollina
Copy link
Member

mcollina commented Jul 8, 2019

This is explicit behavior. We added this to make child logger extremely fast. The object passed to child() is stringified and appended to the base, so we don’t have to do it at runtime for all logs.

@jsumners
Copy link
Member Author

jsumners commented Jul 8, 2019

@jsumners jsumners added documentation and removed bug labels Jul 8, 2019
@mcollina
Copy link
Member

mcollina commented Jul 8, 2019

As most things, it was the result of an endless debate on Github a long time ago :D.

@jsumners
Copy link
Member Author

jsumners commented Jul 8, 2019

I think it is a conflict of interest in wanting a global serializer but and wanting speed. No real solution other than to document the behavior.

@chrisregnier
Copy link

I've spent at least a day now debugging and trying to figure out why my global serializer wasn't working in certain situations, and it turns out to be due in part to this.
I've got a dynamic global serializer that appends a request user and id per request for ALL logs based on something like thread local storage (https://github.com/Jeff-Lewis/cls-hooked), so it HAS to be dynamic. So I find it extremely disappointing that this tries to cache the value without documenting it anywhere. It also seems like extremely bizarre behaviour to have it run per log for the root logger but cache the value for child loggers.
Furthermore, I tried to pass in a new global serializer to the child logger, but due to the way it iterates over serializers it doesn't copy the Symbol for the global serializer over, so I can't even give the child logger the serializer I want.

I definitely think this is a bug that should be fixed

@mcollina
Copy link
Member

@chrisregnier I’m sorry to hear. Can you please make an example with cls-hook and the other issues you mentioned? It seems you are talking about different behavior changes at the same time.

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

No branches or pull requests

3 participants