mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-15 03:45:03 +02:00
Add: Daily file logs and log server settings
This commit is contained in:
parent
2c6cfae6a1
commit
75ba884932
7 changed files with 274 additions and 16 deletions
124
server/LogManager.js
Normal file
124
server/LogManager.js
Normal file
|
@ -0,0 +1,124 @@
|
|||
const Path = require('path')
|
||||
const fs = require('fs-extra')
|
||||
|
||||
const DailyLog = require('./objects/DailyLog')
|
||||
|
||||
const Logger = require('./Logger')
|
||||
const { getFileSize } = require('./utils/fileUtils')
|
||||
|
||||
const TAG = '[LogManager]'
|
||||
|
||||
class LogManager {
|
||||
constructor(MetadataPath, db) {
|
||||
this.db = db
|
||||
this.MetadataPath = MetadataPath
|
||||
|
||||
this.logDirPath = Path.join(this.MetadataPath, 'logs')
|
||||
this.dailyLogDirPath = Path.join(this.logDirPath, 'daily')
|
||||
|
||||
this.currentDailyLog = null
|
||||
this.dailyLogBuffer = []
|
||||
this.dailyLogFiles = []
|
||||
}
|
||||
|
||||
get serverSettings() {
|
||||
return this.db.serverSettings || {}
|
||||
}
|
||||
|
||||
get loggerDailyLogsToKeep() {
|
||||
return this.serverSettings.loggerDailyLogsToKeep || 7
|
||||
}
|
||||
|
||||
async init() {
|
||||
// Load daily logs
|
||||
await this.scanLogFiles()
|
||||
|
||||
// Check remove extra daily logs
|
||||
if (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
|
||||
var dailyLogFilesCopy = [...this.dailyLogFiles]
|
||||
for (let i = 0; i < dailyLogFilesCopy.length - this.loggerDailyLogsToKeep; i++) {
|
||||
var logFileToRemove = dailyLogFilesCopy[i]
|
||||
await this.removeLogFile(logFileToRemove)
|
||||
}
|
||||
}
|
||||
|
||||
var currentDailyLogFilename = DailyLog.getCurrentDailyLogFilename()
|
||||
Logger.info(TAG, `Init current daily log filename: ${currentDailyLogFilename}`)
|
||||
|
||||
this.currentDailyLog = new DailyLog()
|
||||
this.currentDailyLog.setData({ dailyLogDirPath: this.dailyLogDirPath })
|
||||
|
||||
if (this.dailyLogFiles.includes(currentDailyLogFilename)) {
|
||||
Logger.debug(TAG, `Daily log file already exists - set in Logger`)
|
||||
this.currentDailyLog.loadLogs()
|
||||
} else {
|
||||
this.dailyLogFiles.push(this.currentDailyLog.filename)
|
||||
}
|
||||
|
||||
// Log buffered Logs
|
||||
if (this.dailyLogBuffer.length) {
|
||||
this.dailyLogBuffer.forEach((logObj) => this.currentDailyLog.appendLog(logObj))
|
||||
this.dailyLogBuffer = []
|
||||
}
|
||||
}
|
||||
|
||||
async scanLogFiles() {
|
||||
await fs.ensureDir(this.dailyLogDirPath)
|
||||
var dailyFiles = await fs.readdir(this.dailyLogDirPath)
|
||||
if (dailyFiles && dailyFiles.length) {
|
||||
dailyFiles.forEach((logFile) => {
|
||||
if (Path.extname(logFile) === '.txt') {
|
||||
Logger.info('Daily Log file found', logFile)
|
||||
this.dailyLogFiles.push(logFile)
|
||||
} else {
|
||||
Logger.debug(TAG, 'Unknown File in Daily log files dir', logFile)
|
||||
}
|
||||
})
|
||||
}
|
||||
this.dailyLogFiles.sort()
|
||||
}
|
||||
|
||||
async removeOldestLog() {
|
||||
if (!this.dailyLogFiles.length) return
|
||||
var oldestLog = this.dailyLogFiles[0]
|
||||
return this.removeLogFile(oldestLog)
|
||||
}
|
||||
|
||||
async removeLogFile(filename) {
|
||||
var fullPath = Path.join(this.dailyLogDirPath, filename)
|
||||
var exists = await fs.pathExists(fullPath)
|
||||
if (!exists) {
|
||||
Logger.error(TAG, 'Invalid log dne ' + fullPath)
|
||||
this.dailyLogFiles = this.dailyLogFiles.filter(dlf => dlf.filename !== filename)
|
||||
} else {
|
||||
try {
|
||||
await fs.unlink(fullPath)
|
||||
Logger.info(TAG, 'Removed daily log: ' + filename)
|
||||
this.dailyLogFiles = this.dailyLogFiles.filter(dlf => dlf.filename !== filename)
|
||||
} catch (error) {
|
||||
Logger.error(TAG, 'Failed to unlink log file ' + fullPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
logToFile(logObj) {
|
||||
if (!this.currentDailyLog) {
|
||||
this.dailyLogBuffer.push(logObj)
|
||||
return
|
||||
}
|
||||
|
||||
// Check log rolls to next day
|
||||
if (this.currentDailyLog.id !== DailyLog.getCurrentDateString()) {
|
||||
var newDailyLog = new DailyLog()
|
||||
newDailyLog.setData({ dailyLogDirPath: this.dailyLogDirPath })
|
||||
this.currentDailyLog = newDailyLog
|
||||
if (this.dailyLogFiles.length > this.loggerDailyLogsToKeep) {
|
||||
this.removeOldestLog()
|
||||
}
|
||||
}
|
||||
|
||||
// Append log line to log file
|
||||
this.currentDailyLog.appendLog(logObj)
|
||||
}
|
||||
}
|
||||
module.exports = LogManager
|
Loading…
Add table
Add a link
Reference in a new issue