From f583615a46a73583d74a21d4da64b39114db30c4 Mon Sep 17 00:00:00 2001 From: ChrisChrome Date: Mon, 23 Dec 2024 09:49:27 -0700 Subject: [PATCH] Add some log rotation stuff --- .gitignore | 4 +++- index.js | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 96e3b38..bca1e9f 100644 --- a/.gitignore +++ b/.gitignore @@ -129,4 +129,6 @@ dist .yarn/install-state.gz .pnp.* -uploads/ \ No newline at end of file +uploads/ + +access.log* \ No newline at end of file diff --git a/index.js b/index.js index cbbb258..71effe0 100644 --- a/index.js +++ b/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;