From 5caaa731083b25532b4c39fead9b15b524f15146 Mon Sep 17 00:00:00 2001 From: Alex Date: Mon, 13 Jan 2025 20:16:05 +0000 Subject: [PATCH] added color logging --- Core/Models/Utils/ILogger.cs | 3 ++- Core/Utils/Logging/SimpleTextLogger.cs | 34 ------------------------ Core/Utils/Watermark.cs | 2 +- Server/Logger/WebApplicationLogger.cs | 36 +++++++++++++++++++++++--- 4 files changed, 36 insertions(+), 39 deletions(-) delete mode 100644 Core/Utils/Logging/SimpleTextLogger.cs diff --git a/Core/Models/Utils/ILogger.cs b/Core/Models/Utils/ILogger.cs index a455ad5a..e83b7b69 100644 --- a/Core/Models/Utils/ILogger.cs +++ b/Core/Models/Utils/ILogger.cs @@ -7,10 +7,11 @@ public interface ILogger // TODO: Removing these 4 methods for now, revisit in the future // void WriteToLogFile(string data); // void Log(string data, LogTextColor? color, string? backgroundColor = null); - // void LogWithColor(string data, LogTextColor textColor, LogBackgroundColor? backgroundColor = null); + void LogWithColor(string data, LogTextColor? textColor = null, LogBackgroundColor? backgroundColor = null); void Success(string data); void Error(string data); void Warning(string data); void Info(string data); void Debug(string data); + void Critical(string data); } diff --git a/Core/Utils/Logging/SimpleTextLogger.cs b/Core/Utils/Logging/SimpleTextLogger.cs deleted file mode 100644 index 815a2b93..00000000 --- a/Core/Utils/Logging/SimpleTextLogger.cs +++ /dev/null @@ -1,34 +0,0 @@ -using Core.Models.Logging; -using Core.Annotations; -using ILogger = Core.Models.Utils.ILogger; - -namespace Core.Utils.Logging; - -// [Injectable(InjectionType.Singleton)] -public class SimpleTextLogger : ILogger -{ - public void Success(string data) - { - Console.WriteLine(data); - } - - public void Error(string data) - { - Console.WriteLine(data); - } - - public void Warning(string data) - { - Console.WriteLine(data); - } - - public void Info(string data) - { - Console.WriteLine(data); - } - - public void Debug(string data) - { - Console.WriteLine(data); - } -} diff --git a/Core/Utils/Watermark.cs b/Core/Utils/Watermark.cs index 475555b2..2622a1b1 100644 --- a/Core/Utils/Watermark.cs +++ b/Core/Utils/Watermark.cs @@ -182,7 +182,7 @@ public class Watermark { // Log watermark to screen foreach (var text in result) { - _logger.Warning(text); + _logger.LogWithColor(text, LogTextColor.Yellow); } } } diff --git a/Server/Logger/WebApplicationLogger.cs b/Server/Logger/WebApplicationLogger.cs index e9de500d..374b70ab 100644 --- a/Server/Logger/WebApplicationLogger.cs +++ b/Server/Logger/WebApplicationLogger.cs @@ -1,4 +1,5 @@ using Core.Annotations; +using Core.Models.Logging; using ILogger = Core.Models.Utils.ILogger; namespace Server.Logger; @@ -12,19 +13,43 @@ public class WebApplicationLogger : ILogger _logger = provider.CreateLogger("SptLogger"); } + public void LogWithColor(string data, LogTextColor? textColor = null, LogBackgroundColor? backgroundColor = null) + { + if (textColor != null || backgroundColor != null) + { + _logger.LogInformation(GetColorizedText(data, textColor, backgroundColor)); + } + else + _logger.LogInformation(data); + } + + private string GetColorizedText(string data, LogTextColor? textColor = null, LogBackgroundColor? backgroundColor = null) + { + var colorString = string.Empty; + if (textColor != null) + colorString += ((int)textColor.Value).ToString(); + + if (backgroundColor != null) + colorString += string.IsNullOrEmpty(colorString) + ? ((int)backgroundColor.Value).ToString() + : $";{((int)backgroundColor.Value).ToString()}"; + + return $"\x1b[{colorString}m{data}\x1b[0m"; + } + public void Success(string data) { - _logger.LogInformation(data); + _logger.LogInformation(GetColorizedText(data, LogTextColor.Green)); } public void Error(string data) { - _logger.LogError(data); + _logger.LogError(GetColorizedText(data, LogTextColor.Red)); } public void Warning(string data) { - _logger.LogWarning(data); + _logger.LogWarning(GetColorizedText(data, LogTextColor.Yellow)); } public void Info(string data) @@ -36,4 +61,9 @@ public class WebApplicationLogger : ILogger { _logger.LogDebug(data); } + + public void Critical(string data) + { + _logger.LogCritical(GetColorizedText(data, LogTextColor.Black, LogBackgroundColor.Red)); + } }