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)
{
Application.CurrentApplication.Logger.Log("Exiting...", typeof(ICommandAction), LogType.WARNING);
Application.CurrentApplication.Logger.Log("Exiting...", this, LogType.WARNING);
await Application.CurrentApplication.ApplicationEnvironmentVariables.SaveToFile();
Environment.Exit(0);
}
@@ -40,7 +40,7 @@ public class Exit: ICommandAction
case "-f":
case "force":
Application.CurrentApplication.Logger.Log("Exiting (FORCE)...", typeof(ICommandAction), LogType.WARNING);
Application.CurrentApplication.Logger.Log("Exiting (FORCE)...", this, LogType.WARNING);
Environment.Exit(0);
break;

View File

@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
@@ -148,15 +149,13 @@ internal static class PluginMethods
{
if (data.IsSuccess)
{
Application.CurrentApplication.Logger.Log("Successfully loaded command : " + data.PluginName, typeof(ICommandAction),
LogType.INFO
);
Application.CurrentApplication.Logger.Log("Successfully loaded command : " + data.PluginName, LogType.INFO, "\t\t > {Message}");
}
else
{
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)
{
Application.CurrentApplication.Logger.Log("Successfully loaded event : " + data.PluginName, typeof(ICommandAction),
LogType.INFO
);
Application.CurrentApplication.Logger.Log("Successfully loaded event : " + data.PluginName, LogType.INFO, "\t\t > {Message}");
}
else
{
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)
{
Application.CurrentApplication.Logger.Log("Successfully loaded slash command : " + data.PluginName, typeof(ICommandAction),
LogType.INFO
);
Application.CurrentApplication.Logger.Log("Successfully loaded slash command : " + data.PluginName, LogType.INFO, "\t\t > {Message}");
}
else
{
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":
if (pluginsLoaded)
{
Application.CurrentApplication.Logger.Log("Plugins already loaded", typeof(ICommandAction), LogType.WARNING);
Application.CurrentApplication.Logger.Log("Plugins already loaded", this, LogType.WARNING);
break;
}
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;
}

View File

@@ -95,7 +95,6 @@ public class Boot
private void CommonTasks()
{
if (Client == null) return;
Client.LoggedOut += Client_LoggedOut;
Client.Log += Log;
Client.LoggedIn += LoggedIn;
Client.Ready += Ready;
@@ -107,18 +106,12 @@ public class Boot
if (arg.Message.Contains("401"))
{
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();
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()
{
IsReady = true;
@@ -127,7 +120,7 @@ public class Boot
private Task LoggedIn()
{
Application.CurrentApplication.Logger.Log("Successfully Logged In", typeof(Boot));
Application.CurrentApplication.Logger.Log("Successfully Logged In", this);
return Task.CompletedTask;
}
@@ -137,12 +130,12 @@ public class Boot
{
case LogSeverity.Error:
case LogSeverity.Critical:
Application.CurrentApplication.Logger.Log(message.Message, typeof(Boot), LogType.ERROR);
Application.CurrentApplication.Logger.Log(message.Message, this, LogType.ERROR);
break;
case LogSeverity.Info:
case LogSeverity.Debug:
Application.CurrentApplication.Logger.Log(message.Message, typeof(Boot), LogType.INFO);
Application.CurrentApplication.Logger.Log(message.Message, this, LogType.INFO);
break;

View File

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

View File

@@ -32,7 +32,7 @@ public class PluginLoader
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");
@@ -46,7 +46,7 @@ public class PluginLoader
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)
@@ -74,11 +74,11 @@ public class PluginLoader
OnSlashCommandLoaded?.Invoke(result);
}
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;
case PluginType.UNKNOWN:
default:
Application.CurrentApplication.Logger.Log("Unknown plugin type", typeof(PluginLoader), LogType.ERROR);
Application.CurrentApplication.Logger.Log("Unknown plugin type", this, LogType.ERROR);
break;
}
}

View File

@@ -101,7 +101,7 @@ public class PluginManager
{
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);
}
}

View File

@@ -11,6 +11,14 @@ namespace DiscordBotCore.Others.Logger
public string SenderName { 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)
{
Message = message;

View File

@@ -37,6 +37,25 @@ public sealed class Logger : ILogger
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)
{
OnRawLog?.Invoke(this, message);
@@ -44,6 +63,8 @@ public sealed class Logger : ILogger
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, LogType type) => Log(new LogMessage(message, Sender, type));
public void LogException(Exception exception, object Sender) => Log(LogMessage.CreateFromException(exception, Sender));

View File

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