update ConsoleLogger

This commit is contained in:
DNx5 2015-09-15 14:33:23 +07:00
parent e406a7b21c
commit e1b2a1852c

View File

@ -1,33 +1,27 @@
package fr.xephi.authme; package fr.xephi.authme;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import fr.xephi.authme.api.NewAPI;
import fr.xephi.authme.settings.Settings; import fr.xephi.authme.settings.Settings;
import org.bukkit.Bukkit;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.Calendar; import java.util.Date;
import java.util.logging.Logger; import java.util.logging.Logger;
public class ConsoleLogger { public class ConsoleLogger {
private static final Logger log = Logger.getLogger("AuthMe"); private static final Logger log = AuthMe.getInstance().getLogger();
private static final DateFormat format = DateFormat.getDateTimeInstance();
public static void info(String message) { public static void info(String message) {
if (AuthMe.getInstance().isEnabled()) { if (AuthMe.getInstance().isEnabled()) {
log.info("[AuthMe] " + message); log.info("[AuthMe] " + message);
if (Settings.useLogging) { if (Settings.useLogging) {
Calendar date = Calendar.getInstance(); writeLog("[" + format.format(new Date()) + "] " + message);
final String actually = "[" + DateFormat.getDateInstance().format(date.getTime()) + ", " + date.get(Calendar.HOUR_OF_DAY) + ":" + date.get(Calendar.MINUTE) + ":" + date.get(Calendar.SECOND) + "] " + message;
Bukkit.getScheduler().runTaskAsynchronously(AuthMe.getInstance(), new Runnable() {
@Override
public void run() {
writeLog(actually);
}
});
} }
} }
} }
@ -36,31 +30,21 @@ public class ConsoleLogger {
if (AuthMe.getInstance().isEnabled()) { if (AuthMe.getInstance().isEnabled()) {
log.warning("[AuthMe] " + message); log.warning("[AuthMe] " + message);
if (Settings.useLogging) { if (Settings.useLogging) {
Calendar date = Calendar.getInstance(); writeLog("[" + format.format(new Date()) + "] ERROR: " + message);
final String actually = "[" + DateFormat.getDateInstance().format(date.getTime()) + ", " + date.get(Calendar.HOUR_OF_DAY) + ":" + date.get(Calendar.MINUTE) + ":" + date.get(Calendar.SECOND) + "] ERROR : " + message;
Bukkit.getScheduler().runTaskAsynchronously(AuthMe.getInstance(), new Runnable() {
@Override
public void run() {
writeLog(actually);
}
});
} }
} }
} }
public static void writeLog(String string) { public static void writeLog(String message) {
try { try {
FileWriter fw = new FileWriter(AuthMe.getInstance().getDataFolder() + File.separator + "authme.log", true); Files.write(Settings.LOG_FILE.toPath(), (message + NewAPI.newline).getBytes(),
BufferedWriter w = new BufferedWriter(fw); StandardOpenOption.APPEND,
w.write(string); StandardOpenOption.CREATE);
w.newLine(); } catch (IOException ignored) {
w.close();
} catch (IOException e) {
e.printStackTrace();
} }
} }
public static void writeStackTrace(Exception ex){ public static void writeStackTrace(Exception ex) {
writeLog(Throwables.getStackTraceAsString(ex)); writeLog(Throwables.getStackTraceAsString(ex));
} }
} }