Updated logger

This commit is contained in:
2024-05-21 21:00:31 +03:00
parent 8dbbfbfaef
commit 5731165d29
10 changed files with 50 additions and 35 deletions

View File

@@ -24,7 +24,7 @@ public class Exit: ICommandAction
{ {
if (args is null || args.Length == 0) if (args is null || args.Length == 0)
{ {
Application.CurrentApplication.Logger.Log("Exiting...", typeof(ICommandAction), LogType.WARNING); Application.CurrentApplication.Logger.Log("Exiting...", this, LogType.WARNING);
await Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile(); await Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile();
Environment.Exit(0); Environment.Exit(0);
} }
@@ -40,7 +40,7 @@ public class Exit: ICommandAction
case "-f": case "-f":
case "force": case "force":
Application.CurrentApplication.Logger.Log("Exiting (FORCE)...", typeof(ICommandAction), LogType.WARNING); Application.CurrentApplication.Logger.Log("Exiting (FORCE)...", this, LogType.WARNING);
Environment.Exit(0); Environment.Exit(0);
break; break;

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@@ -148,15 +149,13 @@ internal static class PluginMethods
{ {
if (data.IsSuccess) if (data.IsSuccess)
{ {
Application.CurrentApplication.Logger.Log("Successfully loaded command : " + data.PluginName, typeof(ICommandAction), Application.CurrentApplication.Logger.Log("Successfully loaded command : " + data.PluginName, LogType.INFO, "\t\t > {Message}");
LogType.INFO
);
} }
else else
{ {
Application.CurrentApplication.Logger.Log("Failed to load command : " + data.PluginName + " because " + data.ErrorMessage, Application.CurrentApplication.Logger.Log("Failed to load command : " + data.PluginName + " because " + data.ErrorMessage,
typeof(ICommandAction), LogType.ERROR typeof(PluginMethods), LogType.ERROR
); );
} }
@@ -166,14 +165,12 @@ internal static class PluginMethods
{ {
if (data.IsSuccess) if (data.IsSuccess)
{ {
Application.CurrentApplication.Logger.Log("Successfully loaded event : " + data.PluginName, typeof(ICommandAction), Application.CurrentApplication.Logger.Log("Successfully loaded event : " + data.PluginName, LogType.INFO, "\t\t > {Message}");
LogType.INFO
);
} }
else else
{ {
Application.CurrentApplication.Logger.Log("Failed to load event : " + data.PluginName + " because " + data.ErrorMessage, Application.CurrentApplication.Logger.Log("Failed to load event : " + data.PluginName + " because " + data.ErrorMessage,
typeof(ICommandAction), LogType.ERROR typeof(PluginMethods), LogType.ERROR
); );
} }
@@ -184,14 +181,12 @@ internal static class PluginMethods
{ {
if (data.IsSuccess) if (data.IsSuccess)
{ {
Application.CurrentApplication.Logger.Log("Successfully loaded slash command : " + data.PluginName, typeof(ICommandAction), Application.CurrentApplication.Logger.Log("Successfully loaded slash command : " + data.PluginName, LogType.INFO, "\t\t > {Message}");
LogType.INFO
);
} }
else else
{ {
Application.CurrentApplication.Logger.Log("Failed to load slash command : " + data.PluginName + " because " + data.ErrorMessage, Application.CurrentApplication.Logger.Log("Failed to load slash command : " + data.PluginName + " because " + data.ErrorMessage,
typeof(ICommandAction), LogType.ERROR typeof(PluginMethods), LogType.ERROR
); );
} }

View File

@@ -61,13 +61,13 @@ public class Plugin: ICommandAction
case "load": case "load":
if (pluginsLoaded) if (pluginsLoaded)
{ {
Application.CurrentApplication.Logger.Log("Plugins already loaded", typeof(ICommandAction), LogType.WARNING); Application.CurrentApplication.Logger.Log("Plugins already loaded", this, LogType.WARNING);
break; break;
} }
if (Application.CurrentApplication.DiscordBotClient is null) if (Application.CurrentApplication.DiscordBotClient is null)
{ {
Application.CurrentApplication.Logger.Log("DiscordBot is null", typeof(ICommandAction), LogType.WARNING); Application.CurrentApplication.Logger.Log("DiscordBot is null", this, LogType.WARNING);
break; break;
} }

View File

@@ -95,7 +95,6 @@ public class Boot
private void CommonTasks() private void CommonTasks()
{ {
if (Client == null) return; if (Client == null) return;
Client.LoggedOut += Client_LoggedOut;
Client.Log += Log; Client.Log += Log;
Client.LoggedIn += LoggedIn; Client.LoggedIn += LoggedIn;
Client.Ready += Ready; Client.Ready += Ready;
@@ -107,18 +106,12 @@ public class Boot
if (arg.Message.Contains("401")) if (arg.Message.Contains("401"))
{ {
Application.CurrentApplication.ApplicationEnvironmentVariables.Remove("token"); Application.CurrentApplication.ApplicationEnvironmentVariables.Remove("token");
Application.CurrentApplication.Logger.Log("The token is invalid. Please restart the bot and follow the instructions", typeof(Boot), LogType.CRITICAL); Application.CurrentApplication.Logger.Log("The token is invalid. Please restart the bot and follow the instructions", this, LogType.CRITICAL);
await Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile(); await Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile();
Environment.Exit(0); Environment.Exit(0);
} }
} }
private async Task Client_LoggedOut()
{
Application.CurrentApplication.Logger.Log("Successfully Logged Out", typeof(Boot));
await Log(new LogMessage(LogSeverity.Info, "Boot", "Successfully logged out from discord !"));
}
private Task Ready() private Task Ready()
{ {
IsReady = true; IsReady = true;
@@ -127,7 +120,7 @@ public class Boot
private Task LoggedIn() private Task LoggedIn()
{ {
Application.CurrentApplication.Logger.Log("Successfully Logged In", typeof(Boot)); Application.CurrentApplication.Logger.Log("Successfully Logged In", this);
return Task.CompletedTask; return Task.CompletedTask;
} }
@@ -137,12 +130,12 @@ public class Boot
{ {
case LogSeverity.Error: case LogSeverity.Error:
case LogSeverity.Critical: case LogSeverity.Critical:
Application.CurrentApplication.Logger.Log(message.Message, typeof(Boot), LogType.ERROR); Application.CurrentApplication.Logger.Log(message.Message, this, LogType.ERROR);
break; break;
case LogSeverity.Info: case LogSeverity.Info:
case LogSeverity.Debug: case LogSeverity.Debug:
Application.CurrentApplication.Logger.Log(message.Message, typeof(Boot), LogType.INFO); Application.CurrentApplication.Logger.Log(message.Message, this, LogType.INFO);
break; break;

View File

@@ -142,7 +142,7 @@ internal class CommandHandler
Application.CurrentApplication.Logger.Log( Application.CurrentApplication.Logger.Log(
$"User ({context.User.Username}) from Guild \"{context.Guild.Name}\" executed command \"{cmd.cleanContent}\"", $"User ({context.User.Username}) from Guild \"{context.Guild.Name}\" executed command \"{cmd.cleanContent}\"",
typeof(CommandHandler), this,
LogType.INFO LogType.INFO
); );

View File

@@ -32,7 +32,7 @@ public class PluginLoader
public async Task LoadPlugins() public async Task LoadPlugins()
{ {
Application.CurrentApplication.Logger.Log("Loading plugins...", typeof(PluginLoader)); Application.CurrentApplication.Logger.Log("Loading plugins...", this);
var loader = new Loader(Application.CurrentApplication.ApplicationEnvironmentVariables["PluginFolder"], "dll"); var loader = new Loader(Application.CurrentApplication.ApplicationEnvironmentVariables["PluginFolder"], "dll");
@@ -46,7 +46,7 @@ public class PluginLoader
private void FileLoadedException(FileLoaderResult result) private void FileLoadedException(FileLoaderResult result)
{ {
Application.CurrentApplication.Logger.Log(result.ErrorMessage, typeof(PluginLoader), LogType.ERROR); Application.CurrentApplication.Logger.Log(result.ErrorMessage, this, LogType.ERROR);
} }
private async void OnPluginLoaded(PluginLoadResultData result) private async void OnPluginLoaded(PluginLoadResultData result)
@@ -74,11 +74,11 @@ public class PluginLoader
OnSlashCommandLoaded?.Invoke(result); OnSlashCommandLoaded?.Invoke(result);
} }
else else
Application.CurrentApplication.Logger.Log($"Failed to start slash command {result.PluginName}", typeof(PluginLoader), LogType.ERROR); Application.CurrentApplication.Logger.Log($"Failed to start slash command {result.PluginName}", this, LogType.ERROR);
break; break;
case PluginType.UNKNOWN: case PluginType.UNKNOWN:
default: default:
Application.CurrentApplication.Logger.Log("Unknown plugin type", typeof(PluginLoader), LogType.ERROR); Application.CurrentApplication.Logger.Log("Unknown plugin type", this, LogType.ERROR);
break; break;
} }
} }

View File

@@ -101,7 +101,7 @@ public class PluginManager
{ {
if (await pluginUpdater.HasUpdate(plugin.PluginName)) if (await pluginUpdater.HasUpdate(plugin.PluginName))
{ {
Application.CurrentApplication.Logger.Log("Updating plugin: " + plugin.PluginName, typeof(PluginManager), LogType.INFO); Application.CurrentApplication.Logger.Log("Updating plugin: " + plugin.PluginName, this, LogType.INFO);
await pluginUpdater.UpdatePlugin(plugin.PluginName); await pluginUpdater.UpdatePlugin(plugin.PluginName);
} }
} }

View File

@@ -11,6 +11,14 @@ namespace DiscordBotCore.Others.Logger
public string SenderName { get; set; } public string SenderName { get; set; }
public LogType LogMessageType { get; set; } public LogType LogMessageType { get; set; }
public LogMessage(string message, LogType logMessageType)
{
Message = message;
LogMessageType = logMessageType;
ThrowTime = DateTime.Now;
SenderName = string.Empty;
}
public LogMessage(string message, object sender) public LogMessage(string message, object sender)
{ {
Message = message; Message = message;

View File

@@ -37,6 +37,25 @@ public sealed class Logger : ILogger
return messageAsString; return messageAsString;
} }
private string GenerateLogMessage(ILogMessage message, string customFormat)
{
string messageAsString = customFormat;
foreach (var prop in LogMessageProperties)
{
Type messageType = typeof(ILogMessage);
messageAsString = messageAsString.Replace("{" + prop + "}", messageType?.GetProperty(prop)?.GetValue(message)?.ToString());
}
return messageAsString;
}
public void Log(ILogMessage message, string format)
{
OnRawLog?.Invoke(this, message);
string messageAsString = GenerateLogMessage(message, format);
OnFormattedLog?.Invoke(this, new ILogger.FormattedMessage() { Message = messageAsString, Type = message.LogMessageType });
}
public void Log(ILogMessage message) public void Log(ILogMessage message)
{ {
OnRawLog?.Invoke(this, message); OnRawLog?.Invoke(this, message);
@@ -44,6 +63,8 @@ public sealed class Logger : ILogger
OnFormattedLog?.Invoke(this, new ILogger.FormattedMessage() { Message = messageAsString, Type = message.LogMessageType }) ; OnFormattedLog?.Invoke(this, new ILogger.FormattedMessage() { Message = messageAsString, Type = message.LogMessageType }) ;
} }
public void Log(string message, LogType logType, string format) => Log(new LogMessage(message, logType), format);
public void Log(string message, LogType logType) => Log(new LogMessage(message, logType));
public void Log(string message, object Sender) => Log(new LogMessage(message, Sender)); public void Log(string message, object Sender) => Log(new LogMessage(message, Sender));
public void Log(string message, object Sender, LogType type) => Log(new LogMessage(message, Sender, type)); public void Log(string message, object Sender, LogType type) => Log(new LogMessage(message, Sender, type));
public void LogException(Exception exception, object Sender) => Log(LogMessage.CreateFromException(exception, Sender)); public void LogException(Exception exception, object Sender) => Log(LogMessage.CreateFromException(exception, Sender));

View File

@@ -1,7 +1,5 @@
using Avalonia.Controls; using Avalonia.Controls;
using DiscordBotCore;
namespace DiscordBotUI.Views; namespace DiscordBotUI.Views;
public partial class SettingsPage : Window public partial class SettingsPage : Window