From c28a1b537f717c1af7b83bde85d1cd550559b90a Mon Sep 17 00:00:00 2001 From: ljacqu Date: Fri, 5 Feb 2016 23:09:07 +0100 Subject: [PATCH] #495 Create ConsoleLogger method dedicated to logging exceptions --- src/main/java/fr/xephi/authme/AuthMe.java | 13 ++-- .../java/fr/xephi/authme/ConsoleLogger.java | 30 +++++---- .../java/fr/xephi/authme/MetricsStarter.java | 2 +- .../executable/authme/ReloadCommand.java | 2 +- .../authme/converter/RoyalAuthConverter.java | 2 +- .../authme/converter/vAuthFileReader.java | 15 ++--- .../fr/xephi/authme/datasource/MySQL.java | 64 ++++++++++--------- .../fr/xephi/authme/datasource/SQLite.java | 2 +- .../xephi/authme/modules/ModuleManager.java | 2 +- .../process/register/AsyncRegister.java | 2 +- .../authme/settings/CustomConfiguration.java | 2 +- .../fr/xephi/authme/settings/Settings.java | 7 +- .../authme/settings/custom/NewSetting.java | 2 +- .../java/fr/xephi/authme/util/GeoLiteAPI.java | 4 +- 14 files changed, 78 insertions(+), 71 deletions(-) diff --git a/src/main/java/fr/xephi/authme/AuthMe.java b/src/main/java/fr/xephi/authme/AuthMe.java index 82bf4ee9a..23301b42c 100644 --- a/src/main/java/fr/xephi/authme/AuthMe.java +++ b/src/main/java/fr/xephi/authme/AuthMe.java @@ -212,7 +212,7 @@ public class AuthMe extends JavaPlugin { setPluginInfos(); // Load settings and custom configurations, if it fails, stop the server due to security reasons. - if (loadSettings()) { + if (!loadSettings()) { server.shutdown(); setEnabled(false); return; @@ -226,7 +226,8 @@ public class AuthMe extends JavaPlugin { try { setupDatabase(); } catch (Exception e) { - ConsoleLogger.writeStackTrace(e.getMessage() + "\nFatal error occurred during database connection! Authme initialization ABORTED!" , e); + ConsoleLogger.logException("Fatal error occurred during database connection! " + + "Authme initialization aborted!", e); stopOrUnload(); return; } @@ -448,11 +449,11 @@ public class AuthMe extends JavaPlugin { try { settings = new Settings(this); Settings.reload(); - } catch (Exception e) { - ConsoleLogger.writeStackTrace("Can't load the configuration file... Something went wrong. " - + "To avoid security issues the server will shut down!", e); - server.shutdown(); return true; + } catch (Exception e) { + ConsoleLogger.logException("Can't load the configuration file... Something went wrong. " + + "To avoid security issues the server will shut down!", e); + server.shutdown(); } return false; } diff --git a/src/main/java/fr/xephi/authme/ConsoleLogger.java b/src/main/java/fr/xephi/authme/ConsoleLogger.java index 6256bc386..c3a4bdc2f 100644 --- a/src/main/java/fr/xephi/authme/ConsoleLogger.java +++ b/src/main/java/fr/xephi/authme/ConsoleLogger.java @@ -2,6 +2,7 @@ package fr.xephi.authme; import com.google.common.base.Throwables; import fr.xephi.authme.settings.Settings; +import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.Wrapper; import java.io.IOException; @@ -33,9 +34,8 @@ public final class ConsoleLogger { public static void info(String message) { wrapper.getLogger().info(message); if (!Settings.useLogging) { - return; + writeLog(message); } - writeLog("" + message); } /** @@ -45,10 +45,9 @@ public final class ConsoleLogger { */ public static void showError(String message) { wrapper.getLogger().warning(message); - if (!Settings.useLogging) { - return; + if (Settings.useLogging) { + writeLog("ERROR: " + message); } - writeLog("ERROR: " + message); } /** @@ -72,13 +71,22 @@ public final class ConsoleLogger { /** * Write a StackTrace into the log. * - * @param ex Exception + * @param th The Throwable whose stack trace should be logged */ - public static void writeStackTrace(String message , Throwable ex) { - if (!Settings.useLogging) { - return; + public static void writeStackTrace(Throwable th) { + if (Settings.useLogging) { + writeLog(Throwables.getStackTraceAsString(th)); } - writeLog(message); - writeLog(Throwables.getStackTraceAsString(ex)); + } + + /** + * Logs a Throwable with the provided message and saves the stack trace to the log file. + * + * @param message The message to accompany the exception + * @param th The Throwable to log + */ + public static void logException(String message, Throwable th) { + showError(message + " " + StringUtils.formatException(th)); + writeStackTrace(th); } } diff --git a/src/main/java/fr/xephi/authme/MetricsStarter.java b/src/main/java/fr/xephi/authme/MetricsStarter.java index 83f3bb511..8fb0e498b 100644 --- a/src/main/java/fr/xephi/authme/MetricsStarter.java +++ b/src/main/java/fr/xephi/authme/MetricsStarter.java @@ -39,7 +39,7 @@ public class MetricsStarter { metrics.start(); } catch (final IOException e) { // Failed to submit the metrics data - ConsoleLogger.writeStackTrace("Can't start Metrics! The plugin will work anyway...", e); + ConsoleLogger.logException("Can't start Metrics! The plugin will work anyway...", e); } } } diff --git a/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java b/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java index 28a08cb73..875802a64 100644 --- a/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java +++ b/src/main/java/fr/xephi/authme/command/executable/authme/ReloadCommand.java @@ -26,7 +26,7 @@ public class ReloadCommand implements ExecutableCommand { plugin.setupDatabase(); } catch (Exception e) { sender.sendMessage("Error occurred during reload of AuthMe: aborting"); - ConsoleLogger.writeStackTrace("Fatal error occurred! AuthMe instance ABORTED!", e); + ConsoleLogger.logException("Aborting! Encountered exception during reload of AuthMe:", e); plugin.stopOrUnload(); } diff --git a/src/main/java/fr/xephi/authme/converter/RoyalAuthConverter.java b/src/main/java/fr/xephi/authme/converter/RoyalAuthConverter.java index 3159937a3..42bd681df 100644 --- a/src/main/java/fr/xephi/authme/converter/RoyalAuthConverter.java +++ b/src/main/java/fr/xephi/authme/converter/RoyalAuthConverter.java @@ -33,7 +33,7 @@ public class RoyalAuthConverter implements Converter { PlayerAuth auth = new PlayerAuth(name, ra.getHash(), "127.0.0.1", ra.getLastLogin(), "your@email.com", o.getName()); data.saveAuth(auth); } catch (Exception e) { - ConsoleLogger.writeStackTrace("Error while trying to import " + o.getName() + " RoyalAuth datas", e); + ConsoleLogger.logException("Error while trying to import " + o.getName() + " RoyalAuth data", e); } } } diff --git a/src/main/java/fr/xephi/authme/converter/vAuthFileReader.java b/src/main/java/fr/xephi/authme/converter/vAuthFileReader.java index f0aa197e9..bb25b04bd 100644 --- a/src/main/java/fr/xephi/authme/converter/vAuthFileReader.java +++ b/src/main/java/fr/xephi/authme/converter/vAuthFileReader.java @@ -8,6 +8,7 @@ import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import java.io.File; +import java.io.IOException; import java.util.Scanner; import java.util.UUID; @@ -27,7 +28,7 @@ class vAuthFileReader { } public void convert() { - final File file = new File(plugin.getDataFolder().getParent() + "" + File.separator + "vAuth" + File.separator + "passwords.yml"); + final File file = new File(plugin.getDataFolder().getParent() + File.separator + "vAuth" + File.separator + "passwords.yml"); Scanner scanner; try { scanner = new Scanner(file); @@ -52,8 +53,8 @@ class vAuthFileReader { database.saveAuth(auth); } scanner.close(); - } catch (Exception e) { - ConsoleLogger.writeStackTrace("Error while trying to import some vAuth datas", e); + } catch (IOException e) { + ConsoleLogger.logException("Error while trying to import some vAuth data", e); } } @@ -63,12 +64,10 @@ class vAuthFileReader { } private String getName(UUID uuid) { - try { - for (OfflinePlayer op : Bukkit.getOfflinePlayers()) { - if (op.getUniqueId().compareTo(uuid) == 0) - return op.getName(); + for (OfflinePlayer op : Bukkit.getOfflinePlayers()) { + if (op.getUniqueId().compareTo(uuid) == 0) { + return op.getName(); } - } catch (Exception ignored) { } return null; } diff --git a/src/main/java/fr/xephi/authme/datasource/MySQL.java b/src/main/java/fr/xephi/authme/datasource/MySQL.java index de1837696..03da26d3d 100644 --- a/src/main/java/fr/xephi/authme/datasource/MySQL.java +++ b/src/main/java/fr/xephi/authme/datasource/MySQL.java @@ -257,7 +257,7 @@ public class MySQL implements DataSource { ResultSet rs = pst.executeQuery(); return rs.next(); } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return false; } @@ -275,7 +275,7 @@ public class MySQL implements DataSource { !columnSalt.isEmpty() ? rs.getString(columnSalt) : null); } } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return null; } @@ -320,7 +320,7 @@ public class MySQL implements DataSource { } } } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); return null; } return pAuth; @@ -522,7 +522,7 @@ public class MySQL implements DataSource { } return true; } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return false; } @@ -585,7 +585,7 @@ public class MySQL implements DataSource { } return true; } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return false; } @@ -618,7 +618,7 @@ public class MySQL implements DataSource { pst.setLong(1, until); result = pst.executeUpdate(); } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return result; } @@ -641,7 +641,7 @@ public class MySQL implements DataSource { st.executeUpdate(); st.close(); } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return list; } @@ -673,7 +673,7 @@ public class MySQL implements DataSource { pst.executeUpdate(); return true; } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return false; } @@ -694,7 +694,7 @@ public class MySQL implements DataSource { pst.close(); return true; } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return false; } @@ -713,7 +713,7 @@ public class MySQL implements DataSource { rs.close(); pst.close(); } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return countIp; } @@ -729,7 +729,7 @@ public class MySQL implements DataSource { pst.close(); return true; } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return false; } @@ -739,8 +739,8 @@ public class MySQL implements DataSource { try { reloadArguments(); } catch (Exception ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); - ConsoleLogger.showError("Can't reconnect to MySQL database... Please check your MySQL configuration!"); + ConsoleLogger.logException("Can't reconnect to MySQL database... " + + "Please check your MySQL configuration! Encountered", ex); AuthMe.getInstance().stopOrUnload(); } } @@ -766,7 +766,7 @@ public class MySQL implements DataSource { rs.close(); pst.close(); } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return result; } @@ -785,7 +785,7 @@ public class MySQL implements DataSource { rs.close(); pst.close(); } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return result; } @@ -804,7 +804,7 @@ public class MySQL implements DataSource { rs.close(); pst.close(); } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return countEmail; } @@ -819,7 +819,7 @@ public class MySQL implements DataSource { } pst.close(); } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } } @@ -838,7 +838,7 @@ public class MySQL implements DataSource { ResultSet rs = pst.executeQuery(); isLogged = rs.next() && (rs.getInt(columnLogged) == 1); } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } return isLogged; } @@ -853,7 +853,7 @@ public class MySQL implements DataSource { pst.executeUpdate(); pst.close(); } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } } @@ -867,7 +867,7 @@ public class MySQL implements DataSource { pst.executeUpdate(); pst.close(); } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + logSqlException(ex); } } @@ -880,8 +880,8 @@ public class MySQL implements DataSource { pst.setInt(2, 1); pst.executeUpdate(); pst.close(); - } catch (Exception ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + } catch (SQLException ex) { + logSqlException(ex); } } @@ -896,8 +896,8 @@ public class MySQL implements DataSource { } rs.close(); st.close(); - } catch (Exception ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + } catch (SQLException ex) { + logSqlException(ex); } return result; } @@ -910,8 +910,8 @@ public class MySQL implements DataSource { pst.setString(1, newOne); pst.setString(2, oldOne); pst.executeUpdate(); - } catch (Exception ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + } catch (SQLException ex) { + logSqlException(ex); } } @@ -955,8 +955,8 @@ public class MySQL implements DataSource { pst.close(); rs.close(); st.close(); - } catch (Exception ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + } catch (SQLException ex) { + logSqlException(ex); } return auths; } @@ -998,10 +998,14 @@ public class MySQL implements DataSource { } auths.add(pAuth); } - } catch (Exception ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + } catch (SQLException ex) { + logSqlException(ex); } return auths; } + private static void logSqlException(SQLException e) { + ConsoleLogger.logException("Error during SQL operation:", e); + } + } diff --git a/src/main/java/fr/xephi/authme/datasource/SQLite.java b/src/main/java/fr/xephi/authme/datasource/SQLite.java index bab5cf988..ca9c2565c 100644 --- a/src/main/java/fr/xephi/authme/datasource/SQLite.java +++ b/src/main/java/fr/xephi/authme/datasource/SQLite.java @@ -170,7 +170,7 @@ public class SQLite implements DataSource { !columnSalt.isEmpty() ? rs.getString(columnSalt) : null); } } catch (SQLException ex) { - ConsoleLogger.writeStackTrace(ex.getMessage(), ex); + ConsoleLogger.logException("Error getting password:", ex); } finally { close(rs); close(pst); diff --git a/src/main/java/fr/xephi/authme/modules/ModuleManager.java b/src/main/java/fr/xephi/authme/modules/ModuleManager.java index b2e8c9716..6228091c2 100644 --- a/src/main/java/fr/xephi/authme/modules/ModuleManager.java +++ b/src/main/java/fr/xephi/authme/modules/ModuleManager.java @@ -135,7 +135,7 @@ public class ModuleManager { } } catch (Exception ex) { - ConsoleLogger.writeStackTrace("Cannot load " + pathToJar.getName() + " jar file !", ex); + ConsoleLogger.logException("Cannot load " + pathToJar.getName() + " jar file!", ex); } finally { try { if (jarFile != null) { diff --git a/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java b/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java index 886b2b949..6ef2bf24a 100644 --- a/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java +++ b/src/main/java/fr/xephi/authme/process/register/AsyncRegister.java @@ -81,7 +81,7 @@ public class AsyncRegister { passwordRegister(); } } catch (Exception e) { - ConsoleLogger.writeStackTrace(e.getMessage(), e); + ConsoleLogger.logException("Error during async register process", e); m.send(player, MessageKey.ERROR); } } diff --git a/src/main/java/fr/xephi/authme/settings/CustomConfiguration.java b/src/main/java/fr/xephi/authme/settings/CustomConfiguration.java index 0aab1c801..ee1645257 100644 --- a/src/main/java/fr/xephi/authme/settings/CustomConfiguration.java +++ b/src/main/java/fr/xephi/authme/settings/CustomConfiguration.java @@ -77,7 +77,7 @@ public abstract class CustomConfiguration extends YamlConfiguration { return true; } } catch (Exception e) { - ConsoleLogger.writeStackTrace("Failed to load config from JAR", e); + ConsoleLogger.logException("Failed to load config from JAR", e); } } return false; diff --git a/src/main/java/fr/xephi/authme/settings/Settings.java b/src/main/java/fr/xephi/authme/settings/Settings.java index 88d5a5fa8..e38fd9f8d 100644 --- a/src/main/java/fr/xephi/authme/settings/Settings.java +++ b/src/main/java/fr/xephi/authme/settings/Settings.java @@ -313,7 +313,7 @@ public final class Settings { try { return Files.toString(EMAIL_FILE, Charsets.UTF_8); } catch (IOException e) { - ConsoleLogger.writeStackTrace("Error loading email text: " + StringUtils.formatException(e), e); + ConsoleLogger.logException("Error loading email text:", e); return ""; } } @@ -748,11 +748,6 @@ public final class Settings { } } - /** - * @param path - * - * @return - */ private static boolean contains(String path) { return configFile.contains(path); } diff --git a/src/main/java/fr/xephi/authme/settings/custom/NewSetting.java b/src/main/java/fr/xephi/authme/settings/custom/NewSetting.java index c6db84541..7a9094f61 100644 --- a/src/main/java/fr/xephi/authme/settings/custom/NewSetting.java +++ b/src/main/java/fr/xephi/authme/settings/custom/NewSetting.java @@ -129,7 +129,7 @@ public class NewSetting { writer.flush(); writer.close(); } catch (IOException e) { - ConsoleLogger.writeStackTrace("Could not save config file - " + StringUtils.formatException(e), e); + ConsoleLogger.logException("Could not save config file:", e); } } diff --git a/src/main/java/fr/xephi/authme/util/GeoLiteAPI.java b/src/main/java/fr/xephi/authme/util/GeoLiteAPI.java index 4e396557d..7f519a779 100644 --- a/src/main/java/fr/xephi/authme/util/GeoLiteAPI.java +++ b/src/main/java/fr/xephi/authme/util/GeoLiteAPI.java @@ -36,7 +36,7 @@ public class GeoLiteAPI { plugin.getLogger().info(LICENSE); return true; } catch (IOException e) { - ConsoleLogger.writeStackTrace("Could not find/download GeoLiteAPI", e); + ConsoleLogger.logException("Could not find/download GeoLiteAPI", e); return false; } } @@ -63,7 +63,7 @@ public class GeoLiteAPI { output.close(); input.close(); } catch (IOException e) { - ConsoleLogger.writeStackTrace("Could not download GeoLiteAPI", e); + ConsoleLogger.logException("Could not download GeoLiteAPI", e); } } });