Morgan 和 Rotating-file-stream:间隔不能正常工作
Morgan and Rotating-file-stream: interval doesn't work normally
你好,这是我的问题,最近,我在 2021/10/01 部署了一个服务器,我在那里保存了访问日志。我正在使用 morgan library to do this and rotating-file-stream 进行文件轮换。然而,经过几天的activity,我发现旋转没有正确完成。
这是我的代码:
const morgan = require('morgan');
const rfs = require('rotating-file-stream');
const path = require('path');
const { format } = require('date-fns');
function logFilename() {
return `${format(new Date(), 'yyyy-MM-dd')}-access.log`;
}
const accessLogStream = rfs.createStream(logFilename, {
interval: '1d', // rotate daily
path: path.resolve(__dirname, '..', 'log'),
});
morgan.token('ip', (req) => req.headers['x-forwarded-for'] || req.connection.remoteAddress);
morgan.token('user', (req) => {
if (req.user) return req.user;
return '-';
});
module.exports = morgan(':ip ":user" [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"', { stream: accessLogStream });
以下是我可以在日志文件中找到的示例(您必须查看日期级别)。
2021-10-03-access.log
:ip ":user" [01/Oct/2021:07:30:52 +0000] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
...
...
:ip ":user" [02/Oct/2021:23:56:26 +0000] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
2021-10-04-access.log
:ip ":user" [03/Oct/2021:00:01:26 +0000] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
...
...
:ip ":user" [03/Oct/2021:23:56:26 +0000] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
2021-10-05-access.log
:ip ":user" [04/Oct/2021:00:01:26 +0000] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
...
...
:ip ":user" [05/Oct/2021:06:03:02 +0000] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
我在 rotating-file-stream doc 上看到如果我们将“1d”放在间隔参数中,它会在每个午夜轮换,但也许我忘记了什么。
提前感谢您的关注和未来的回答。
我忘记了文件名生成器的一个条件和一个参数
function logFilename(time) {
if (!time) return 'access.log';
return `${format(time, 'yyyy-MM-dd')}-access.log`;
}
你好,这是我的问题,最近,我在 2021/10/01 部署了一个服务器,我在那里保存了访问日志。我正在使用 morgan library to do this and rotating-file-stream 进行文件轮换。然而,经过几天的activity,我发现旋转没有正确完成。
这是我的代码:
const morgan = require('morgan');
const rfs = require('rotating-file-stream');
const path = require('path');
const { format } = require('date-fns');
function logFilename() {
return `${format(new Date(), 'yyyy-MM-dd')}-access.log`;
}
const accessLogStream = rfs.createStream(logFilename, {
interval: '1d', // rotate daily
path: path.resolve(__dirname, '..', 'log'),
});
morgan.token('ip', (req) => req.headers['x-forwarded-for'] || req.connection.remoteAddress);
morgan.token('user', (req) => {
if (req.user) return req.user;
return '-';
});
module.exports = morgan(':ip ":user" [:date[clf]] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"', { stream: accessLogStream });
以下是我可以在日志文件中找到的示例(您必须查看日期级别)。
2021-10-03-access.log
:ip ":user" [01/Oct/2021:07:30:52 +0000] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
...
...
:ip ":user" [02/Oct/2021:23:56:26 +0000] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
2021-10-04-access.log
:ip ":user" [03/Oct/2021:00:01:26 +0000] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
...
...
:ip ":user" [03/Oct/2021:23:56:26 +0000] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
2021-10-05-access.log
:ip ":user" [04/Oct/2021:00:01:26 +0000] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
...
...
:ip ":user" [05/Oct/2021:06:03:02 +0000] ":method :url HTTP/:http-version" :status :res[content-length] ":referrer" ":user-agent"
我在 rotating-file-stream doc 上看到如果我们将“1d”放在间隔参数中,它会在每个午夜轮换,但也许我忘记了什么。
提前感谢您的关注和未来的回答。
我忘记了文件名生成器的一个条件和一个参数
function logFilename(time) {
if (!time) return 'access.log';
return `${format(time, 'yyyy-MM-dd')}-access.log`;
}