Adder EnvoyLog#attach(String) method to log specific packages

This commit is contained in:
Kai S. K. Engelbart 2020-02-18 08:14:04 +01:00
parent 5f9ebd4c43
commit c692fe25ff
2 changed files with 21 additions and 9 deletions

View File

@ -68,7 +68,8 @@ public class Startup {
System.exit(1); System.exit(1);
} }
// Set new logger levels loaded from config // Setup logger for the envoy package
EnvoyLog.attach("envoy");
EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier()); EnvoyLog.setFileLevelBarrier(config.getFileLevelBarrier());
EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier()); EnvoyLog.setConsoleLevelBarrier(config.getConsoleLevelBarrier());

View File

@ -19,17 +19,14 @@ import envoy.client.data.Config;
*/ */
public class EnvoyLog { public class EnvoyLog {
private static Logger root;
private static FileHandler fileHandler; private static FileHandler fileHandler;
private static StreamHandler consoleHandler; private static StreamHandler consoleHandler;
static { static {
// Remove default console handler // Remove default console handler
LogManager.getLogManager().reset(); LogManager.getLogManager().reset();
// Get root logger
root = Logger.getLogger("envoy");
// Configure log file // Configure log file
File logFile = new File(Config.getInstance().getHomeDirectory(), File logFile = new File(Config.getInstance().getHomeDirectory(),
"log/envoy_user_" + new SimpleDateFormat("yyyy-MM-dd--hh-mm-mm").format(new Date()) + ".log"); "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 = new FileHandler(logFile.getAbsolutePath());
fileHandler.setLevel(Config.getInstance().getFileLevelBarrier()); fileHandler.setLevel(Config.getInstance().getFileLevelBarrier());
fileHandler.setFormatter(formatter); fileHandler.setFormatter(formatter);
root.addHandler(fileHandler);
} catch (SecurityException | IOException e) { } catch (SecurityException | IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
consoleHandler = new StreamHandler(System.out, formatter); consoleHandler = new StreamHandler(System.out, formatter);
consoleHandler.setLevel(Config.getInstance().getConsoleLevelBarrier()); consoleHandler.setLevel(Config.getInstance().getConsoleLevelBarrier());
consoleHandler.setFormatter(formatter); consoleHandler.setFormatter(formatter);
root.addHandler(consoleHandler);
root.setLevel(Level.ALL);
} }
private EnvoyLog() {} 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 * Creates a {@link Logger} with a specified name
* *