pinojs / pino-pretty

🌲Basic prettifier for Pino log lines
MIT License
1.27k stars 150 forks source link

Usage with logMethod hook and multiple log params #361

Closed alexcroox closed 2 years ago

alexcroox commented 2 years ago

I'm using a custom hook in order to add 2 param support to pino like I'm used to with console log, e.g

log.info('message', data)

I've modified the logMethod hook to support this:

const app = Fastify({
  logger: {
    hooks: {
      // Pino doesn't support concatenating log params so let's do it ourselves
      // e.g log.info('message', data)
      logMethod(args, method) {
        if (args.length === 2) {
          args[0] = `${args[0]} %j`
        }
        method.apply(this, args)
      }
    },
    transport:
      process.env.NODE_ENV !== 'production'
        ? {
            target: 'pino-pretty',
            options: {
              destination: 1,
              colorize: true,
              translateTime: 'HH:MM:ss.l',
              ignore: 'pid,hostname'
            }
          }
        : undefined
  },

However this is now interferring with pino-pretty and I get the following output from it:

[07:42:19.974] INFO: [object Object] 'incoming request'

What's the best approach to re-supporting pino-pretty's format? Is there a way to detect in the logMethod if pino-pretty is the initiator?

mcollina commented 2 years ago

Your implementation of logMethod is likely incorrect.

I would try something like:

      logMethod(args, method) {
        if (args.length === 2 && typeof args[0] === 'string') {
          args[0] = `${args[0]} %j`
        }
        method.apply(this, args)
      }
alexcroox commented 2 years ago

ah much better, thank you!!