pino icon indicating copy to clipboard operation
pino copied to clipboard

Functions stored in array will be shown as nulls

Open DaniilVysotskiy opened this issue 5 years ago • 5 comments

We have some abstract method in class, like this:

class SomeClass {
    constructor() {
        this.dataListeners = {};
    }

    someAbstractMethod (topic, eventFn) {
      if (!Array.isArray(this.dataListeners[topic])) {
        this.dataListeners[topic] = [];
        ...
      }
      ...
      this.dataListeners[topic].push(eventFn);
      this.logger.info('Listeners to topic', this.dataListeners[topic]); // --> output is { ...,"msg":"Listeners to topic [null]","v":1}

      return this;
    }
}

this.dataListeners[topic] actually stores the functions, but somehow pino interpreters those as nulls. Same message shown via console.log() is proper:

Listeners to topic [ [Function: onIncomingTask] ]

What am I doing wrong?

DaniilVysotskiy avatar Jun 07 '19 07:06 DaniilVysotskiy

Half day stuck because of these nulls in logged arrays!

rusekr avatar Jun 07 '19 08:06 rusekr

the msg text should be fixed to ease processing/searching.

Add those as a property.

log.info({ hello: “world” }, “some log line”)

I don’t recall if we did this for some purpose or not, there were definitely some differences in interpolation. Maybe would you like to send a PR? What do you think @davidmarkclements?

mcollina avatar Jun 08 '19 14:06 mcollina

this is something we can improve in the mean time a workaround for this scenario would be to convert the functions to strings yourself

this.logger.info('Listeners to topic', this.dataListeners[topic].map(({name}) => `[Function: ${name}]`));

davidmarkclements avatar Jun 08 '19 17:06 davidmarkclements

Stumbled across this today, is there any other workaround than the one david mentioned above? Having to do this in every log is quite cumbersome expecially when using typescript as I want to print out whole objects and and data mangling them to print out function names is quite a hassle tbh :thinking:

zaunermax avatar Jul 14 '20 11:07 zaunermax

Use a hook -- https://github.com/pinojs/pino/blob/613d700fc2b77768bbbc84095a5cc28fb745b10d/docs/api.md#logmethod

jsumners avatar Jul 14 '20 11:07 jsumners

Use a hook -- https://github.com/pinojs/pino/blob/613d700fc2b77768bbbc84095a5cc28fb745b10d/docs/api.md#logmethod

Resolved by this.

Fdawgs avatar May 29 '23 08:05 Fdawgs

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Jun 29 '23 00:06 github-actions[bot]