Merge pull request #136 from AuthMe-Team/writelog

better method to write the messages
This commit is contained in:
Gabriele C 2016-06-01 11:37:37 +02:00
commit e6f2eb487d
4 changed files with 49 additions and 18 deletions

View File

@ -502,6 +502,7 @@ public class AuthMe extends JavaPlugin {
// Disabled correctly // Disabled correctly
ConsoleLogger.info("AuthMe " + this.getDescription().getVersion() + " disabled!"); ConsoleLogger.info("AuthMe " + this.getDescription().getVersion() + " disabled!");
ConsoleLogger.close();
} }
// Stop/unload the server/plugin as defined in the configuration // Stop/unload the server/plugin as defined in the configuration

View File

@ -6,9 +6,8 @@ import fr.xephi.authme.settings.properties.SecuritySettings;
import fr.xephi.authme.util.StringUtils; import fr.xephi.authme.util.StringUtils;
import java.io.File; 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.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Date; import java.util.Date;
@ -25,6 +24,7 @@ public final class ConsoleLogger {
private static boolean enableDebug = false; private static boolean enableDebug = false;
private static boolean useLogging = false; private static boolean useLogging = false;
private static File logFile; private static File logFile;
private static FileWriter fileWriter;
private ConsoleLogger() { private ConsoleLogger() {
} }
@ -40,6 +40,16 @@ public final class ConsoleLogger {
public static void setLoggingOptions(NewSetting settings) { public static void setLoggingOptions(NewSetting settings) {
ConsoleLogger.useLogging = settings.getProperty(SecuritySettings.USE_LOGGING); ConsoleLogger.useLogging = settings.getProperty(SecuritySettings.USE_LOGGING);
ConsoleLogger.enableDebug = !settings.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE); ConsoleLogger.enableDebug = !settings.getProperty(SecuritySettings.REMOVE_SPAM_FROM_CONSOLE);
if (useLogging) {
if (fileWriter == null) {
try {
fileWriter = new FileWriter(logFile, true);
} catch (IOException ignored) {
}
}
} else {
close();
}
} }
/** /**
@ -49,6 +59,10 @@ public final class ConsoleLogger {
*/ */
public static void info(String message) { public static void info(String message) {
logger.info(message); logger.info(message);
if (useLogging) {
writeLog(message);
}
} }
public static void debug(String message) { public static void debug(String message) {
@ -83,9 +97,11 @@ public final class ConsoleLogger {
dateTime = DATE_FORMAT.format(new Date()); dateTime = DATE_FORMAT.format(new Date());
} }
try { try {
Files.write(logFile.toPath(), (dateTime + ": " + message + NEW_LINE).getBytes(), fileWriter.write(dateTime);
StandardOpenOption.APPEND, fileWriter.write(": ");
StandardOpenOption.CREATE); fileWriter.write(message);
fileWriter.write(NEW_LINE);
fileWriter.flush();
} catch (IOException ignored) { } catch (IOException ignored) {
} }
} }
@ -105,10 +121,21 @@ public final class ConsoleLogger {
* Logs a Throwable with the provided message and saves the stack trace to the log file. * 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 message The message to accompany the exception
* @param th The Throwable to log * @param th The Throwable to log
*/ */
public static void logException(String message, Throwable th) { public static void logException(String message, Throwable th) {
showError(message + " " + StringUtils.formatException(th)); showError(message + " " + StringUtils.formatException(th));
writeStackTrace(th); writeStackTrace(th);
} }
public static void close() {
if (fileWriter != null) {
try {
fileWriter.flush();
fileWriter.close();
fileWriter = null;
} catch (IOException ignored) {
}
}
}
} }

View File

@ -35,12 +35,12 @@ public class ReloadCommand implements ExecutableCommand {
public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) { public void executeCommand(CommandSender sender, List<String> arguments, CommandService commandService) {
try { try {
settings.reload(); settings.reload();
ConsoleLogger.setLoggingOptions(settings);
// We do not change database type for consistency issues, but we'll output a note in the logs // We do not change database type for consistency issues, but we'll output a note in the logs
if (!settings.getProperty(DatabaseSettings.BACKEND).equals(dataSource.getType())) { if (!settings.getProperty(DatabaseSettings.BACKEND).equals(dataSource.getType())) {
ConsoleLogger.info("Note: cannot change database type during /authme reload"); ConsoleLogger.info("Note: cannot change database type during /authme reload");
sender.sendMessage("Note: cannot change database type during /authme reload"); sender.sendMessage("Note: cannot change database type during /authme reload");
} }
ConsoleLogger.setLoggingOptions(settings);
initializer.performReloadOnServices(); initializer.performReloadOnServices();
commandService.send(sender, MessageKey.CONFIG_RELOAD_SUCCESS); commandService.send(sender, MessageKey.CONFIG_RELOAD_SUCCESS);
} catch (Exception e) { } catch (Exception e) {

View File

@ -39,18 +39,21 @@ public class GeoLiteAPI {
} }
final File pluginFolder = AuthMe.getInstance().getDataFolder(); final File pluginFolder = AuthMe.getInstance().getDataFolder();
final File data = new File(pluginFolder, "GeoIP.dat"); final File data = new File(pluginFolder, "GeoIP.dat");
boolean dataIsOld = (System.currentTimeMillis() - data.lastModified()) > TimeUnit.DAYS.toMillis(30);
if (dataIsOld && !data.delete()) {
ConsoleLogger.showError("Failed to delete GeoLiteAPI database");
}
if (data.exists()) { if (data.exists()) {
try { boolean dataIsOld = (System.currentTimeMillis() - data.lastModified()) > TimeUnit.DAYS.toMillis(30);
lookupService = new LookupService(data); if (!dataIsOld) {
ConsoleLogger.info(LICENSE); try {
return true; lookupService = new LookupService(data);
} catch (IOException e) { ConsoleLogger.info(LICENSE);
ConsoleLogger.logException("Failed to load GeoLiteAPI database", e); return true;
return false; } catch (IOException e) {
ConsoleLogger.logException("Failed to load GeoLiteAPI database", e);
return false;
}
} else {
if (!data.delete()) {
ConsoleLogger.showError("Failed to delete GeoLiteAPI database");
}
} }
} }
// Ok, let's try to download the data file! // Ok, let's try to download the data file!