You can log to multiple files by spawning a child process. In the following
example pino-tee writes into three different files for warn, error & fatal log
levels.
const pino = require('pino')const childProcess = require('child_process')const stream = require('stream')// Environment variablesconst cwd = process.cwd()const { env } = processconst logPath = `${cwd}/log`// Create a stream where the logs will be writtenconst logThrough = new stream.PassThrough()const log = pino({ name: 'project' }, logThrough)// Log to multiple files using a separate process
This prints raw logs into log files, you can also print pretty logs to the
console for development purposes. For that, you need to use
pino-multi-stream. See the example below
Here, we're tapping into the write stream that pino-tee gets and manually
formatting the log line using pino-pretty to write on the stdout. Note that
the pretty printing is typically only done while developing.
API
pinoTee(source)
Create a new tee instance from source. It is an extended instance of
cloneable-readable.
Example:
const { tee } = require('pino-tee')const fs = require('fs')const stream = tee(process.stdin)stream.tee(fs.createWriteStream('errors'), line => line.level >= 50)stream.pipe(process.stdout)
stream.tee(dest, [filter])
Create a new stream that will filter a given line based on some
parameters. Each line is automatically parsed, or skipped if it is not
a newline delimited json.
The filter can be a function with signature filter(line), where
line is a parsed JSON object. The filter can also be one of the
pino levels either
as text or as a custom level number, in that case all log lines with
that level or greater will be written.