pinojs / pino

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

Log to command line in dev when using streams #334

Closed robcalcroft closed 6 years ago

robcalcroft commented 6 years ago

Currently we use

const pino = require('pino')(fs.createWriteStream(path.resolve(__dirname, '../../app.log')));
module.exports = pino;

to expose the logger, but I'd like to be able to have pino log to command line in development where performance is not an issue, is this possible?

jsumners commented 6 years ago

You should be handling logs downstream, e.g. node your_app.js > out.log. Thus, you can:

const isdebug = require('isdebug')
const pino = require('pino')
module.exports = pino({
  level: (isdebug) ? 'debug' : 'info',
  prettyPrint: isdebug
})
robcalcroft commented 6 years ago

Right ok, i'll work out a solution from that. Thanks 😄

marcbachmann commented 6 years ago

More examples I'm using in a few projects:

const pino = require('pino')

module.exports = function (config) {
  const pinoConfig = {
    level: config.level
  }

  if (config.pretty) {
    pinoConfig.prettyPrint = {
      forceColor: true,
      formatter: require('./dev_string_formatter')
    }
  }

  return pino(pinoConfig)
}
const pino = require('pino')
module.exports = function loggerFactory (logOptions = {}) {
  if ([false, 'false'].includes(logOptions.enabled)) return pino({enabled: false})
  if (![true, 'true'].includes(logOptions.pretty)) return pino(logOptions)

  const stream = require('pino-colada')()
  stream.pipe(process.stdout)
  return pino(logOptions, stream)
}
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.