Add some log rotation stuff

This commit is contained in:
Christopher Cookman 2024-12-23 09:49:27 -07:00
parent 1a48cd8ca0
commit f583615a46
2 changed files with 23 additions and 1 deletions

4
.gitignore vendored
View file

@ -129,4 +129,6 @@ dist
.yarn/install-state.gz
.pnp.*
uploads/
uploads/
access.log*

View file

@ -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;