mcollina / pino-roll

A Pino transport that automatically rolls your log files
MIT License
41 stars 11 forks source link

Be able to benefit from pino-pretty when using pino-roll #4

Closed noweh closed 2 years ago

noweh commented 2 years ago

Hello! pino-roll looks good to me, but we are already using the "pino-pretty" and when we use both at the same time, pino-pretty doesn't seem to work:

pino.transport({
  targets: [
    {
      target: 'pino-roll',
      options: {
        file: join('logs', 'log'), frequency: 'daily', mkdir: true
      }
    },
    {
      target: 'pino-pretty',
      options: {
        // formatting output
        translateTime: 'yyyy-dd-mm, h:MM:ss.l o TT',
        colorize: false,
        destination: 'XXX'
      }
    }
  ]
})

Expected logs: [2022-06-04, 4:01:30.709 +0000 PM] INFO (8200 on DIGI-LAPW051): XXX.

Real Logs: {"level":30,"time":1649260890720,"pid":8200,"hostname":"DIGI-LAPW051","msg":"XXX."}

is it possible to use both at the same time? Thanks!

feugy commented 2 years ago

Hi @noweh.

This gave me some headaches.

First, I've tried to leverage pino builtin pipeline. Unfortunately, pino-pretty is not a transform stream, and can not be used this way.

Then I've tried to use pino-pretty's destination which can be any SonicBoom instance. As you know, pino-roll is a SonicBoom instance... but it can not be serialized using pino@7+ new paradigm for options

The final solution is a bit convoluted, but works as you expect:

First, create a file for your own transport. Let's say pino-pretty-roll.js

module.exports = async ({ prettyOptions, rollOptions }) => 
  require('pino-pretty')({ ...prettyOptions, destination: await require('pino-roll')(rollOptions) })

Then, use it:

pino.transport({
  target: './pino-pretty-roll.js',
  options: {
    prettyOptions: { /* options for pino pretty */ },
    rollOptions: { /* options for pino roll */ }
  }
})

Maybe @mcollina will know a better way.

feugy commented 2 years ago

Closing since Noweh "acknoledge" the suggested implementation