From 30bba43e4523abf495c058651abc13ab8bde53b1 Mon Sep 17 00:00:00 2001 From: Lixfel Date: Sat, 18 Jan 2020 12:13:25 +0100 Subject: [PATCH] Saving exceptions to database --- .../src/de/steamwar/core/Core.java | 1 + .../src/de/steamwar/core/ErrorLogger.java | 42 +++++++++++++++++++ .../src/de/steamwar/sql/SWException.java | 25 +++++++++++ 3 files changed, 68 insertions(+) create mode 100644 SpigotCore_Main/src/de/steamwar/core/ErrorLogger.java create mode 100644 SpigotCore_Main/src/de/steamwar/sql/SWException.java diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index f84b991..0b0026d 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -36,6 +36,7 @@ public class Core extends JavaPlugin{ public void onEnable() { Bukkit.getPluginManager().registerEvents(new PlayerJoinedEvent(), this); Bukkit.getPluginManager().registerEvents(new ChattingEvent(), this); + ErrorLogger.init(); } @Override diff --git a/SpigotCore_Main/src/de/steamwar/core/ErrorLogger.java b/SpigotCore_Main/src/de/steamwar/core/ErrorLogger.java new file mode 100644 index 0000000..1b8bed6 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/core/ErrorLogger.java @@ -0,0 +1,42 @@ +package de.steamwar.core; + +import de.steamwar.sql.SWException; +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.core.Core; +import org.apache.logging.log4j.core.*; +import org.apache.logging.log4j.core.appender.AbstractAppender; +import org.apache.logging.log4j.core.config.plugins.Plugin; +import org.apache.logging.log4j.core.config.plugins.PluginAttribute; +import org.apache.logging.log4j.core.config.plugins.PluginFactory; + +@Plugin(name = "ErrorLogger", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE) +public class ErrorLogger extends AbstractAppender { + + public static void init(){ + final LoggerContext lc = (LoggerContext) LogManager.getContext(false); + ErrorLogger el = ErrorLogger.createAppender("SWErrorLogger"); + el.start(); + lc.getConfiguration().addAppender(el); + lc.getRootLogger().addAppender(lc.getConfiguration().getAppender(el.getName())); + lc.updateLoggers(); + } + + private ErrorLogger(String name) { + super(name, null, null); + } + + @PluginFactory + public static ErrorLogger createAppender( + @PluginAttribute("name") String name) { + return new ErrorLogger(name); + } + + @Override + public void append(LogEvent logEvent) { + if(logEvent.getLevel().isLessSpecificThan(Level.WARN)) + return; + + SWException.log(logEvent); + } +} diff --git a/SpigotCore_Main/src/de/steamwar/sql/SWException.java b/SpigotCore_Main/src/de/steamwar/sql/SWException.java new file mode 100644 index 0000000..718f458 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/sql/SWException.java @@ -0,0 +1,25 @@ +package de.steamwar.sql; + +import org.apache.logging.log4j.core.LogEvent; +import org.bukkit.Bukkit; + +public class SWException { + private SWException(){} + + public static void log(LogEvent logEvent){ + String server = SQL.disarmString(Bukkit.getWorlds().get(0).getName()); + StringBuilder stacktrace = new StringBuilder(logEvent.getSource().toString()); + + Throwable throwable = logEvent.getThrown(); + while(throwable != null){ + stacktrace.append("\nCaused by ").append(throwable.getMessage()); + + for(StackTraceElement ste : throwable.getStackTrace()) + stacktrace.append("\n").append(ste.toString()); + + throwable = throwable.getCause(); + } + + SQL.update("INSERT INTO Exception (server, message, stacktrace) VALUES ('" + server + "', '" + SQL.disarmString(logEvent.getMessage().getFormattedMessage()) + "', '" + SQL.disarmString(stacktrace.toString()) + "')"); + } +} -- 2.39.5