diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java index b479de8..14dc6f6 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/Config.java @@ -125,9 +125,21 @@ public class Config { */ public int getSyncTimeout() { return (int) items.get("syncTimeout").get(); } + /** + * @return the directory in which all local files are saves + * @since Envoy v0.2-alpha + */ public File getHomeDirectory() { return (File) items.get("homeDirectory").get(); } + /** + * @return the minimal {@link Level} to log inside the log file + * @since Envoy v0.2-alpha + */ public Level getFileLevelBarrier() { return (Level) items.get("fileLevelBarrier").get(); } + /** + * @return the minimal {@link Level} to log inside the console + * @since Envoy v0.2-alpha + */ public Level getConsoleLevelBarrier() { return (Level) items.get("consoleLevelBarrier").get(); } } diff --git a/src/main/java/envoy/client/ConfigItem.java b/src/main/java/envoy/client/ConfigItem.java index fb30e10..87e927b 100644 --- a/src/main/java/envoy/client/ConfigItem.java +++ b/src/main/java/envoy/client/ConfigItem.java @@ -3,11 +3,16 @@ package envoy.client; import java.util.function.Function; /** + * Contains a single {@link Config} value as well as the corresponding command + * line arguments and its default value.
+ *
* Project: envoy-clientChess
* File: ConfigItem.javaEvent.java
* Created: 21.12.2019
* * @author Kai S. K. Engelbart + * @param the type of the config item's value + * @since Envoy v0.2-alpha */ public class ConfigItem { @@ -15,6 +20,16 @@ public class ConfigItem { private Function parseFunction; private T value; + /** + * Initializes a {@link ConfigItem} + * + * @param commandLong the long command line argument to set this value + * @param commandShort the short command line argument to set this value + * @param parseFunction the {@code Function} that parses the value + * from a string + * @param defaultValue the optional default value to set before parsing + * @since Envoy v0.2-alpha + */ public ConfigItem(String commandLong, String commandShort, Function parseFunction, T defaultValue) { this.commandLong = commandLong; this.commandShort = commandShort; @@ -22,11 +37,28 @@ public class ConfigItem { value = defaultValue; } + /** + * Parses this {@ConfigItem}'s value from a string + * @param input the string to parse from + * @since Envoy v0.2-alpha + */ public void parse(String input) { value = parseFunction.apply(input); } + /** + * @return The long command line argument to set the value of this {@link ConfigItem} + * @since Envoy v0.2-alpha + */ public String getCommandLong() { return commandLong; } + /** + * @return The short command line argument to set the value of this {@link ConfigItem} + * @since Envoy v0.2-alpha + */ public String getCommandShort() { return commandShort; } + /** + * @return the value of this {@link ConfigItem} + * @since Envoy v0.2-alpha + */ public T get() { return value; } } diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 11db9f6..21e4cf0 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -60,6 +60,7 @@ public class Startup { e.printStackTrace(); } + // Set new logger levels loaded from config EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier()); EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier()); diff --git a/src/main/java/envoy/client/util/EnvoyLog.java b/src/main/java/envoy/client/util/EnvoyLog.java index 1e93b4c..ab2012d 100644 --- a/src/main/java/envoy/client/util/EnvoyLog.java +++ b/src/main/java/envoy/client/util/EnvoyLog.java @@ -17,7 +17,25 @@ import envoy.client.Config; */ public class EnvoyLog { - private static Level fileLevelBarrier = Config.getInstance().getFileLevelBarrier(), consoleLevelBarrier = Config.getInstance().getConsoleLevelBarrier(); + private static FileHandler fileHandler; + private static ConsoleHandler consoleHandler; + + static { + File logFile = new File(Config.getInstance().getHomeDirectory(), "log/envoy_user.log"); + logFile.getParentFile().mkdirs(); + SimpleFormatter formatter = new SimpleFormatter(); + + try { + fileHandler = new FileHandler(logFile.getAbsolutePath()); + fileHandler.setLevel(Config.getInstance().getFileLevelBarrier()); + fileHandler.setFormatter(formatter); + } catch (SecurityException | IOException e) { + e.printStackTrace(); + } + consoleHandler = new ConsoleHandler(); + consoleHandler.setLevel(Config.getInstance().getConsoleLevelBarrier()); + consoleHandler.setFormatter(formatter); + } private EnvoyLog() {} @@ -31,44 +49,31 @@ public class EnvoyLog { // Get a logger with the specified name Logger logger = Logger.getLogger(name); - final String logPath = new File(Config.getInstance().getHomeDirectory(), "log/envoy_user.log").getAbsolutePath(); - new File(logPath).getParentFile().mkdirs(); - - SimpleFormatter formatter = new SimpleFormatter(); - - try { - FileHandler fh = new FileHandler(logPath); - fh.setLevel(fileLevelBarrier); - fh.setFormatter(formatter); - logger.addHandler(fh); - } catch (SecurityException | IOException e) { - e.printStackTrace(); - } - - ConsoleHandler ch = new ConsoleHandler(); - ch.setLevel(consoleLevelBarrier); - ch.setFormatter(formatter); - logger.addHandler(ch); + // Add handlers + if (fileHandler != null) logger.addHandler(fileHandler); + if (consoleHandler != null) logger.addHandler(consoleHandler); return logger; } /** - * @return the fileLevelBarrier: The current barrier for writing logs to a file. - * @since Envoy v0.2-alpha - */ - public static Level getFileLevelBarrier() { return fileLevelBarrier; } - - /** - * @param fileLevelBarrier the severity below which logRecords will be written - * only to the console. At or above they'll also be + * @param fileLevelBarrier the severity below which logRecords will not be + * written to the log file. At or above they'll also be * logged in a file. Can be written either in Digits * from 0 - 1000 or with the according name of the level * @since Envoy v0.2-alpha */ - public static void setFileLevelBarrier(Level fileLevelBarrier) { EnvoyLog.fileLevelBarrier = fileLevelBarrier; } + public static void setFileLevelBarrier(Level fileLevelBarrier) { if (fileHandler != null) fileHandler.setLevel(fileLevelBarrier); } - public static Level getConsoleLevelBarrier() { return consoleLevelBarrier; } - - public static void setConsoleLevelBarrier(Level consoleLevelBarrier) { EnvoyLog.consoleLevelBarrier = consoleLevelBarrier; } + /** + * @param consoleLevelBarrier the severity below which logRecords will not be + * written to the console. At or above they'll also + * be logged in a file. Can be written either in + * digits from 0 - 1000 or with the according name of + * the level + * @since Envoy v0.2-alpha + */ + public static void setConsoleLevelBarrier(Level consoleLevelBarrier) { + if (consoleHandler != null) consoleHandler.setLevel(consoleLevelBarrier); + } }