GeWuYou fb14d7122c docs(style): 更新文档中的命名空间导入格式
- 将所有小写的命名空间导入更正为首字母大写格式
- 统一 GFramework 框架的命名空间引用规范
- 修复 core、ecs、godot 等模块的命名空间导入错误
- 标准化文档示例代码中的 using 语句格式
- 确保所有文档中的命名空间引用保持一致性
- 更新 global using 语句以匹配正确的命名空间格式
2026-03-10 07:18:49 +08:00

91 lines
2.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.IO;
using GFramework.Core.Abstractions.Logging;
namespace GFramework.Core.Logging.Appenders;
/// <summary>
/// 控制台日志输出器
/// </summary>
public sealed class ConsoleAppender : ILogAppender, IDisposable
{
private readonly ILogFilter? _filter;
private readonly ILogFormatter _formatter;
private readonly bool _useColors;
private readonly TextWriter _writer;
/// <summary>
/// 创建控制台日志输出器
/// </summary>
/// <param name="formatter">日志格式化器</param>
/// <param name="writer">文本写入器(默认为 Console.Out</param>
/// <param name="useColors">是否使用颜色(默认为 true</param>
/// <param name="filter">日志过滤器(可选)</param>
public ConsoleAppender(
ILogFormatter formatter,
TextWriter? writer = null,
bool useColors = true,
ILogFilter? filter = null)
{
_formatter = formatter ?? throw new ArgumentNullException(nameof(formatter));
_writer = writer ?? Console.Out;
_useColors = useColors && _writer == Console.Out;
_filter = filter;
}
/// <summary>
/// 释放资源
/// </summary>
public void Dispose()
{
_writer.Flush();
}
/// <summary>
/// 追加日志条目到控制台
/// </summary>
/// <param name="entry">日志条目</param>
public void Append(LogEntry entry)
{
if (_filter != null && !_filter.ShouldLog(entry))
return;
var message = _formatter.Format(entry);
if (_useColors)
{
WriteColored(entry.Level, message);
}
else
{
_writer.WriteLine(message);
}
}
/// <summary>
/// 刷新控制台输出
/// </summary>
public void Flush()
{
_writer.Flush();
}
private void WriteColored(LogLevel level, string message)
{
var colorCode = GetAnsiColorCode(level);
_writer.WriteLine($"\x1b[{colorCode}m{message}\x1b[0m");
}
private static string GetAnsiColorCode(LogLevel level)
{
return level switch
{
LogLevel.Trace => "90", // 暗灰色
LogLevel.Debug => "36", // 青色
LogLevel.Info => "37", // 白色
LogLevel.Warning => "33", // 黄色
LogLevel.Error => "31", // 红色
LogLevel.Fatal => "35", // 洋红色
_ => "37"
};
}
}