Purge daily logs folder whenever a new one is created.

This commit is contained in:
sir-wilhelm 2026-01-27 22:48:02 -06:00
parent 8f8acc2271
commit ead270abbe

View file

@ -36,6 +36,12 @@ class LogManager {
return global.ServerSettings.loggerDailyLogsToKeep || 7 return global.ServerSettings.loggerDailyLogsToKeep || 7
} }
async enforceDailyLogRetention() {
while (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
await this.removeLogFile(this.dailyLogFiles[0])
}
}
async ensureLogDirs() { async ensureLogDirs() {
try { try {
await fs.ensureDir(this.DailyLogPath) await fs.ensureDir(this.DailyLogPath)
@ -49,8 +55,9 @@ class LogManager {
/** /**
* 1. Ensure log directories exist * 1. Ensure log directories exist
* 2. Load daily log files * 2. Load daily log files
* 3. Remove old daily log files * 3. Create/set current daily log file
* 4. Create/set current daily log file * 4. Remove old daily log files
* 5. Log any buffered daily logs
*/ */
async init() { async init() {
await this.ensureLogDirs() await this.ensureLogDirs()
@ -58,14 +65,6 @@ class LogManager {
// Load daily logs // Load daily logs
await this.scanLogFiles() await this.scanLogFiles()
// Check remove extra daily logs
if (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
const dailyLogFilesCopy = [...this.dailyLogFiles]
for (let i = 0; i < dailyLogFilesCopy.length - this.loggerDailyLogsToKeep; i++) {
await this.removeLogFile(dailyLogFilesCopy[i])
}
}
// set current daily log file or create if does not exist // set current daily log file or create if does not exist
const currentDailyLogFilename = DailyLog.getCurrentDailyLogFilename() const currentDailyLogFilename = DailyLog.getCurrentDailyLogFilename()
Logger.info(TAG, `Init current daily log filename: ${currentDailyLogFilename}`) Logger.info(TAG, `Init current daily log filename: ${currentDailyLogFilename}`)
@ -79,6 +78,9 @@ class LogManager {
this.dailyLogFiles.push(this.currentDailyLog.filename) this.dailyLogFiles.push(this.currentDailyLog.filename)
} }
// Check remove extra daily logs
await this.enforceDailyLogRetention()
// Log buffered daily logs // Log buffered daily logs
if (this.dailyLogBuffer.length) { if (this.dailyLogBuffer.length) {
this.dailyLogBuffer.forEach((logObj) => { this.dailyLogBuffer.forEach((logObj) => {
@ -146,10 +148,13 @@ class LogManager {
// Check log rolls to next day // Check log rolls to next day
if (this.currentDailyLog.id !== DailyLog.getCurrentDateString()) { if (this.currentDailyLog.id !== DailyLog.getCurrentDateString()) {
this.currentDailyLog = new DailyLog(this.DailyLogPath) this.currentDailyLog = new DailyLog(this.DailyLogPath)
if (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
// Remove oldest log // Track the new daily log file so retention works for long-running servers
this.removeLogFile(this.dailyLogFiles[0]) if (!this.dailyLogFiles.includes(this.currentDailyLog.filename)) {
this.dailyLogFiles.push(this.currentDailyLog.filename)
} }
await this.enforceDailyLogRetention()
} }
// Append log line to log file // Append log line to log file