hapijs / hapi-pino

🌲 Hapi plugin for the Pino logger
MIT License
148 stars 61 forks source link

Recommended way to save to files #169

Closed ghostdevv closed 2 years ago

ghostdevv commented 2 years ago

Hiya, this is my first time using Pino so might be missing something huge here - basically I have setup hapi-pino and it works great however I want to persist my logs to files as to debug I would potentially have to sift through 1000s of objects to find what I am looking for. This just won't work when printing to std. Ideally I would also be able to make the log file per day but that is a nice to have. Any ideas how I can achieve this file logging?

I did notice there is a stream option, and I found some bits on it in the pino docs however I wasn't able to figure out the recommended way to save to a file.

mcollina commented 2 years ago

You can do something like the following:

const pino = require('pino')
const transport = pino.transport({
  targets: [
    { target: 'pino/file', level: 'info' }, // only for production, 
    { target: 'pino-pretty', level: 'info' }, // only for development
    { target: 'pino/file', level: 'trace', options: { destination: '/path/to/your/file' }} // all logs to a file
  ]
})
pino({ stream: transport, level: 'trace' })

Let me know if it works for you! If it doesn't please paste a reproducible example of what you are doing.

ghostdevv commented 2 years ago

Awesome, I got this working:

    transport: DEV
        ? {
              target: 'pino-pretty',
              options: {
                  colorize: true,
                  translateTime: 'HH:MM:ss.l mmmm dS yyyy "UTC"',
              } as PrettyOptions,
          }
        : {
              target: 'pino/file',
              options: {
                  destination: desm.join(import.meta.url, '../../../logs/api.log'),
                  mkdir: true,
              },
          },

Hopefully I can figure out how to split it up into seperate files by date or something in the future as that file is going to get big fast. Thank you for your help, will close this as it's unrelated to hapi-pino :pray: