use of com.voxelgameslib.voxelgameslib.handler.Handler in project VoxelGamesLibv2 by VoxelGamesLib.
the class LoggingHandler method enable.
@Override
public void enable() {
System.out.println("[VoxelGamesLib] Taking over logging...");
// force everybody to use the parent handler
java.util.logging.LogManager manager = java.util.logging.LogManager.getLogManager();
Enumeration<String> names = manager.getLoggerNames();
while (names.hasMoreElements()) {
String name = names.nextElement();
if (name.startsWith("com.voxelgameslib")) {
Logger logger = Logger.getLogger(name);
logger.setUseParentHandlers(true);
}
}
parent = Logger.getLogger("com.voxelgameslib");
// get file logger
org.apache.logging.log4j.core.Logger log4j = (org.apache.logging.log4j.core.Logger) LogManager.getLogger("Minecraft");
java.util.Optional<Appender> appender = log4j.getContext().getConfiguration().getAppenders().values().stream().filter(app -> app instanceof RollingRandomAccessFileAppender).findAny();
RollingRandomAccessFileAppender log4jAppender = null;
if (appender.isPresent()) {
log4jAppender = (RollingRandomAccessFileAppender) appender.get();
} else {
log.warning("COULD NOT FIND LOG4j APPENDER! FILE LOGGING IS DISABLED!");
}
LogFormatter logFormatter = new LogFormatter(log4jAppender);
// fuck everyone
// jul first
java.util.logging.Logger global = java.util.logging.Logger.getLogger("");
global.setUseParentHandlers(false);
for (java.util.logging.Handler handler : global.getHandlers()) {
global.removeHandler(handler);
}
// log4j later
log4j.getContext().getConfiguration().getAppenders().values().forEach(log4j::removeAppender);
// forward
// get all log4j messages and let them go thru our handler
((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger()).addFilter(new Log4JForwardHandler(logFormatter));
// get all jul messages and let them go thru our handler
global.addHandler(new JULForwardHandler(logFormatter));
// get all sout messages and let them go thru out handler (via jul)
System.setOut(new PrintStream(new SoutForwardHandler(), true));
System.setErr(new PrintStream(new SoutForwardHandler(), true));
}
Aggregations