#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();
// 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();
return true;
} catch (Exception e) {
ConsoleLogger.writeStackTrace("Can't load the configuration file... Something went wrong. "
ConsoleLogger.logException("Can't load the configuration file... Something went wrong. "
+ "To avoid security issues the server will shut down!", e);
server.shutdown();
return true;
}
return false;
}

View File

@ -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,11 +45,10 @@ public final class ConsoleLogger {
*/
public static void showError(String message) {
wrapper.getLogger().warning(message);
if (!Settings.useLogging) {
return;
}
if (Settings.useLogging) {
writeLog("ERROR: " + message);
}
}
/**
* Write a message into the log file with a TimeStamp.
@ -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);
}
}

View File

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

View File

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

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

View File

@ -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)
if (op.getUniqueId().compareTo(uuid) == 0) {
return op.getName();
}
} catch (Exception ignored) {
}
return null;
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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