pinojs / pino

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

How can I create a logger that outputs BOTH in the `console` and dumps it into a file? #704

Closed lffg closed 5 years ago

lffg commented 5 years ago

Hello guys,

I recently discovered this library, but I couldn't find any way to create a pino instance that outputs to the console (pretty) and into a file.

Is there a way to do that?

I tried this (and works), but it not pretty-prints into the console:

import { spawn } from 'child_process';
import path from 'path';
import { PassThrough } from 'stream';
import pino from 'pino';

const logPath = path.join.bind(undefined, __dirname, '..', 'temp', 'logs');

const logThrough = new PassThrough();

export const log = pino(
  {
    name: 'thod'
    // prettyPrint: {
    //   colorize: true,
    //   levelFirst: true,
    //   translateTime: true
    // }
  },
  logThrough
);

const child = spawn(
  process.execPath,
  [
    require.resolve('pino-tee'),
    'warn',
    logPath('warn.log'),
    'error',
    logPath('error.log'),
    'fatal',
    logPath('fatal.log')
  ],
  {
    cwd: process.cwd(),
    env: process.env
  }
);

logThrough.pipe(child.stdin);

When I uncomment those lines, the logger stops dumping into the files...

How can I solve that problem?

PS: I want to log into the console when process.env.NODE_ENV === 'development'.

Thanks in advance for any help or guiding. :)

mcollina commented 5 years ago

check out https://github.com/pinojs/pino-multi-stream

lffg commented 5 years ago

Thanks! o/

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.