DebugLog + Messaging (Forgot these :))

This commit is contained in:
Rigby 2011-03-31 16:07:03 +01:00
parent 93aae71375
commit 85b219282e
2 changed files with 95 additions and 0 deletions

View File

@ -0,0 +1,70 @@
package com.onarandombox.utils;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
public class DebugLog {
private FileHandler fh;
private Logger log;
public DebugLog(String logger, String file) {
log = Logger.getLogger(logger);
try {
fh = new FileHandler(file, true);
log.setUseParentHandlers(false);
for (Handler handler : log.getHandlers()) {
log.removeHandler(handler);
}
log.addHandler(fh);
log.setLevel(Level.ALL);
fh.setFormatter(new LogFormatter());
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void log(Level level, String msg) {
log.log(level, msg);
}
private class LogFormatter extends Formatter {
private final SimpleDateFormat date = new SimpleDateFormat("yyyy-MM-dd 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(record.getMessage());
builder.append('\n');
if (ex != null) {
StringWriter writer = new StringWriter();
ex.printStackTrace(new PrintWriter(writer));
builder.append(writer);
}
return builder.toString();
}
}
public void close() {
fh.close();
}
}

View File

@ -0,0 +1,25 @@
package com.onarandombox.utils;
import org.bukkit.command.CommandSender;
import com.onarandombox.MultiverseCore.MultiverseCore;
public class Messaging {
public void send(CommandSender player, String msg, String... params) {
player.sendMessage(parameterizeMessage(msg, params));
}
public void broadcast(MultiverseCore plugin, String msg, String... params) {
plugin.getServer().broadcastMessage(parameterizeMessage(msg, params));
}
private String parameterizeMessage(String msg, String... params) {
msg = "§cHeroes: " + msg;
for (int i = 0; i < params.length; i++) {
msg = msg.replace("$" + (i + 1), "§f" + params[i] + "§c");
}
return msg;
}
}