mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
Refactored logs to use a logs/ folder and also roll over after 10MB. … (#448)
# Changed - Changed: Log files now roll (kavita, kavita1, etc) up to 5 files, each with a max of 10MB each. After all 5 files fill up, they will roll over. (Closes #446 ) ============================= * Refactored logs to use a logs/ folder and also roll over after 10MB. A maximum of 5 logs will persist (50MB of log data). * Updated entrypoint to accommodate rolling logs Co-authored-by: Chris Plaatjes <kizaing@gmail.com>
This commit is contained in:
parent
58856c0d70
commit
01007dee28
@ -20,6 +20,7 @@ namespace API.Services.Tasks
|
||||
private readonly ILogger<BackupService> _logger;
|
||||
private readonly IDirectoryService _directoryService;
|
||||
private readonly string _tempDirectory = Path.Join(Directory.GetCurrentDirectory(), "temp");
|
||||
private readonly string _logDirectory = Path.Join(Directory.GetCurrentDirectory(), "logs");
|
||||
|
||||
private readonly IList<string> _backupFiles;
|
||||
|
||||
@ -28,7 +29,7 @@ namespace API.Services.Tasks
|
||||
_unitOfWork = unitOfWork;
|
||||
_logger = logger;
|
||||
_directoryService = directoryService;
|
||||
|
||||
|
||||
var maxRollingFiles = config.GetMaxRollingFiles();
|
||||
var loggingSection = config.GetLoggingFileName();
|
||||
var files = LogFiles(maxRollingFiles, loggingSection);
|
||||
@ -53,7 +54,7 @@ namespace API.Services.Tasks
|
||||
var fi = new FileInfo(logFileName);
|
||||
|
||||
var files = maxRollingFiles > 0
|
||||
? _directoryService.GetFiles(Directory.GetCurrentDirectory(), $@"{fi.Name}{multipleFileRegex}\.log")
|
||||
? _directoryService.GetFiles(_logDirectory, $@"{Path.GetFileNameWithoutExtension(fi.Name)}{multipleFileRegex}\.log")
|
||||
: new[] {"kavita.log"};
|
||||
return files;
|
||||
}
|
||||
@ -63,17 +64,17 @@ namespace API.Services.Tasks
|
||||
{
|
||||
_logger.LogInformation("Beginning backup of Database at {BackupTime}", DateTime.Now);
|
||||
var backupDirectory = Task.Run(() => _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BackupDirectory)).Result.Value;
|
||||
|
||||
|
||||
_logger.LogDebug("Backing up to {BackupDirectory}", backupDirectory);
|
||||
if (!DirectoryService.ExistOrCreate(backupDirectory))
|
||||
{
|
||||
_logger.LogError("Could not write to {BackupDirectory}; aborting backup", backupDirectory);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
var dateString = DateTime.Now.ToShortDateString().Replace("/", "_");
|
||||
var zipPath = Path.Join(backupDirectory, $"kavita_backup_{dateString}.zip");
|
||||
|
||||
|
||||
if (File.Exists(zipPath))
|
||||
{
|
||||
_logger.LogInformation("{ZipFile} already exists, aborting", zipPath);
|
||||
@ -83,7 +84,7 @@ namespace API.Services.Tasks
|
||||
var tempDirectory = Path.Join(_tempDirectory, dateString);
|
||||
DirectoryService.ExistOrCreate(tempDirectory);
|
||||
DirectoryService.ClearDirectory(tempDirectory);
|
||||
|
||||
|
||||
_directoryService.CopyFilesToDirectory(
|
||||
_backupFiles.Select(file => Path.Join(Directory.GetCurrentDirectory(), file)).ToList(), tempDirectory);
|
||||
try
|
||||
@ -142,10 +143,10 @@ namespace API.Services.Tasks
|
||||
_logger.LogError(ex, "There was an issue deleting {FileName}", file.Name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
_logger.LogInformation("Finished cleanup of Database backups at {Time}", DateTime.Now);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -17,10 +17,10 @@
|
||||
"Microsoft.AspNetCore.Hosting.Internal.WebHost": "Information"
|
||||
},
|
||||
"File": {
|
||||
"Path": "kavita.log",
|
||||
"Path": "logs/kavita.log",
|
||||
"Append": "True",
|
||||
"FileSizeLimitBytes": 0,
|
||||
"MaxRollingFiles": 0
|
||||
"FileSizeLimitBytes": 10485760,
|
||||
"MaxRollingFiles": 5
|
||||
}
|
||||
},
|
||||
"Port": 5000
|
||||
|
@ -46,6 +46,20 @@ else
|
||||
ln -s /kavita/data/cache /kavita/cache
|
||||
fi
|
||||
|
||||
if [ -d /kavita/data/logs ]
|
||||
then
|
||||
if [ -d /kavita/logs ]
|
||||
then
|
||||
unlink /kavita/logs
|
||||
ln -s /kavita/data/logs /kavita/logs
|
||||
else
|
||||
ln -s /kavita/data/logs /kavita/logs
|
||||
fi
|
||||
else
|
||||
mkdir /kavita/data/logs
|
||||
ln -s /kavita/data/logs /kavita/logs
|
||||
fi
|
||||
|
||||
if [ -d /kavita/data/stats ]
|
||||
then
|
||||
if [ -d /kavita/stats ]
|
||||
@ -60,25 +74,6 @@ else
|
||||
ln -s /kavita/data/stats /kavita/stats
|
||||
fi
|
||||
|
||||
# Checks for the log file
|
||||
|
||||
if test -f "/kavita/data/logs/kavita.log"
|
||||
then
|
||||
rm /kavita/kavita.log
|
||||
ln -s /kavita/data/logs/kavita.log /kavita/
|
||||
else
|
||||
if [ -d /kavita/data/logs ]
|
||||
then
|
||||
echo "" > /kavita/data/logs/kavita.log || true
|
||||
ln -s /kavita/data/logs/kavita.log /kavita/
|
||||
else
|
||||
mkdir /kavita/data/logs
|
||||
echo "" > /kavita/data/logs/kavita.log || true
|
||||
ln -s /kavita/data/logs/kavita.log /kavita/
|
||||
fi
|
||||
|
||||
fi
|
||||
|
||||
chmod +x ./Kavita
|
||||
|
||||
./Kavita
|
||||
|
Loading…
x
Reference in New Issue
Block a user