Implemented logger level configuration, added Javadoc

Fixes #45
This commit is contained in:
Kai S. K. Engelbart 2019-12-21 12:20:23 +01:00
parent 1324356827
commit d62793b810
4 changed files with 81 additions and 31 deletions

View File

@ -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(); }
}

View File

@ -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.<br>
* <br>
* Project: <strong>envoy-clientChess</strong><br>
* File: <strong>ConfigItem.javaEvent.java</strong><br>
* Created: <strong>21.12.2019</strong><br>
*
* @author Kai S. K. Engelbart
* @param <T> the type of the config item's value
* @since Envoy v0.2-alpha
*/
public class ConfigItem<T> {
@ -15,6 +20,16 @@ public class ConfigItem<T> {
private Function<String, T> 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<String, T>} 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<String, T> parseFunction, T defaultValue) {
this.commandLong = commandLong;
this.commandShort = commandShort;
@ -22,11 +37,28 @@ public class ConfigItem<T> {
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; }
}

View File

@ -60,6 +60,7 @@ public class Startup {
e.printStackTrace();
}
// Set new logger levels loaded from config
EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier());
EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier());

View File

@ -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);
}
}