feat: add logging feature with console, Godot and file outputs
This commit is contained in:
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user