我在node.js项目中使用pino多流npm包,如下所示:
const fs = require('fs');
const pino = require('pino');
const multistream = require('pino-multi-stream').multistream;
const path = require('path');
const logDirectory = path.join(__dirname, '..', 'logs');
const streams = [
{ stream: fs.createWriteStream(`${logDirectory}/info.log`) },
{ level: 'info', stream: fs.createWriteStream(`${logDirectory}/info.log`) },
{ level: 'debug', stream: fs.createWriteStream(`${logDirectory}/debug.log`) },
{ level: 'warn', stream: fs.createWriteStream(`${logDirectory}/warn.log`) },
{ level: 'error', stream: fs.createWriteStream(`${logDirectory}/error.log`) },
{ level: 'fatal', stream: fs.createWriteStream(`${logDirectory}/fatal.log`) }
];
const logger = pino({ level: 'debug' }, multistream(streams));
export { pino, logger };
并且使用记录器,如:
logger.info(`Ready on localhost:${port}`);
logger.error('error message');
logger.warn('warning message');
...
现在,我的项目中的日志目录中有多个日志文件。
我的问题是:
如何旋转这些文件(如"每日"旋转,使用"文件流旋转器"等npm包(?
只需将文件流旋转器的流传递到pino多流。
{ level: 'info', stream: fs.createWriteStream(`${logDirectory}/info.log`) },
至
{ level: 'info', stream: require('file-stream-rotator').getStream({filename:`${logDirectory}/info.log`, frequency:"daily"}) },