Add some log rotation stuff
This commit is contained in:
parent
1a48cd8ca0
commit
f583615a46
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -130,3 +130,5 @@ dist
|
|||
.pnp.*
|
||||
|
||||
uploads/
|
||||
|
||||
access.log*
|
20
index.js
20
index.js
|
@ -47,6 +47,26 @@ const ejs = require("ejs")
|
|||
app.set('view engine', 'ejs');
|
||||
app.set('views', __dirname + '/views');
|
||||
|
||||
// Rotate log files if they exist
|
||||
if (process.env.LOGFILE && fs.existsSync(process.env.LOGFILE)) {
|
||||
const logFilePath = process.env.LOGFILE;
|
||||
const maxLogFiles = 5; // Maximum number of rotated log files to keep
|
||||
|
||||
// Move old log files up
|
||||
for (let i = maxLogFiles - 1; i > 0; i--) {
|
||||
const oldLogFile = `${logFilePath}.${i}`;
|
||||
const newLogFile = `${logFilePath}.${i + 1}`;
|
||||
if (fs.existsSync(oldLogFile)) {
|
||||
fs.renameSync(oldLogFile, newLogFile);
|
||||
}
|
||||
}
|
||||
|
||||
// Move current log file to .1
|
||||
if (fs.existsSync(logFilePath)) {
|
||||
fs.renameSync(logFilePath, `${logFilePath}.1`);
|
||||
}
|
||||
}
|
||||
|
||||
app.use((req, res, next) => {
|
||||
if (!process.env.LOGFILE) return next();
|
||||
var requestIp = req.ip;
|
||||
|
|
Loading…
Reference in a new issue