如何轮转使用 pino-multi-stream 生成的日志文件?
How to rotate log files that generate with pino-multi-stream?
我在我的 node.js 项目中使用 pino-multi-stream 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 包如“file-stream-rotator”)?
只需传递 file-stream-rotator 即可将您带到 pino-multi-stream 的流。
{ level: 'info', stream: fs.createWriteStream(`${logDirectory}/info.log`) },
至
{ level: 'info', stream: require('file-stream-rotator').getStream({filename:`${logDirectory}/info.log`, frequency:"daily"}) },
我在我的 node.js 项目中使用 pino-multi-stream 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 包如“file-stream-rotator”)?
只需传递 file-stream-rotator 即可将您带到 pino-multi-stream 的流。
{ level: 'info', stream: fs.createWriteStream(`${logDirectory}/info.log`) },
至
{ level: 'info', stream: require('file-stream-rotator').getStream({filename:`${logDirectory}/info.log`, frequency:"daily"}) },