added color logging
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -182,7 +182,7 @@ public class Watermark {
|
||||
|
||||
// Log watermark to screen
|
||||
foreach (var text in result) {
|
||||
_logger.Warning(text);
|
||||
_logger.LogWithColor(text, LogTextColor.Yellow);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user