Add errorLog method to log throwables/exceptions

* Add methods errorLog(Throwable) and errorLog(Throwable, Boolean) to
GeneralMethods.java
* Add documentation to both methods describing how to use them
This commit is contained in:
Jack Lin 2015-05-27 14:23:40 +12:00
parent cb13a38873
commit 7891e84adb

View file

@ -1796,4 +1796,53 @@ public class GeneralMethods {
return false; return false;
} }
/**
* Logs a throwable into an ERROR.log file in the ProjectKorra root folder.
* <p>
* Calls {@link #logError(Throwable, boolean)} with silent set to true
* </p>
*
* @param e The throwable/exception to log
*/
public static void logError(Throwable e) {
logError(e, true);
}
/**
* Logs a throwable into an ERROR.log file in the ProjectKorra root folder.
* <p>
* If silent is {@code true} this method will display
* will display an error in console indicating admins to check ERROR.log
* <br>
* Otherwise it will log the error silently
* </p>
*
* @param e The throwable/exception to log
* @param silent Wether or not the console should be notified of an error
*/
public static void logError(Throwable e, boolean silent) {
File errorFile = new File(plugin.getDataFolder(), "ERROR.log");
if (!errorFile.exists()) {
try {
errorFile.createNewFile();
} catch (IOException ex) {
ProjectKorra.log.severe("Unable to generate error log!");
ex.printStackTrace();
}
} else {
try(PrintWriter out = new PrintWriter(new FileWriter(errorFile, true))) {
if (!silent) {
ProjectKorra.log.severe("#################################################");
ProjectKorra.log.severe("#################====[ERROR]====#################");
ProjectKorra.log.severe(" An error has been caught");
ProjectKorra.log.severe("Please check the ERROR.log file for more details.");
ProjectKorra.log.severe("#################====[ERROR]====#################");
ProjectKorra.log.severe("#################################################");
}
e.printStackTrace(out);
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
} }