mirror of
https://github.com/TotalFreedomMC/TF-ProjectKorra.git
synced 2025-02-11 11:40:40 +00:00
Add PKLogHandler, PKFilter, PKErrorFilter, PKFormatter
This commit is contained in:
parent
354796e997
commit
3b2b0e403a
5 changed files with 118 additions and 3 deletions
|
@ -1,6 +1,8 @@
|
|||
package com.projectkorra.ProjectKorra;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.logging.Handler;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
@ -13,6 +15,8 @@ import com.projectkorra.ProjectKorra.Ability.MultiAbility.MultiAbilityModuleMana
|
|||
import com.projectkorra.ProjectKorra.Objects.Preset;
|
||||
import com.projectkorra.ProjectKorra.Utilities.CraftingRecipes;
|
||||
import com.projectkorra.ProjectKorra.Utilities.Updater;
|
||||
import com.projectkorra.ProjectKorra.Utilities.logging.PKFilter;
|
||||
import com.projectkorra.ProjectKorra.Utilities.logging.PKLogHandler;
|
||||
import com.projectkorra.ProjectKorra.airbending.AirbendingManager;
|
||||
import com.projectkorra.ProjectKorra.chiblocking.ChiComboManager;
|
||||
import com.projectkorra.ProjectKorra.chiblocking.ChiblockingManager;
|
||||
|
@ -26,12 +30,19 @@ public class ProjectKorra extends JavaPlugin {
|
|||
public static long time_step = 1;
|
||||
public static ProjectKorra plugin;
|
||||
public static Logger log;
|
||||
|
||||
public static PKLogHandler handler;
|
||||
public Updater updater;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
ProjectKorra.log = this.getLogger();
|
||||
try {
|
||||
handler = new PKLogHandler(getDataFolder() + File.separator + "ERROR.log");
|
||||
log.getParent().setFilter(new PKFilter());
|
||||
log.getParent().addHandler(handler);
|
||||
} catch (SecurityException | IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
plugin = this;
|
||||
new ConfigManager(this);
|
||||
new GeneralMethods(this);
|
||||
|
@ -74,7 +85,6 @@ public class ProjectKorra extends JavaPlugin {
|
|||
metrics.start();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
GeneralMethods.logError(e);
|
||||
}
|
||||
|
||||
GeneralMethods.deserializeFile();
|
||||
|
@ -89,9 +99,12 @@ public class ProjectKorra extends JavaPlugin {
|
|||
GeneralMethods.stopBending();
|
||||
if (DBConnection.isOpen == false) return;
|
||||
DBConnection.sql.close();
|
||||
for (Handler handler : log.getHandlers()) {
|
||||
handler.close();
|
||||
}
|
||||
}
|
||||
|
||||
public void stopPlugin() {
|
||||
getServer().getPluginManager().disablePlugin(plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
package com.projectkorra.ProjectKorra.Utilities.logging;
|
||||
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
public class PKErrorFilter extends PKFilter {
|
||||
|
||||
@Override
|
||||
public boolean isLoggable(LogRecord record) {
|
||||
if (consoleError.contains(record.getMessage().replace("[ProjectKorra] ", ""))) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.projectkorra.ProjectKorra.Utilities.logging;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.logging.Filter;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
import com.projectkorra.ProjectKorra.ProjectKorra;
|
||||
|
||||
public class PKFilter implements Filter {
|
||||
|
||||
List<String> consoleError = Arrays.asList(
|
||||
"###################################################",
|
||||
"##################====[ERROR]====##################",
|
||||
" An error has been caught",
|
||||
" Please check the ERROR.log file for stack trace.",
|
||||
" Create a bug report with the log contents at.",
|
||||
"http://projectkorra.com/forum/forums/bug-reports.6/",
|
||||
"##################====[ERROR]====##################",
|
||||
"###################################################"
|
||||
);
|
||||
|
||||
@Override
|
||||
public boolean isLoggable(LogRecord record) {
|
||||
if (record.getLevel() == Level.SEVERE && record.getMessage().contains("ProjectKorra") && record.getThrown() != null) {
|
||||
for (String line : consoleError) {
|
||||
ProjectKorra.log.severe(line);
|
||||
}
|
||||
ProjectKorra.handler.publish(record);
|
||||
ProjectKorra.handler.flush();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.projectkorra.ProjectKorra.Utilities.logging;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.logging.Formatter;
|
||||
import java.util.logging.LogRecord;
|
||||
|
||||
public class PKFormatter extends Formatter {
|
||||
|
||||
private final SimpleDateFormat date = new SimpleDateFormat("HH:mm:ss");
|
||||
|
||||
@Override
|
||||
public String format(LogRecord record) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
Throwable ex = record.getThrown();
|
||||
|
||||
builder.append(date.format(record.getMillis()));
|
||||
builder.append(" [");
|
||||
builder.append(record.getLevel().getLocalizedName().toUpperCase());
|
||||
builder.append("] ");
|
||||
builder.append(formatMessage(record));
|
||||
builder.append('\n');
|
||||
|
||||
if (ex != null) {
|
||||
StringWriter writer = new StringWriter();
|
||||
ex.printStackTrace(new PrintWriter(writer));
|
||||
builder.append(writer);
|
||||
}
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.projectkorra.ProjectKorra.Utilities.logging;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.logging.FileHandler;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class PKLogHandler extends FileHandler {
|
||||
|
||||
public PKLogHandler(String filename) throws IOException {
|
||||
super(filename);
|
||||
this.setLevel(Level.WARNING);
|
||||
this.setFilter(new PKErrorFilter());
|
||||
this.setFormatter(new PKFormatter());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue