2021-10-31 23:55:28 +01:00
|
|
|
const Path = require('path')
|
|
|
|
const fs = require('fs-extra')
|
|
|
|
|
2022-03-20 22:41:06 +01:00
|
|
|
const DailyLog = require('../objects/DailyLog')
|
2021-10-31 23:55:28 +01:00
|
|
|
|
2022-03-20 22:41:06 +01:00
|
|
|
const Logger = require('../Logger')
|
2021-10-31 23:55:28 +01:00
|
|
|
|
|
|
|
const TAG = '[LogManager]'
|
|
|
|
|
|
|
|
class LogManager {
|
2022-02-27 20:47:52 +01:00
|
|
|
constructor(db) {
|
2021-10-31 23:55:28 +01:00
|
|
|
this.db = db
|
|
|
|
|
2022-02-27 20:47:52 +01:00
|
|
|
this.logDirPath = Path.join(global.MetadataPath, 'logs')
|
2021-10-31 23:55:28 +01:00
|
|
|
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`)
|
2021-11-01 01:10:45 +01:00
|
|
|
await this.currentDailyLog.loadLogs()
|
2021-10-31 23:55:28 +01:00
|
|
|
} else {
|
|
|
|
this.dailyLogFiles.push(this.currentDailyLog.filename)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log buffered Logs
|
|
|
|
if (this.dailyLogBuffer.length) {
|
2021-11-01 01:10:45 +01:00
|
|
|
this.dailyLogBuffer.forEach((logObj) => {
|
|
|
|
this.currentDailyLog.appendLog(logObj)
|
|
|
|
})
|
2021-10-31 23:55:28 +01:00
|
|
|
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') {
|
2021-11-01 01:10:45 +01:00
|
|
|
Logger.debug('Daily Log file found', logFile)
|
2021-10-31 23:55:28 +01:00
|
|
|
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)
|
|
|
|
}
|
2021-11-01 01:10:45 +01:00
|
|
|
|
|
|
|
socketRequestDailyLogs(socket) {
|
|
|
|
if (!this.currentDailyLog) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var lastLogs = this.currentDailyLog.logs.slice(-5000)
|
|
|
|
socket.emit('daily_logs', lastLogs)
|
|
|
|
}
|
2021-10-31 23:55:28 +01:00
|
|
|
}
|
|
|
|
module.exports = LogManager
|