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 bc0a44bca1
commit 34e9dc9e8b
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));
}

View File

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