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
+4 -11
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;
+1 -1
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
);
+4 -4
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;
}
}
+1 -1
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);
}
}
@@ -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;
+21
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));