SteamWar/BungeeCore
Archiviert
13
2

Merge pull request 'New ErrorLogger implementation matching new SpigotCore implementation' (#244) from newErrorLogger into master

Reviewed-on: #244
Reviewed-by: YoyoNow <jwsteam@nidido.de>
Dieser Commit ist enthalten in:
Lixfel 2021-09-17 07:45:30 +02:00
Commit 14d0fd8615
2 geänderte Dateien mit 35 neuen und 59 gelöschten Zeilen

Datei anzeigen

@ -158,7 +158,6 @@ public class BungeeCore extends Plugin {
@Override
public void onDisable(){
ErrorLogger.stop();
Statement.close();
try {
SteamwarDiscordBot.instance().getJda().shutdownNow();

Datei anzeigen

@ -20,83 +20,47 @@
package de.steamwar.bungeecore;
import de.steamwar.bungeecore.sql.SWException;
import net.md_5.bungee.api.ProxyServer;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.text.MessageFormat;
import java.util.logging.Filter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public class ErrorLogger extends Handler implements Filter {
private static ErrorLogger instance;
public class ErrorLogger extends Handler {
private int ddosRate = 0;
ErrorLogger(){
ProxyServer.getInstance().getLogger().addHandler(this);
ProxyServer.getInstance().getLogger().setFilter(this);
instance = this;
}
static void stop(){
ProxyServer.getInstance().getLogger().removeHandler(instance);
Logger.getLogger("").addHandler(this);
}
@Override
public boolean isLoggable(LogRecord record) {
if(record.getLevel() != Level.SEVERE)
return true;
public void publish(LogRecord logRecord) {
if(logRecord.getLevel().intValue() < Level.WARNING.intValue())
return;
StringBuilder stacktrace = new StringBuilder(record.getSourceClassName() + "\n" + record.getSourceMethodName());
Throwable thrown = record.getThrown();
while(thrown != null){
stacktrace.append("\nCaused by ").append(thrown.getMessage());
String message = MessageFormat.format(logRecord.getMessage(), logRecord.getParameters());
for(String reason : ignoreContains)
if(message.contains(reason))
return;
for(StackTraceElement ste : thrown.getStackTrace())
stacktrace.append("\n").append(ste.toString());
thrown = thrown.getCause();
}
String stacktraceString = stacktrace.toString();
if(stacktraceString.contains("Cannot request protocol")){
ddosRate++;
if(ddosRate % 1000 == 0){
ByteArrayOutputStream stacktraceOutput = new ByteArrayOutputStream();
if(logRecord.getThrown() != null)
logRecord.getThrown().printStackTrace(new PrintStream(stacktraceOutput));
String stacktrace = stacktraceOutput.toString();
if(stacktrace.contains("Cannot request protocol")) {
if(++ddosRate % 1000 == 0) {
SWException.log("Bungee", "DDOS", ddosRate + "");
}
return false;
}
return true;
}
@Override
public void publish(LogRecord record) {
if(record.getLevel() != Level.SEVERE && record.getLevel() != Level.WARNING)
return;
StringBuilder stacktrace = new StringBuilder(record.getSourceClassName() + "\n" + record.getSourceMethodName());
Throwable thrown = record.getThrown();
while(thrown != null){
stacktrace.append("\nCaused by ").append(thrown.getMessage());
for(StackTraceElement ste : thrown.getStackTrace())
stacktrace.append("\n").append(ste.toString());
thrown = thrown.getCause();
}
String stacktraceString = stacktrace.toString();
String message = MessageFormat.format(record.getMessage(), record.getParameters());
if(message.contains("ServerConnector")
|| message.contains("InitialHandler")
|| message.contains("UpstreamBridge")
|| message.contains("DownstreamBridge")
|| message.contains(" took ")
|| message.contains("No client connected for pending server!"))
return;
SWException.log("Bungee", message, stacktraceString);
SWException.log("Bungee", message, stacktrace);
}
@Override
@ -108,4 +72,17 @@ public class ErrorLogger extends Handler implements Filter {
public void close() {
//ignored
}
private static final List<String> ignoreContains;
static {
List<String> contains = new ArrayList<>();
contains.add("ServerConnector");
contains.add("InitialHandler");
contains.add("UpstreamBridge");
contains.add("DownstreamBridge");
contains.add(" took ");
contains.add("No client connected for pending server!");
ignoreContains = Collections.unmodifiableList(contains);
}
}