mirror of
				https://github.com/Kareadita/Kavita.git
				synced 2025-11-04 03:27:05 -05: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 ILogger<BackupService> _logger;
 | 
				
			||||||
        private readonly IDirectoryService _directoryService;
 | 
					        private readonly IDirectoryService _directoryService;
 | 
				
			||||||
        private readonly string _tempDirectory = Path.Join(Directory.GetCurrentDirectory(), "temp");
 | 
					        private readonly string _tempDirectory = Path.Join(Directory.GetCurrentDirectory(), "temp");
 | 
				
			||||||
 | 
					        private readonly string _logDirectory = Path.Join(Directory.GetCurrentDirectory(), "logs");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        private readonly IList<string> _backupFiles;
 | 
					        private readonly IList<string> _backupFiles;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -28,7 +29,7 @@ namespace API.Services.Tasks
 | 
				
			|||||||
            _unitOfWork = unitOfWork;
 | 
					            _unitOfWork = unitOfWork;
 | 
				
			||||||
            _logger = logger;
 | 
					            _logger = logger;
 | 
				
			||||||
            _directoryService = directoryService;
 | 
					            _directoryService = directoryService;
 | 
				
			||||||
            
 | 
					
 | 
				
			||||||
            var maxRollingFiles = config.GetMaxRollingFiles();
 | 
					            var maxRollingFiles = config.GetMaxRollingFiles();
 | 
				
			||||||
            var loggingSection = config.GetLoggingFileName();
 | 
					            var loggingSection = config.GetLoggingFileName();
 | 
				
			||||||
            var files = LogFiles(maxRollingFiles, loggingSection);
 | 
					            var files = LogFiles(maxRollingFiles, loggingSection);
 | 
				
			||||||
@ -53,7 +54,7 @@ namespace API.Services.Tasks
 | 
				
			|||||||
            var fi = new FileInfo(logFileName);
 | 
					            var fi = new FileInfo(logFileName);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            var files = maxRollingFiles > 0
 | 
					            var files = maxRollingFiles > 0
 | 
				
			||||||
                ? _directoryService.GetFiles(Directory.GetCurrentDirectory(), $@"{fi.Name}{multipleFileRegex}\.log")
 | 
					                ? _directoryService.GetFiles(_logDirectory, $@"{Path.GetFileNameWithoutExtension(fi.Name)}{multipleFileRegex}\.log")
 | 
				
			||||||
                : new[] {"kavita.log"};
 | 
					                : new[] {"kavita.log"};
 | 
				
			||||||
            return files;
 | 
					            return files;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@ -63,17 +64,17 @@ namespace API.Services.Tasks
 | 
				
			|||||||
        {
 | 
					        {
 | 
				
			||||||
            _logger.LogInformation("Beginning backup of Database at {BackupTime}", DateTime.Now);
 | 
					            _logger.LogInformation("Beginning backup of Database at {BackupTime}", DateTime.Now);
 | 
				
			||||||
            var backupDirectory = Task.Run(() => _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BackupDirectory)).Result.Value;
 | 
					            var backupDirectory = Task.Run(() => _unitOfWork.SettingsRepository.GetSettingAsync(ServerSettingKey.BackupDirectory)).Result.Value;
 | 
				
			||||||
            
 | 
					
 | 
				
			||||||
            _logger.LogDebug("Backing up to {BackupDirectory}", backupDirectory);
 | 
					            _logger.LogDebug("Backing up to {BackupDirectory}", backupDirectory);
 | 
				
			||||||
            if (!DirectoryService.ExistOrCreate(backupDirectory))
 | 
					            if (!DirectoryService.ExistOrCreate(backupDirectory))
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                _logger.LogError("Could not write to {BackupDirectory}; aborting backup", backupDirectory);
 | 
					                _logger.LogError("Could not write to {BackupDirectory}; aborting backup", backupDirectory);
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            
 | 
					
 | 
				
			||||||
            var dateString = DateTime.Now.ToShortDateString().Replace("/", "_");
 | 
					            var dateString = DateTime.Now.ToShortDateString().Replace("/", "_");
 | 
				
			||||||
            var zipPath = Path.Join(backupDirectory, $"kavita_backup_{dateString}.zip");
 | 
					            var zipPath = Path.Join(backupDirectory, $"kavita_backup_{dateString}.zip");
 | 
				
			||||||
            
 | 
					
 | 
				
			||||||
            if (File.Exists(zipPath))
 | 
					            if (File.Exists(zipPath))
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                _logger.LogInformation("{ZipFile} already exists, aborting", zipPath);
 | 
					                _logger.LogInformation("{ZipFile} already exists, aborting", zipPath);
 | 
				
			||||||
@ -83,7 +84,7 @@ namespace API.Services.Tasks
 | 
				
			|||||||
            var tempDirectory = Path.Join(_tempDirectory, dateString);
 | 
					            var tempDirectory = Path.Join(_tempDirectory, dateString);
 | 
				
			||||||
            DirectoryService.ExistOrCreate(tempDirectory);
 | 
					            DirectoryService.ExistOrCreate(tempDirectory);
 | 
				
			||||||
            DirectoryService.ClearDirectory(tempDirectory);
 | 
					            DirectoryService.ClearDirectory(tempDirectory);
 | 
				
			||||||
            
 | 
					
 | 
				
			||||||
            _directoryService.CopyFilesToDirectory(
 | 
					            _directoryService.CopyFilesToDirectory(
 | 
				
			||||||
                _backupFiles.Select(file => Path.Join(Directory.GetCurrentDirectory(), file)).ToList(), tempDirectory);
 | 
					                _backupFiles.Select(file => Path.Join(Directory.GetCurrentDirectory(), file)).ToList(), tempDirectory);
 | 
				
			||||||
            try
 | 
					            try
 | 
				
			||||||
@ -142,10 +143,10 @@ namespace API.Services.Tasks
 | 
				
			|||||||
                        _logger.LogError(ex, "There was an issue deleting {FileName}", file.Name);
 | 
					                        _logger.LogError(ex, "There was an issue deleting {FileName}", file.Name);
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                
 | 
					
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            _logger.LogInformation("Finished cleanup of Database backups at {Time}", DateTime.Now);
 | 
					            _logger.LogInformation("Finished cleanup of Database backups at {Time}", DateTime.Now);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        
 | 
					
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -17,10 +17,10 @@
 | 
				
			|||||||
      "Microsoft.AspNetCore.Hosting.Internal.WebHost": "Information"
 | 
					      "Microsoft.AspNetCore.Hosting.Internal.WebHost": "Information"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "File": {
 | 
					    "File": {
 | 
				
			||||||
      "Path": "kavita.log",
 | 
					      "Path": "logs/kavita.log",
 | 
				
			||||||
      "Append": "True",
 | 
					      "Append": "True",
 | 
				
			||||||
      "FileSizeLimitBytes": 0,
 | 
					      "FileSizeLimitBytes": 10485760,
 | 
				
			||||||
      "MaxRollingFiles": 0
 | 
					      "MaxRollingFiles": 5
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "Port": 5000
 | 
					  "Port": 5000
 | 
				
			||||||
 | 
				
			|||||||
@ -46,6 +46,20 @@ else
 | 
				
			|||||||
	ln -s /kavita/data/cache /kavita/cache
 | 
						ln -s /kavita/data/cache /kavita/cache
 | 
				
			||||||
fi
 | 
					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 ]
 | 
					if [ -d /kavita/data/stats ]
 | 
				
			||||||
then
 | 
					then
 | 
				
			||||||
	if [ -d /kavita/stats ]
 | 
						if [ -d /kavita/stats ]
 | 
				
			||||||
@ -60,25 +74,6 @@ else
 | 
				
			|||||||
	ln -s /kavita/data/stats /kavita/stats
 | 
						ln -s /kavita/data/stats /kavita/stats
 | 
				
			||||||
fi
 | 
					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
 | 
					chmod +x ./Kavita
 | 
				
			||||||
 | 
					
 | 
				
			||||||
./Kavita
 | 
					./Kavita
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user