The logger now supports colors
This commit is contained in:
@@ -17,8 +17,9 @@ public class Exit : ICommandAction
|
|||||||
{
|
{
|
||||||
if (args is null || args.Length == 0)
|
if (args is null || args.Length == 0)
|
||||||
{
|
{
|
||||||
Config.Logger.Log("Exiting...", "Exit");
|
Config.Logger.Log("Exiting...", "Exit", isInternal:false);
|
||||||
Config.Data.Save();
|
await Config.Data.Save();
|
||||||
|
await Config.Logger.SaveToFile();
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
@@ -32,7 +33,7 @@ public class Exit : ICommandAction
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case "force":
|
case "force":
|
||||||
Config.Logger.Log("Exiting...", "Exit");
|
Config.Logger.Log("Exiting (FORCE)...", "Exit", LogLevel.WARNING, false);
|
||||||
Environment.Exit(0);
|
Environment.Exit(0);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public static class Installer
|
|||||||
Config.Data.Add("ServerID", serverId);
|
Config.Data.Add("ServerID", serverId);
|
||||||
}
|
}
|
||||||
|
|
||||||
Config.Logger.Log("Config Saved", "Installer");
|
Config.Logger.Log("Config Saved", "Installer", isInternal:true);
|
||||||
|
|
||||||
Config.Data.Save();
|
Config.Data.Save();
|
||||||
|
|
||||||
|
|||||||
@@ -165,7 +165,24 @@ public class Program
|
|||||||
|
|
||||||
URLs = new Json<string, string>("./Data/Resources/URLs.json");
|
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 ...");
|
Console.WriteLine("Loading resources ...");
|
||||||
|
|||||||
@@ -144,7 +144,7 @@ public class Config
|
|||||||
return ((IEnumerable)_dictionary).GetEnumerator();
|
return ((IEnumerable)_dictionary).GetEnumerator();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async void Save()
|
public async Task Save()
|
||||||
{
|
{
|
||||||
if (!string.IsNullOrEmpty(_file))
|
if (!string.IsNullOrEmpty(_file))
|
||||||
await Functions.SaveToJsonFile(_file, _dictionary);
|
await Functions.SaveToJsonFile(_file, _dictionary);
|
||||||
|
|||||||
@@ -33,14 +33,14 @@ public class InternalActionManager
|
|||||||
return;
|
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)
|
public async Task<string> Execute(string actionName, params string[]? args)
|
||||||
{
|
{
|
||||||
if (!Actions.ContainsKey(actionName))
|
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";
|
return "Action not found";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,6 @@ public enum OperatingSystem
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public enum LogLevel
|
public enum LogLevel
|
||||||
{
|
{
|
||||||
NONE,
|
|
||||||
INFO,
|
INFO,
|
||||||
WARNING,
|
WARNING,
|
||||||
ERROR,
|
ERROR,
|
||||||
|
|||||||
@@ -1,11 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace PluginManager.Others.Logger;
|
namespace PluginManager.Others.Logger;
|
||||||
|
|
||||||
public class DBLogger
|
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;
|
private readonly string _errFolder;
|
||||||
|
|
||||||
@@ -23,6 +24,21 @@ public class DBLogger
|
|||||||
public IReadOnlyList<LogMessage> Errors => ErrorHistory;
|
public IReadOnlyList<LogMessage> Errors => ErrorHistory;
|
||||||
|
|
||||||
public event LogHandler LogEvent;
|
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)
|
public void Log(string message, string sender = "unknown", LogLevel type = LogLevel.INFO)
|
||||||
{
|
{
|
||||||
@@ -45,13 +61,14 @@ public class DBLogger
|
|||||||
ErrorHistory.Add(message);
|
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);
|
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",
|
await Functions.SaveToJsonFile(_logFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json",
|
||||||
LogHistory);
|
LogHistory);
|
||||||
await Functions.SaveToJsonFile(_errFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json",
|
await Functions.SaveToJsonFile(_errFolder + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".json",
|
||||||
|
|||||||
@@ -6,20 +6,28 @@ public class LogMessage
|
|||||||
{
|
{
|
||||||
public LogMessage(string message, LogLevel type)
|
public LogMessage(string message, LogLevel type)
|
||||||
{
|
{
|
||||||
Message = message;
|
Message = message;
|
||||||
Type = type;
|
Type = type;
|
||||||
Time = DateTime.Now.ToString("HH:mm:ss");
|
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 LogMessage(string message, LogLevel type, string sender) : this (message, type, sender, false)
|
||||||
public LogLevel Type { get; set; }
|
{
|
||||||
public string Time { get; set; }
|
|
||||||
public string Sender { get; set; }
|
}
|
||||||
|
|
||||||
|
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()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user