feat: add logging feature with console, Godot and file outputs

This commit is contained in:
2026-08-23 20:20:41 +02:00
parent 5ee80df4c1
commit 988f1102f8
16 changed files with 205 additions and 16 deletions
+48
View File
@@ -0,0 +1,48 @@
using System;
using Game.Features.Logging.Contracts;
using Godot;
namespace Game.Features.Logging.Godot;
public sealed class FileLogger : ILogger, IDisposable
{
private const string LogPath = "user://logs/latest.log";
private readonly FileAccess _file;
public FileLogger()
{
DirAccess.MakeDirRecursiveAbsolute(LogPath.GetBaseDir());
_file = FileAccess.Open(LogPath, FileAccess.ModeFlags.Write);
if (_file is null) return;
_file.StoreLine($"=== Log started at {DateTime.Now:yyyy-MM-dd HH:mm:ss} ===");
_file.Flush();
}
private void Write(LogLevel level, string category, string message)
{
if (_file is null) return;
_file.StoreLine($"{DateTime.Now:HH:mm:ss} [{level}] [{category}] {message}");
_file.Flush();
}
public void Debug(string category, string message)
=> Write(LogLevel.Debug, category, message);
public void Info(string category, string message)
=> Write(LogLevel.Info, category, message);
public void Warning(string category, string message)
=> Write(LogLevel.Warning, category, message);
public void Error(string category, string message)
=> Write(LogLevel.Error, category, message);
public void Dispose()
{
_file?.Dispose();
}
}