给logs命令使用新的logger样式

This commit is contained in:
2026-03-20 22:09:13 +08:00
parent f5b8faa2e8
commit ca30113efb
3 changed files with 43 additions and 30 deletions

View File

@@ -1 +1 @@
2026-03-18 21:37:24 2026-03-20 22:07:06

View File

@@ -1 +1 @@
ef4be8c f5b8faa

View File

@@ -26,7 +26,6 @@ namespace CMLeonOS.Commands
try try
{ {
var logs = Log.Logs; var logs = Log.Logs;
if (logs == null || logs.Count == 0) if (logs == null || logs.Count == 0)
{ {
Console.WriteLine("No logs available."); Console.WriteLine("No logs available.");
@@ -35,29 +34,13 @@ namespace CMLeonOS.Commands
Console.WriteLine($"Total logs: {logs.Count}"); Console.WriteLine($"Total logs: {logs.Count}");
Console.WriteLine(); Console.WriteLine();
int countToShow = Math.Min(logs.Count, 20); int countToShow = Math.Min(logs.Count, 20);
int startIndex = logs.Count - countToShow; int startIndex = logs.Count - countToShow;
for (int i = startIndex; i < logs.Count; i++) for (int i = startIndex; i < logs.Count; i++)
{ {
LogEntry entry = logs[i]; LogEntry entry = logs[i];
string level = entry.Level switch WriteToConsole(entry);
{
LogLevel.Debug => "DEBUG",
LogLevel.Info => "INFO ",
LogLevel.Warning => "WARN ",
LogLevel.Error => "ERROR",
LogLevel.Success => "SUCCESS",
_ => "UNKNOWN"
};
string time = entry.Date.ToString("HH:mm:ss");
string message = entry.Message.Length > 50 ? entry.Message.Substring(0, 50) + "..." : entry.Message;
Console.ForegroundColor = GetLevelColor(entry.Level);
Console.WriteLine($"[{time}] [{level}] [{entry.Source}] {message}");
Console.ResetColor();
} }
if (logs.Count > 20) if (logs.Count > 20)
@@ -72,17 +55,47 @@ namespace CMLeonOS.Commands
} }
} }
private static ConsoleColor GetLevelColor(LogLevel level) private static void WriteToConsole(LogEntry entry)
{ {
return level switch ConsoleColor originalBackgroundColor = Console.BackgroundColor;
{ ConsoleColor originalForegroundColor = Console.ForegroundColor;
LogLevel.Debug => ConsoleColor.Gray,
LogLevel.Info => ConsoleColor.Cyan, // 日志等级
LogLevel.Warning => ConsoleColor.Yellow, Console.Write("[ ");
LogLevel.Error => ConsoleColor.Red, switch (entry.Level)
LogLevel.Success => ConsoleColor.Green, {
_ => ConsoleColor.White case LogLevel.Debug:
}; Console.ForegroundColor = ConsoleColor.Gray;
break;
case LogLevel.Info:
Console.ForegroundColor = ConsoleColor.DarkCyan;
break;
case LogLevel.Warning:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case LogLevel.Error:
Console.ForegroundColor = ConsoleColor.Red;
break;
case LogLevel.Success:
Console.ForegroundColor = ConsoleColor.Green;
break;
}
Console.Write(entry.ToStringLevelStr());
Console.ForegroundColor = originalForegroundColor;
Console.Write(" ] ");
// 日志来源
Console.Write($"{entry.Source}: ");
// 日志内容
Console.Write(entry.ToStringMessage());
// 换行
Console.WriteLine();
Console.ForegroundColor = originalForegroundColor;
Console.BackgroundColor = originalBackgroundColor;
} }
} }
} }