From 590ee771c117e3cbe83c470cb21c279e2227ed37 Mon Sep 17 00:00:00 2001 From: Joseph Milazzo Date: Wed, 28 Jul 2021 15:07:06 -0500 Subject: [PATCH] Refactored logs to use a logs/ folder and also roll over after 10MB. A maximum of 5 logs will persist (50MB of log data). --- API/Services/Tasks/BackupService.cs | 19 ++++++++++--------- API/appsettings.Development.json | 6 +++--- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/API/Services/Tasks/BackupService.cs b/API/Services/Tasks/BackupService.cs index a5b115c3f..e04e5373f 100644 --- a/API/Services/Tasks/BackupService.cs +++ b/API/Services/Tasks/BackupService.cs @@ -20,6 +20,7 @@ namespace API.Services.Tasks private readonly ILogger _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 _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); } - + } -} \ No newline at end of file +} diff --git a/API/appsettings.Development.json b/API/appsettings.Development.json index 35e9218b9..9d93e100a 100644 --- a/API/appsettings.Development.json +++ b/API/appsettings.Development.json @@ -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