The logger now supports colors

This commit is contained in:
2023-07-05 19:30:38 +03:00
parent ac7212ca00
commit a7a71bf49a
8 changed files with 63 additions and 21 deletions

View File

@@ -17,8 +17,9 @@ public class Exit : ICommandAction
{
if (args is null || args.Length == 0)
{
Config.Logger.Log("Exiting...", "Exit");
Config.Data.Save();
Config.Logger.Log("Exiting...", "Exit", isInternal:false);
await Config.Data.Save();
await Config.Logger.SaveToFile();
Environment.Exit(0);
}
else
@@ -32,7 +33,7 @@ public class Exit : ICommandAction
break;
case "force":
Config.Logger.Log("Exiting...", "Exit");
Config.Logger.Log("Exiting (FORCE)...", "Exit", LogLevel.WARNING, false);
Environment.Exit(0);
break;

View File

@@ -36,7 +36,7 @@ public static class Installer
Config.Data.Add("ServerID", serverId);
}
Config.Logger.Log("Config Saved", "Installer");
Config.Logger.Log("Config Saved", "Installer", isInternal:true);
Config.Data.Save();

View File

@@ -165,7 +165,24 @@ public class Program
URLs = new Json<string, string>("./Data/Resources/URLs.json");
Logger.LogEvent += (message, type) => { Console.WriteLine(message); };
Logger.LogEvent += (message, type, isInternal) =>
{
if (isInternal) return;
if (type == LogLevel.INFO)
Console.ForegroundColor = ConsoleColor.Green;
else if (type == LogLevel.WARNING)
Console.ForegroundColor = ConsoleColor.DarkYellow;
else if (type == LogLevel.ERROR)
Console.ForegroundColor = ConsoleColor.Red;
else if(type == LogLevel.CRITICAL)
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.WriteLine($"[{type.ToString()}] {message}");
Console.ResetColor();
};
Console.WriteLine("Loading resources ...");

View File

@@ -144,7 +144,7 @@ public class Config
return ((IEnumerable)_dictionary).GetEnumerator();
}
public async void Save()
public async Task Save()
{
if (!string.IsNullOrEmpty(_file))
await Functions.SaveToJsonFile(_file, _dictionary);

View File

@@ -33,14 +33,14 @@ public class InternalActionManager
return;
}
Config.Logger.Log($"Action {name} loaded successfully", typeName);
Config.Logger.Log($"Action {name} loaded successfully", LogLevel.INFO, true);
}
public async Task<string> Execute(string actionName, params string[]? args)
{
if (!Actions.ContainsKey(actionName))
{
Config.Logger.Log($"Action {actionName} not found", "InternalActionManager", LogLevel.WARNING);
Config.Logger.Log($"Action {actionName} not found", "InternalActionManager", LogLevel.WARNING, true);
return "Action not found";
}

View File

@@ -16,7 +16,6 @@ public enum OperatingSystem
/// </summary>
public enum LogLevel
{
NONE,
INFO,
WARNING,
ERROR,

View File

@@ -1,11 +1,12 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace PluginManager.Others.Logger;
public class DBLogger
{
public delegate void LogHandler(string message, LogLevel logType);
public delegate void LogHandler(string message, LogLevel logType, bool isInternal = false);
private readonly string _errFolder;
@@ -23,6 +24,21 @@ public class DBLogger
public IReadOnlyList<LogMessage> Errors => ErrorHistory;
public event LogHandler LogEvent;
public void Log(string message, LogLevel type = LogLevel.INFO)
{
Log(new LogMessage(message, type));
}
public void Log(string message, LogLevel type= LogLevel.INFO, bool isInternal = false)
{
Log(new LogMessage(message, type,"unknown", isInternal));
}
public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO, bool isInternal = false)
{
Log(new LogMessage(message, type,sender,isInternal));
}
public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO)
{
@@ -45,13 +61,14 @@ public class DBLogger
ErrorHistory.Add(message);
}
public void Log(string message, object sender, LogLevel type = LogLevel.NONE)
public void Log(string message, object sender, LogLevel type = LogLevel.INFO)
{
Log(message, sender.GetType().Name, type);
}
public async void SaveToFile()
public async Task SaveToFile(bool ErrorsOnly = true)
{
if(!ErrorsOnly)
await Functions.SaveToJsonFile(_logFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json",
LogHistory);
await Functions.SaveToJsonFile(_errFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json",

View File

@@ -6,20 +6,28 @@ public class LogMessage
{
public LogMessage(string message, LogLevel type)
{
Message = message;
Type = type;
Time = DateTime.Now.ToString("HH:mm:ss");
Message = message;
Type = type;
Time = DateTime.Now.ToString("HH:mm:ss");
isInternal = false;
}
public LogMessage(string message, LogLevel type, string sender) : this(message, type)
public LogMessage(string message, LogLevel type, string sender, bool isInternal) : this(message, type)
{
Sender = sender;
Sender = sender;
this.isInternal = isInternal;
}
public string Message { get; set; }
public LogLevel Type { get; set; }
public string Time { get; set; }
public string Sender { get; set; }
public LogMessage(string message, LogLevel type, string sender) : this (message, type, sender, false)
{
}
public string Message { get; set; }
public LogLevel Type { get; set; }
public string Time { get; set; }
public string Sender { get; set; }
public bool isInternal { get; set; }
public override string ToString()
{