mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2026-03-03 06:29:42 +00:00
Purge daily logs folder whenever a new one is created.
This commit is contained in:
parent
8f8acc2271
commit
ead270abbe
1 changed files with 18 additions and 13 deletions
|
|
@ -36,6 +36,12 @@ class LogManager {
|
|||
return global.ServerSettings.loggerDailyLogsToKeep || 7
|
||||
}
|
||||
|
||||
async enforceDailyLogRetention() {
|
||||
while (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
|
||||
await this.removeLogFile(this.dailyLogFiles[0])
|
||||
}
|
||||
}
|
||||
|
||||
async ensureLogDirs() {
|
||||
try {
|
||||
await fs.ensureDir(this.DailyLogPath)
|
||||
|
|
@ -49,8 +55,9 @@ class LogManager {
|
|||
/**
|
||||
* 1. Ensure log directories exist
|
||||
* 2. Load daily log files
|
||||
* 3. Remove old daily log files
|
||||
* 4. Create/set current daily log file
|
||||
* 3. Create/set current daily log file
|
||||
* 4. Remove old daily log files
|
||||
* 5. Log any buffered daily logs
|
||||
*/
|
||||
async init() {
|
||||
await this.ensureLogDirs()
|
||||
|
|
@ -58,14 +65,6 @@ class LogManager {
|
|||
// Load daily logs
|
||||
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
|
||||
const currentDailyLogFilename = DailyLog.getCurrentDailyLogFilename()
|
||||
Logger.info(TAG, `Init current daily log filename: ${currentDailyLogFilename}`)
|
||||
|
|
@ -79,6 +78,9 @@ class LogManager {
|
|||
this.dailyLogFiles.push(this.currentDailyLog.filename)
|
||||
}
|
||||
|
||||
// Check remove extra daily logs
|
||||
await this.enforceDailyLogRetention()
|
||||
|
||||
// Log buffered daily logs
|
||||
if (this.dailyLogBuffer.length) {
|
||||
this.dailyLogBuffer.forEach((logObj) => {
|
||||
|
|
@ -146,10 +148,13 @@ class LogManager {
|
|||
// Check log rolls to next day
|
||||
if (this.currentDailyLog.id !== DailyLog.getCurrentDateString()) {
|
||||
this.currentDailyLog = new DailyLog(this.DailyLogPath)
|
||||
if (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
|
||||
// Remove oldest log
|
||||
this.removeLogFile(this.dailyLogFiles[0])
|
||||
|
||||
// Track the new daily log file so retention works for long-running servers
|
||||
if (!this.dailyLogFiles.includes(this.currentDailyLog.filename)) {
|
||||
this.dailyLogFiles.push(this.currentDailyLog.filename)
|
||||
}
|
||||
|
||||
await this.enforceDailyLogRetention()
|
||||
}
|
||||
|
||||
// Append log line to log file
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue