pinojs / pino

🌲 super fast, all natural json logger
http://getpino.io
MIT License
14.21k stars 875 forks source link

How to ouput pino-pretty to console and file at the same time #1341

Closed FluffyDiscord closed 2 years ago

FluffyDiscord commented 2 years ago

As the prettyPrint option is deprecated, how can I output pretty printed log to two streams - console and file - at the same time ?

Currently I am using configuration bellow which works fine for me for now

const pino = require('pino')
const logger = pino({
    prettyPrint: true
}, pino.multistream([
    { stream: process.stdout },
    { stream: fs.createWriteStream('/path/last.log') }
]))
dbacarel commented 2 years ago

According to https://github.com/pinojs/pino-pretty#programmatic-integration, the alternative is to use pino-pretty transport which writes onto stdout by default.

So, you would need something like this:

const pino = require('pino')
const transport = pino.transport({
  targets: [{
    level: 'info',
    target: 'pino-pretty' // must be installed separately
  }, {
    level: 'trace',
    target: 'pino/file',
    options: { destination: '/path/last.log' }
  }]
})
const logger = pino(transport)
FluffyDiscord commented 2 years ago

With this approach only console output is formatted, but the log file is still a raw json. I would like to have pretty formatted output in both

dbacarel commented 2 years ago

pino-pretty transport writes to stdout by default but "the options.destination property may be set to log pretty logs to a file descriptor or file." see pino-pretty transport

Like so:

const pino = require('pino')
const transport = pino.transport({
  targets: [{
    level: 'info',
    target: 'pino-pretty'
  }, {
    level: 'info',
    target: 'pino-pretty',
    options: { destination: '/tmp/last.log' }
  }]
})
const logger = pino(transport)
github-actions[bot] commented 2 years ago

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.