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 a7e9c70cfc
commit 6dd1b40130
2 changed files with 21 additions and 9 deletions

View File

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

View File

@ -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
*