NodeJS12 Morgan 记录器跳过不使用旋转文件流
NodeJS12 Morgan logger skip not working with rotating file stream
我将 NodeJS 与 morgan 一起用于日志记录和旋转文件流来创建日志文件。我想设置应用程序以创建 apache 组合格式的轮换日志文件。代码正在运行,日志已在所需目录中创建。但是,跳过功能不起作用。发送到 /api/healthcheck
的请求仍然出现在日志中。我已经确认 'skip' url 是正确的并且在发送对 url 的请求时与 originalUrl 匹配,并且我已经确认该函数实际上 return true 时运行状况检查路由被调用。
const logger = require('morgan');
/* Logger */
// Logger skip url
const loggerSkipUrl = '/api/healthcheck';
// Create a rotating write stream
let accessLogStream = rfs.createStream('server.log', {
interval: '1d', // Rotate daily
maxFiles: 30, // Maximum number of rotated files to keep in storage
compress: 'gzip', // Compress rotated files
path: path.join(__dirname, 'logs'),
})
// Log requests in the Apache combined format to one log file per day
app.use(logger('combined',
{ stream: accessLogStream }, // Works but doesn't skip
{ skip: (req, res) => req.originalUrl === loggerSkipUrl }, // Does not skip
{ flags: 'a' },
));
更新:我仍然遇到这个问题。它似乎与使用流选项有关。例如,不带 stream 选项的日志记录会正确跳过 healthcheck 路由,但是当使用 stream 选项时,它不会跳过。这是配置问题还是 Morgan 记录器模块中的错误?
function skipLog(req, res) {
const answer = req.originalUrl.indexOf('healthcheck') > -1;
console.log(`skipLog ${answer}`);
return answer;
}
/* SKIP WORKS WITHOUT STREAM OPTION */
// Log API requests to console
app.use(requestLogger('dev', { skip: skipLog }));
/* SKIP DOES NOT WORK WITH STREAM OPTION */
requestLogStream = rfs.createStream('request.log', {
interval: '1d', // Rotate daily
maxFiles: 30, // Maximum number of rotated files to keep in storage
path: path.join(__dirname, `logs/${hostname}`)
});
// Log API requests in the Apache combined format to one log file per day
app.use(requestLogger('combined',
{ stream: requestLogStream },
{ flags: 'a' },
{ skip: skipLog },
));
查看 code of morgan 您会看到 skip
和 stream
一起受支持。
问题是如何将选项提供给工厂函数:
app.use(requestLogger('combined',
{
stream: requestLogStream,
flags: 'a',
skip: skipLog
})
)
必须是单个对象
我将 NodeJS 与 morgan 一起用于日志记录和旋转文件流来创建日志文件。我想设置应用程序以创建 apache 组合格式的轮换日志文件。代码正在运行,日志已在所需目录中创建。但是,跳过功能不起作用。发送到 /api/healthcheck
的请求仍然出现在日志中。我已经确认 'skip' url 是正确的并且在发送对 url 的请求时与 originalUrl 匹配,并且我已经确认该函数实际上 return true 时运行状况检查路由被调用。
const logger = require('morgan');
/* Logger */
// Logger skip url
const loggerSkipUrl = '/api/healthcheck';
// Create a rotating write stream
let accessLogStream = rfs.createStream('server.log', {
interval: '1d', // Rotate daily
maxFiles: 30, // Maximum number of rotated files to keep in storage
compress: 'gzip', // Compress rotated files
path: path.join(__dirname, 'logs'),
})
// Log requests in the Apache combined format to one log file per day
app.use(logger('combined',
{ stream: accessLogStream }, // Works but doesn't skip
{ skip: (req, res) => req.originalUrl === loggerSkipUrl }, // Does not skip
{ flags: 'a' },
));
更新:我仍然遇到这个问题。它似乎与使用流选项有关。例如,不带 stream 选项的日志记录会正确跳过 healthcheck 路由,但是当使用 stream 选项时,它不会跳过。这是配置问题还是 Morgan 记录器模块中的错误?
function skipLog(req, res) {
const answer = req.originalUrl.indexOf('healthcheck') > -1;
console.log(`skipLog ${answer}`);
return answer;
}
/* SKIP WORKS WITHOUT STREAM OPTION */
// Log API requests to console
app.use(requestLogger('dev', { skip: skipLog }));
/* SKIP DOES NOT WORK WITH STREAM OPTION */
requestLogStream = rfs.createStream('request.log', {
interval: '1d', // Rotate daily
maxFiles: 30, // Maximum number of rotated files to keep in storage
path: path.join(__dirname, `logs/${hostname}`)
});
// Log API requests in the Apache combined format to one log file per day
app.use(requestLogger('combined',
{ stream: requestLogStream },
{ flags: 'a' },
{ skip: skipLog },
));
查看 code of morgan 您会看到 skip
和 stream
一起受支持。
问题是如何将选项提供给工厂函数:
app.use(requestLogger('combined',
{
stream: requestLogStream,
flags: 'a',
skip: skipLog
})
)
必须是单个对象