#495 Create ConsoleLogger method dedicated to logging exceptions

This commit is contained in:
ljacqu 2016-02-05 23:09:07 +01:00
parent 558cbf5848
commit c28a1b537f
14 changed files with 78 additions and 71 deletions

View File

@ -212,7 +212,7 @@ public class AuthMe extends JavaPlugin {
setPluginInfos(); setPluginInfos();
// Load settings and custom configurations, if it fails, stop the server due to security reasons. // Load settings and custom configurations, if it fails, stop the server due to security reasons.
if (loadSettings()) { if (!loadSettings()) {
server.shutdown(); server.shutdown();
setEnabled(false); setEnabled(false);
return; return;
@ -226,7 +226,8 @@ public class AuthMe extends JavaPlugin {
try { try {
setupDatabase(); setupDatabase();
} catch (Exception e) { } 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(); stopOrUnload();
return; return;
} }
@ -448,11 +449,11 @@ public class AuthMe extends JavaPlugin {
try { try {
settings = new Settings(this); settings = new Settings(this);
Settings.reload(); 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; 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; return false;
} }

View File

@ -2,6 +2,7 @@ package fr.xephi.authme;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import fr.xephi.authme.util.StringUtils;
import fr.xephi.authme.util.Wrapper; import fr.xephi.authme.util.Wrapper;
import java.io.IOException; import java.io.IOException;
@ -33,9 +34,8 @@ public final class ConsoleLogger {
public static void info(String message) { public static void info(String message) {
wrapper.getLogger().info(message); wrapper.getLogger().info(message);
if (!Settings.useLogging) { if (!Settings.useLogging) {
return; writeLog(message);
} }
writeLog("" + message);
} }
/** /**
@ -45,10 +45,9 @@ public final class ConsoleLogger {
*/ */
public static void showError(String message) { public static void showError(String message) {
wrapper.getLogger().warning(message); wrapper.getLogger().warning(message);
if (!Settings.useLogging) { if (Settings.useLogging) {
return; writeLog("ERROR: " + message);
} }
writeLog("ERROR: " + message);
} }
/** /**
@ -72,13 +71,22 @@ public final class ConsoleLogger {
/** /**
* Write a StackTrace into the log. * 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) { public static void writeStackTrace(Throwable th) {
if (!Settings.useLogging) { if (Settings.useLogging) {
return; 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);
} }
} }

View File

@ -39,7 +39,7 @@ public class MetricsStarter {
metrics.start(); metrics.start();
} catch (final IOException e) { } catch (final IOException e) {
// Failed to submit the metrics data // 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);
} }
} }
} }

View File

@ -26,7 +26,7 @@ public class ReloadCommand implements ExecutableCommand {
plugin.setupDatabase(); plugin.setupDatabase();
} catch (Exception e) { } catch (Exception e) {
sender.sendMessage("Error occurred during reload of AuthMe: aborting"); 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(); plugin.stopOrUnload();
} }

View File

@ -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()); PlayerAuth auth = new PlayerAuth(name, ra.getHash(), "127.0.0.1", ra.getLastLogin(), "your@email.com", o.getName());
data.saveAuth(auth); data.saveAuth(auth);
} catch (Exception e) { } 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);
} }
} }
} }

View File

@ -8,6 +8,7 @@ import org.bukkit.Bukkit;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.Scanner; import java.util.Scanner;
import java.util.UUID; import java.util.UUID;
@ -27,7 +28,7 @@ class vAuthFileReader {
} }
public void convert() { 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; Scanner scanner;
try { try {
scanner = new Scanner(file); scanner = new Scanner(file);
@ -52,8 +53,8 @@ class vAuthFileReader {
database.saveAuth(auth); database.saveAuth(auth);
} }
scanner.close(); scanner.close();
} catch (Exception e) { } catch (IOException e) {
ConsoleLogger.writeStackTrace("Error while trying to import some vAuth datas", e); ConsoleLogger.logException("Error while trying to import some vAuth data", e);
} }
} }
@ -63,12 +64,10 @@ class vAuthFileReader {
} }
private String getName(UUID uuid) { private String getName(UUID uuid) {
try { for (OfflinePlayer op : Bukkit.getOfflinePlayers()) {
for (OfflinePlayer op : Bukkit.getOfflinePlayers()) { if (op.getUniqueId().compareTo(uuid) == 0) {
if (op.getUniqueId().compareTo(uuid) == 0) return op.getName();
return op.getName();
} }
} catch (Exception ignored) {
} }
return null; return null;
} }

View File

@ -257,7 +257,7 @@ public class MySQL implements DataSource {
ResultSet rs = pst.executeQuery(); ResultSet rs = pst.executeQuery();
return rs.next(); return rs.next();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return false; return false;
} }
@ -275,7 +275,7 @@ public class MySQL implements DataSource {
!columnSalt.isEmpty() ? rs.getString(columnSalt) : null); !columnSalt.isEmpty() ? rs.getString(columnSalt) : null);
} }
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return null; return null;
} }
@ -320,7 +320,7 @@ public class MySQL implements DataSource {
} }
} }
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
return null; return null;
} }
return pAuth; return pAuth;
@ -522,7 +522,7 @@ public class MySQL implements DataSource {
} }
return true; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return false; return false;
} }
@ -585,7 +585,7 @@ public class MySQL implements DataSource {
} }
return true; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return false; return false;
} }
@ -618,7 +618,7 @@ public class MySQL implements DataSource {
pst.setLong(1, until); pst.setLong(1, until);
result = pst.executeUpdate(); result = pst.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return result; return result;
} }
@ -641,7 +641,7 @@ public class MySQL implements DataSource {
st.executeUpdate(); st.executeUpdate();
st.close(); st.close();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return list; return list;
} }
@ -673,7 +673,7 @@ public class MySQL implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
return true; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return false; return false;
} }
@ -694,7 +694,7 @@ public class MySQL implements DataSource {
pst.close(); pst.close();
return true; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return false; return false;
} }
@ -713,7 +713,7 @@ public class MySQL implements DataSource {
rs.close(); rs.close();
pst.close(); pst.close();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return countIp; return countIp;
} }
@ -729,7 +729,7 @@ public class MySQL implements DataSource {
pst.close(); pst.close();
return true; return true;
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return false; return false;
} }
@ -739,8 +739,8 @@ public class MySQL implements DataSource {
try { try {
reloadArguments(); reloadArguments();
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); ConsoleLogger.logException("Can't reconnect to MySQL database... " +
ConsoleLogger.showError("Can't reconnect to MySQL database... Please check your MySQL configuration!"); "Please check your MySQL configuration! Encountered", ex);
AuthMe.getInstance().stopOrUnload(); AuthMe.getInstance().stopOrUnload();
} }
} }
@ -766,7 +766,7 @@ public class MySQL implements DataSource {
rs.close(); rs.close();
pst.close(); pst.close();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return result; return result;
} }
@ -785,7 +785,7 @@ public class MySQL implements DataSource {
rs.close(); rs.close();
pst.close(); pst.close();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return result; return result;
} }
@ -804,7 +804,7 @@ public class MySQL implements DataSource {
rs.close(); rs.close();
pst.close(); pst.close();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return countEmail; return countEmail;
} }
@ -819,7 +819,7 @@ public class MySQL implements DataSource {
} }
pst.close(); pst.close();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
} }
@ -838,7 +838,7 @@ public class MySQL implements DataSource {
ResultSet rs = pst.executeQuery(); ResultSet rs = pst.executeQuery();
isLogged = rs.next() && (rs.getInt(columnLogged) == 1); isLogged = rs.next() && (rs.getInt(columnLogged) == 1);
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return isLogged; return isLogged;
} }
@ -853,7 +853,7 @@ public class MySQL implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
pst.close(); pst.close();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
} }
@ -867,7 +867,7 @@ public class MySQL implements DataSource {
pst.executeUpdate(); pst.executeUpdate();
pst.close(); pst.close();
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
} }
@ -880,8 +880,8 @@ public class MySQL implements DataSource {
pst.setInt(2, 1); pst.setInt(2, 1);
pst.executeUpdate(); pst.executeUpdate();
pst.close(); pst.close();
} catch (Exception ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
} }
@ -896,8 +896,8 @@ public class MySQL implements DataSource {
} }
rs.close(); rs.close();
st.close(); st.close();
} catch (Exception ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return result; return result;
} }
@ -910,8 +910,8 @@ public class MySQL implements DataSource {
pst.setString(1, newOne); pst.setString(1, newOne);
pst.setString(2, oldOne); pst.setString(2, oldOne);
pst.executeUpdate(); pst.executeUpdate();
} catch (Exception ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
} }
@ -955,8 +955,8 @@ public class MySQL implements DataSource {
pst.close(); pst.close();
rs.close(); rs.close();
st.close(); st.close();
} catch (Exception ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return auths; return auths;
} }
@ -998,10 +998,14 @@ public class MySQL implements DataSource {
} }
auths.add(pAuth); auths.add(pAuth);
} }
} catch (Exception ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); logSqlException(ex);
} }
return auths; return auths;
} }
private static void logSqlException(SQLException e) {
ConsoleLogger.logException("Error during SQL operation:", e);
}
} }

View File

@ -170,7 +170,7 @@ public class SQLite implements DataSource {
!columnSalt.isEmpty() ? rs.getString(columnSalt) : null); !columnSalt.isEmpty() ? rs.getString(columnSalt) : null);
} }
} catch (SQLException ex) { } catch (SQLException ex) {
ConsoleLogger.writeStackTrace(ex.getMessage(), ex); ConsoleLogger.logException("Error getting password:", ex);
} finally { } finally {
close(rs); close(rs);
close(pst); close(pst);

View File

@ -135,7 +135,7 @@ public class ModuleManager {
} }
} catch (Exception ex) { } catch (Exception ex) {
ConsoleLogger.writeStackTrace("Cannot load " + pathToJar.getName() + " jar file !", ex); ConsoleLogger.logException("Cannot load " + pathToJar.getName() + " jar file!", ex);
} finally { } finally {
try { try {
if (jarFile != null) { if (jarFile != null) {

View File

@ -81,7 +81,7 @@ public class AsyncRegister {
passwordRegister(); passwordRegister();
} }
} catch (Exception e) { } catch (Exception e) {
ConsoleLogger.writeStackTrace(e.getMessage(), e); ConsoleLogger.logException("Error during async register process", e);
m.send(player, MessageKey.ERROR); m.send(player, MessageKey.ERROR);
} }
} }

View File

@ -77,7 +77,7 @@ public abstract class CustomConfiguration extends YamlConfiguration {
return true; return true;
} }
} catch (Exception e) { } catch (Exception e) {
ConsoleLogger.writeStackTrace("Failed to load config from JAR", e); ConsoleLogger.logException("Failed to load config from JAR", e);
} }
} }
return false; return false;

View File

@ -313,7 +313,7 @@ public final class Settings {
try { try {
return Files.toString(EMAIL_FILE, Charsets.UTF_8); return Files.toString(EMAIL_FILE, Charsets.UTF_8);
} catch (IOException e) { } catch (IOException e) {
ConsoleLogger.writeStackTrace("Error loading email text: " + StringUtils.formatException(e), e); ConsoleLogger.logException("Error loading email text:", e);
return ""; return "";
} }
} }
@ -748,11 +748,6 @@ public final class Settings {
} }
} }
/**
* @param path
*
* @return
*/
private static boolean contains(String path) { private static boolean contains(String path) {
return configFile.contains(path); return configFile.contains(path);
} }

View File

@ -129,7 +129,7 @@ public class NewSetting {
writer.flush(); writer.flush();
writer.close(); writer.close();
} catch (IOException e) { } catch (IOException e) {
ConsoleLogger.writeStackTrace("Could not save config file - " + StringUtils.formatException(e), e); ConsoleLogger.logException("Could not save config file:", e);
} }
} }

View File

@ -36,7 +36,7 @@ public class GeoLiteAPI {
plugin.getLogger().info(LICENSE); plugin.getLogger().info(LICENSE);
return true; return true;
} catch (IOException e) { } catch (IOException e) {
ConsoleLogger.writeStackTrace("Could not find/download GeoLiteAPI", e); ConsoleLogger.logException("Could not find/download GeoLiteAPI", e);
return false; return false;
} }
} }
@ -63,7 +63,7 @@ public class GeoLiteAPI {
output.close(); output.close();
input.close(); input.close();
} catch (IOException e) { } catch (IOException e) {
ConsoleLogger.writeStackTrace("Could not download GeoLiteAPI", e); ConsoleLogger.logException("Could not download GeoLiteAPI", e);
} }
} }
}); });