Search in sources :

Example 1 with Handler

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));
}
Also used : RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender) Appender(org.apache.logging.log4j.core.Appender) PrintStream(java.io.PrintStream) RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender) Subcommand(co.aikar.commands.annotation.Subcommand) CommandAlias(co.aikar.commands.annotation.CommandAlias) CommandCompletion(co.aikar.commands.annotation.CommandCompletion) Enumeration(java.util.Enumeration) LangKey(com.voxelgameslib.voxelgameslib.lang.LangKey) BaseCommand(co.aikar.commands.BaseCommand) Appender(org.apache.logging.log4j.core.Appender) Optional(co.aikar.commands.annotation.Optional) Logger(java.util.logging.Logger) Singleton(javax.inject.Singleton) Lang(com.voxelgameslib.voxelgameslib.lang.Lang) Level(java.util.logging.Level) Handler(com.voxelgameslib.voxelgameslib.handler.Handler) Syntax(co.aikar.commands.annotation.Syntax) User(com.voxelgameslib.voxelgameslib.user.User) CommandPermission(co.aikar.commands.annotation.CommandPermission) LogManager(org.apache.logging.log4j.LogManager) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) PrintStream(java.io.PrintStream) Logger(java.util.logging.Logger) Logger(java.util.logging.Logger) RollingRandomAccessFileAppender(org.apache.logging.log4j.core.appender.RollingRandomAccessFileAppender)

Aggregations

BaseCommand (co.aikar.commands.BaseCommand)1 CommandAlias (co.aikar.commands.annotation.CommandAlias)1 CommandCompletion (co.aikar.commands.annotation.CommandCompletion)1 CommandPermission (co.aikar.commands.annotation.CommandPermission)1 Optional (co.aikar.commands.annotation.Optional)1 Subcommand (co.aikar.commands.annotation.Subcommand)1 Syntax (co.aikar.commands.annotation.Syntax)1 Handler (com.voxelgameslib.voxelgameslib.handler.Handler)1 Lang (com.voxelgameslib.voxelgameslib.lang.Lang)1 LangKey (com.voxelgameslib.voxelgameslib.lang.LangKey)1 User (com.voxelgameslib.voxelgameslib.user.User)1 PrintStream (java.io.PrintStream)1 Enumeration (java.util.Enumeration)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Nonnull (javax.annotation.Nonnull)1 Nullable (javax.annotation.Nullable)1 Singleton (javax.inject.Singleton)1 LogManager (org.apache.logging.log4j.LogManager)1 Appender (org.apache.logging.log4j.core.Appender)1