From ab13d5adb42b155521d0abb926d545f6e0fccaa0 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 21 Dec 2019 18:19:10 +0100 Subject: [PATCH] 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 --- src/main/java/envoy/client/Config.java | 7 ++++--- src/main/java/envoy/client/ui/Startup.java | 2 +- src/main/java/envoy/client/util/EnvoyLog.java | 15 +++++++++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/main/java/envoy/client/Config.java b/src/main/java/envoy/client/Config.java index 3e511f9..17da0d8 100644 --- a/src/main/java/envoy/client/Config.java +++ b/src/main/java/envoy/client/Config.java @@ -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 diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index 21e4cf0..db9644a 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -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()); diff --git a/src/main/java/envoy/client/util/EnvoyLog.java b/src/main/java/envoy/client/util/EnvoyLog.java index 282e1b4..b59b714 100644 --- a/src/main/java/envoy/client/util/EnvoyLog.java +++ b/src/main/java/envoy/client/util/EnvoyLog.java @@ -10,7 +10,7 @@ import envoy.client.Config; * Project: envoy-client
* File: EnvoyLogger.java
* Created: 14 Dec 2019
- * + * * @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