SteamWar/SpigotCore
Archiviert
13
0

Merge branch 'logger' of SteamWar/SpigotCore into master

Dieser Commit ist enthalten in:
Lixfel 2020-01-19 06:33:59 +01:00 committet von Gitea
Commit 7ca97476bd
3 geänderte Dateien mit 68 neuen und 0 gelöschten Zeilen

Datei anzeigen

@ -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

Datei anzeigen

@ -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);
}
}

Datei anzeigen

@ -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()) + "')");
}
}