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.
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.
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.
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));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With