Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging to STDOUT and a file with node/pino

I am sharing this as I struggled to get a pino logger to write to both STDOUT and a log file:

const dest = new stream.PassThrough();
dest.pipe(process.stdout);
dest.pipe(fs.createWriteStream('/logs/file.log', { flags: 'a' }));
const logger = pino({ level: 'info' }, dest);

As this appears very low level, I wonder if this is the right way to do this.

like image 691
Olivier D. Avatar asked Aug 21 '20 12:08

Olivier D.


People also ask

What is express Pino logger?

An express middleware to log with pino. Incidentally, it also works without express. To our knowledge, express-pino-logger is the fastest express logger in town.

What is Pino pretty?

This module provides a basic ndjson formatter to be used in development. If an incoming line looks like it could be a log line from an ndjson logger, in particular the Pino logging library, then it will apply extra formatting by considering things like the log level and timestamp.


1 Answers

A bit late but pino-multi-stream may be what you want. I followed this section here and it works for me in TypeScript for my purpose. You could try something like this:

const streams = [
  { stream: process.stdout },
  { stream: fs.createWriteStream('/logs/file.log', { flags: 'a' }) },
]

const logger = pino({ level: 'info' }, pinoms.multistream(streams));
like image 124
wtwtwt Avatar answered Sep 29 '22 21:09

wtwtwt