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
ConsoleLogger.info("AuthMe " + this.getDescription().getVersion() + " disabled!");
ConsoleLogger.close();
}
// 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 java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -25,6 +24,7 @@ public final class ConsoleLogger {
private static boolean enableDebug = false;
private static boolean useLogging = false;
private static File logFile;
private static FileWriter fileWriter;
private ConsoleLogger() {
}
@ -40,6 +40,16 @@ public final class ConsoleLogger {
public static void setLoggingOptions(NewSetting settings) {
ConsoleLogger.useLogging = settings.getProperty(SecuritySettings.USE_LOGGING);
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) {
logger.info(message);
if (useLogging) {
writeLog(message);
}
}
public static void debug(String message) {
@ -83,9 +97,11 @@ public final class ConsoleLogger {
dateTime = DATE_FORMAT.format(new Date());
}
try {
Files.write(logFile.toPath(), (dateTime + ": " + message + NEW_LINE).getBytes(),
StandardOpenOption.APPEND,
StandardOpenOption.CREATE);
fileWriter.write(dateTime);
fileWriter.write(": ");
fileWriter.write(message);
fileWriter.write(NEW_LINE);
fileWriter.flush();
} 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.
*
* @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) {
showError(message + " " + StringUtils.formatException(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) {
try {
settings.reload();
ConsoleLogger.setLoggingOptions(settings);
// 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())) {
ConsoleLogger.info("Note: cannot change database type during /authme reload");
sender.sendMessage("Note: cannot change database type during /authme reload");
}
ConsoleLogger.setLoggingOptions(settings);
initializer.performReloadOnServices();
commandService.send(sender, MessageKey.CONFIG_RELOAD_SUCCESS);
} catch (Exception e) {

View File

@ -39,18 +39,21 @@ public class GeoLiteAPI {
}
final File pluginFolder = AuthMe.getInstance().getDataFolder();
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()) {
try {
lookupService = new LookupService(data);
ConsoleLogger.info(LICENSE);
return true;
} catch (IOException e) {
ConsoleLogger.logException("Failed to load GeoLiteAPI database", e);
return false;
boolean dataIsOld = (System.currentTimeMillis() - data.lastModified()) > TimeUnit.DAYS.toMillis(30);
if (!dataIsOld) {
try {
lookupService = new LookupService(data);
ConsoleLogger.info(LICENSE);
return true;
} 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!