From c692fe25ffa0570e78ff51377393cfa6dccd5bbb Mon Sep 17 00:00:00 2001 From: kske Date: Tue, 18 Feb 2020 08:14:04 +0100 Subject: [PATCH] Adder EnvoyLog#attach(String) method to log specific packages --- src/main/java/envoy/client/ui/Startup.java | 3 ++- src/main/java/envoy/client/util/EnvoyLog.java | 27 +++++++++++++------ 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/main/java/envoy/client/ui/Startup.java b/src/main/java/envoy/client/ui/Startup.java index a6c1aaa..62d3f69 100644 --- a/src/main/java/envoy/client/ui/Startup.java +++ b/src/main/java/envoy/client/ui/Startup.java @@ -68,7 +68,8 @@ public class Startup { System.exit(1); } - // Set new logger levels loaded from config + // Setup logger for the envoy package + EnvoyLog.attach("envoy"); 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 451810f..ac90cd2 100644 --- a/src/main/java/envoy/client/util/EnvoyLog.java +++ b/src/main/java/envoy/client/util/EnvoyLog.java @@ -19,17 +19,14 @@ import envoy.client.data.Config; */ public class EnvoyLog { - private static Logger root; private static FileHandler fileHandler; private static StreamHandler consoleHandler; static { + // Remove default console handler LogManager.getLogManager().reset(); - // Get root logger - root = Logger.getLogger("envoy"); - // Configure log file File logFile = new File(Config.getInstance().getHomeDirectory(), "log/envoy_user_" + new SimpleDateFormat("yyyy-MM-dd--hh-mm-mm").format(new Date()) + ".log"); @@ -43,20 +40,34 @@ public class EnvoyLog { fileHandler = new FileHandler(logFile.getAbsolutePath()); fileHandler.setLevel(Config.getInstance().getFileLevelBarrier()); fileHandler.setFormatter(formatter); - root.addHandler(fileHandler); } catch (SecurityException | IOException e) { e.printStackTrace(); } consoleHandler = new StreamHandler(System.out, formatter); consoleHandler.setLevel(Config.getInstance().getConsoleLevelBarrier()); consoleHandler.setFormatter(formatter); - root.addHandler(consoleHandler); - - root.setLevel(Level.ALL); } private EnvoyLog() {} + /** + * Configures all loggers that are contained within the hierarchy of a specific + * path + * + * @param path the path to the loggers to configure + */ + public static void attach(String path) { + // Get root logger + Logger logger = Logger.getLogger(path); + + // Add handlers + if (fileHandler != null) logger.addHandler(fileHandler); + logger.addHandler(consoleHandler); + + // Delegate logger level filtering to the handlers + logger.setLevel(Level.ALL); + } + /** * Creates a {@link Logger} with a specified name *