mirror of
https://github.com/EggLinks/DanhengServer-OpenSource.git
synced 2026-01-02 20:26:03 +08:00
feat: pack the old log files when launching
This commit is contained in:
@@ -5,19 +5,27 @@ namespace EggLink.DanhengServer.Util;
|
||||
|
||||
public class Logger(string moduleName)
|
||||
{
|
||||
private static FileInfo? LogFile;
|
||||
private static readonly object _lock = new();
|
||||
private readonly string ModuleName = moduleName;
|
||||
private static FileInfo? _logFile;
|
||||
private static FileInfo? _debugLogFile;
|
||||
private static readonly object Lock = new();
|
||||
|
||||
public void Log(string message, LoggerLevel level)
|
||||
{
|
||||
lock (_lock)
|
||||
lock (Lock)
|
||||
{
|
||||
AnsiConsole.Write(new Markup($"[[[bold deepskyblue3_1]{DateTime.Now:HH:mm:ss}[/]]] " +
|
||||
$"[[[gray]{ModuleName}[/]]] [[[{(ConsoleColor)level}]{level}[/]]] {message.Replace("[", "[[").Replace("]", "]]")}\n"));
|
||||
$"[[[gray]{moduleName}[/]]] [[[{(ConsoleColor)level}]{level}[/]]] {message.Replace("[", "[[").Replace("]", "]]")}\n"));
|
||||
|
||||
var logMessage = $"[{DateTime.Now:HH:mm:ss}] [{ModuleName}] [{level}] {message}";
|
||||
var logMessage = $"[{DateTime.Now:HH:mm:ss}] [{moduleName}] [{level}] {message}";
|
||||
PluginEventCommon.InvokeOnConsoleLog(logMessage);
|
||||
|
||||
if (level == LoggerLevel.DEBUG)
|
||||
{
|
||||
WriteToDebugFile(logMessage);
|
||||
return;
|
||||
}
|
||||
|
||||
WriteToDebugFile(logMessage);
|
||||
WriteToFile(logMessage);
|
||||
}
|
||||
}
|
||||
@@ -74,15 +82,33 @@ public class Logger(string moduleName)
|
||||
|
||||
public static void SetLogFile(FileInfo file)
|
||||
{
|
||||
LogFile = file;
|
||||
_logFile = file;
|
||||
}
|
||||
|
||||
public static void SetDebugLogFile(FileInfo file)
|
||||
{
|
||||
_debugLogFile = file;
|
||||
}
|
||||
|
||||
public static void WriteToFile(string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (LogFile == null) throw new Exception("LogFile is not set");
|
||||
using var sw = LogFile.AppendText();
|
||||
if (_logFile == null) throw new Exception("LogFile is not set");
|
||||
using var sw = _logFile.AppendText();
|
||||
sw.WriteLine(message);
|
||||
}
|
||||
catch
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
public static void WriteToDebugFile(string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (_debugLogFile == null) throw new Exception("DebugLogFile is not set");
|
||||
using var sw = _debugLogFile.AppendText();
|
||||
sw.WriteLine(message);
|
||||
}
|
||||
catch
|
||||
@@ -103,8 +129,4 @@ public enum LoggerLevel
|
||||
ERROR = ConsoleColor.Red,
|
||||
FATAL = ConsoleColor.DarkRed,
|
||||
DEBUG = ConsoleColor.Blue
|
||||
}
|
||||
|
||||
public class LoggerLevelHelper
|
||||
{
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using System.Globalization;
|
||||
using System.IO.Compression;
|
||||
using EggLink.DanhengServer.Command.Command;
|
||||
using EggLink.DanhengServer.Configuration;
|
||||
using EggLink.DanhengServer.Data;
|
||||
@@ -50,18 +51,60 @@ public class EntryPoint
|
||||
Environment.Exit(0);
|
||||
};
|
||||
var time = DateTime.Now;
|
||||
|
||||
// pack the old log
|
||||
var logDirectory = new DirectoryInfo(GetConfig().Path.LogPath);
|
||||
if (logDirectory.Exists)
|
||||
{
|
||||
List<string> packed = [];
|
||||
foreach (var oldFile in logDirectory.GetFiles().ToArray())
|
||||
{
|
||||
if (!oldFile.Name.EndsWith(".log")) continue;
|
||||
if (oldFile.Name.EndsWith("-debug.log")) continue;
|
||||
if (packed.Contains(oldFile.Name)) continue;
|
||||
|
||||
var fileName = oldFile.Name.Replace(".log", "");
|
||||
var debugFileName = fileName + "-debug";
|
||||
var oldDebugFile = logDirectory.GetFiles(debugFileName + ".log").FirstOrDefault();
|
||||
|
||||
if (oldFile.Exists)
|
||||
{
|
||||
var zipFileName = fileName + ".zip";
|
||||
var zipFile = new FileInfo(GetConfig().Path.LogPath + $"/{zipFileName}");
|
||||
if (zipFile.Exists) zipFile.Delete();
|
||||
using (var zip = ZipFile.Open(zipFile.FullName, ZipArchiveMode.Create))
|
||||
{
|
||||
zip.CreateEntryFromFile(oldFile.FullName, oldFile.Name);
|
||||
if (oldDebugFile is { Exists: true })
|
||||
zip.CreateEntryFromFile(oldDebugFile.FullName, oldDebugFile.Name);
|
||||
}
|
||||
|
||||
oldFile.Delete();
|
||||
oldDebugFile?.Delete();
|
||||
packed.Add(oldFile.Name);
|
||||
packed.Add(oldDebugFile?.Name ?? "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize the logfile
|
||||
var counter = 0;
|
||||
FileInfo file;
|
||||
FileInfo zi;
|
||||
while (true)
|
||||
{
|
||||
file = new FileInfo(GetConfig().Path.LogPath + $"/{DateTime.Now:yyyy-MM-dd}-{++counter}.log");
|
||||
zi = new FileInfo(GetConfig().Path.LogPath + $"/{DateTime.Now:yyyy-MM-dd}-{counter}.zip");
|
||||
if (file is not { Exists: false, Directory: not null }) continue;
|
||||
if (zi is not { Exists: false, Directory: not null }) continue;
|
||||
file.Directory.Create();
|
||||
break;
|
||||
}
|
||||
|
||||
var debugFile = new FileInfo(GetConfig().Path.LogPath + $"/{DateTime.Now:yyyy-MM-dd}-{counter}-debug.log");
|
||||
|
||||
Logger.SetLogFile(file);
|
||||
Logger.SetDebugLogFile(debugFile);
|
||||
// Starting the server
|
||||
Logger.Info(I18NManager.Translate("Server.ServerInfo.StartingServer"));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user