Improved logging

Logs are now written to System.out instead of System.err. Also they are
not duplicated as the default ConsoleHandler has been removed.

When using the application, logs may not appear in the console
immediately as the StreamHandler used to output them used an internal
buffer that may only be flushed when closing the application.

Logs are now formatted as [DATE TIME] [LEVEL] [LOGGER] MSG
This commit is contained in:
Kai S. K. Engelbart 2019-12-21 18:19:10 +01:00
parent 408cda25f7
commit ab13d5adb4
3 changed files with 16 additions and 8 deletions

View File

@ -32,7 +32,8 @@ public class Config {
items.put("port", new ConfigItem<>("port", "p", (input) -> Integer.parseInt(input), null));
items.put("localDB", new ConfigItem<>("localDB", "db", (input) -> new File(input), new File("localDB")));
items.put("syncTimeout", new ConfigItem<>("syncTimeout", "st", (input) -> Integer.parseInt(input), 1000));
items.put("homeDirectory", new ConfigItem<>("homeDirectory", "h", (input) -> new File(input), new File(System.getProperty("user.home"), ".envoy")));
items.put("homeDirectory",
new ConfigItem<>("homeDirectory", "h", (input) -> new File(input), new File(System.getProperty("user.home"), ".envoy")));
items.put("fileLevelBarrier", new ConfigItem<>("fileLevelBarrier", "fb", (input) -> Level.parse(input), Level.CONFIG));
items.put("consoleLevelBarrier", new ConfigItem<>("consoleLevelBarrier", "cb", (input) -> Level.parse(input), Level.FINEST));
}
@ -130,13 +131,13 @@ public class Config {
* @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

View File

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

View File

@ -10,7 +10,7 @@ import envoy.client.Config;
* Project: <strong>envoy-client</strong><br>
* File: <strong>EnvoyLogger.java</strong><br>
* Created: <strong>14 Dec 2019</strong><br>
*
*
* @author Leon Hofmeister
* @author Kai S. K. Engelbart
* @since Envoy v0.2-alpha
@ -18,11 +18,18 @@ import envoy.client.Config;
public class EnvoyLog {
private static FileHandler fileHandler;
private static ConsoleHandler consoleHandler;
private static StreamHandler consoleHandler;
static {
// Remove default console handler
LogManager.getLogManager().reset();
// Configure log file
File logFile = new File(Config.getInstance().getHomeDirectory(), "log/envoy_user.log");
logFile.getParentFile().mkdirs();
// Configure formatting
System.setProperty("java.util.logging.SimpleFormatter.format", "[%1$tF %1$tT] [%4$-7s] [%3$s] %5$s %n");
SimpleFormatter formatter = new SimpleFormatter();
try {
@ -32,7 +39,7 @@ public class EnvoyLog {
} catch (SecurityException | IOException e) {
e.printStackTrace();
}
consoleHandler = new ConsoleHandler();
consoleHandler = new StreamHandler(System.out, formatter);
consoleHandler.setLevel(Config.getInstance().getConsoleLevelBarrier());
consoleHandler.setFormatter(formatter);
}
@ -41,7 +48,7 @@ public class EnvoyLog {
/**
* Creates a {@link Logger} with a specified name
*
*
* @param name the name of the {@link Logger} to create
* @return the created {@link Logger}
* @since Envoy v0.2-alpha