GeWuYou fdaac135f9 feat(logging): 添加日志记录系统的基础实现
- 定义了ILog接口,提供日志记录和级别检查功能
- 实现了GodotLogger类,将日志输出到Godot控制台
- 创建了ILoggerFactory接口用于创建日志记录器实例
- 添加了Log静态类提供全局日志记录功能
- 定义了LogLevel枚举标识不同严重程度的日志级别
- 实现了NullLogger用于禁用日志记录功能
2025-12-23 12:52:07 +08:00

50 lines
1.4 KiB
C#

using GFramework.Core.logging;
using Godot;
namespace GFramework.Godot.logging;
/// <summary>
/// Godot平台的日志记录器实现
/// </summary>
public sealed class GodotLogger : ILog
{
/// <summary>
/// 记录日志消息到Godot控制台
/// </summary>
/// <param name="level">日志级别</param>
/// <param name="message">日志消息</param>
/// <param name="exception">关联的异常对象</param>
/// <param name="context">日志上下文</param>
public void Log(LogLevel level, string message, Exception? exception, object? context)
{
var prefix = $"[{level}]";
if (exception != null)
message += $"\n{exception}";
switch (level)
{
case LogLevel.Error:
case LogLevel.Fatal:
GD.PrintErr(prefix, message);
break;
case LogLevel.Warning:
GD.PushWarning($"{prefix} {message}");
break;
case LogLevel.Trace:
case LogLevel.Debug:
case LogLevel.Info:
default:
GD.Print(prefix, message);
break;
}
}
/// <summary>
/// 检查指定日志级别是否启用
/// </summary>
/// <param name="level">日志级别</param>
/// <returns>始终返回 true</returns>
public bool IsEnabled(LogLevel level) => true;
}