From eb4aaaf4348132709354b59d0c2e684201f6953a Mon Sep 17 00:00:00 2001 From: Lixfel Date: Thu, 16 Sep 2021 13:10:00 +0200 Subject: [PATCH] New ErrorLogger implementation matching new SpigotCore implementation Signed-off-by: Lixfel --- src/de/steamwar/bungeecore/BungeeCore.java | 1 - src/de/steamwar/bungeecore/ErrorLogger.java | 93 ++++++++------------- 2 files changed, 35 insertions(+), 59 deletions(-) diff --git a/src/de/steamwar/bungeecore/BungeeCore.java b/src/de/steamwar/bungeecore/BungeeCore.java index 7dee2ca..6609324 100644 --- a/src/de/steamwar/bungeecore/BungeeCore.java +++ b/src/de/steamwar/bungeecore/BungeeCore.java @@ -158,7 +158,6 @@ public class BungeeCore extends Plugin { @Override public void onDisable(){ - ErrorLogger.stop(); Statement.close(); try { SteamwarDiscordBot.instance().getJda().shutdownNow(); diff --git a/src/de/steamwar/bungeecore/ErrorLogger.java b/src/de/steamwar/bungeecore/ErrorLogger.java index 5f4ab53..23c00cf 100644 --- a/src/de/steamwar/bungeecore/ErrorLogger.java +++ b/src/de/steamwar/bungeecore/ErrorLogger.java @@ -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 ignoreContains; + + static { + List 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); + } }