Search in sources :

Example 1 with Handler

use of com.voxelgameslib.voxelgameslib.internal.handler.Handler in project VoxelGamesLibv2 by VoxelGamesLib.

the class LoggingHandler method enable.

@Override
public void enable() {
    System.out.println("[VoxelGamesLib] Taking over logging...");
    if (errorHandler == null) {
        System.err.println("ERRORHANDLER IS NULL, ABORTING");
        return;
    }
    // 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, errorHandler);
    // 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) User(com.voxelgameslib.voxelgameslib.components.user.User) CommandAlias(co.aikar.commands.annotation.CommandAlias) CommandCompletion(co.aikar.commands.annotation.CommandCompletion) Enumeration(java.util.Enumeration) BaseCommand(co.aikar.commands.BaseCommand) Appender(org.apache.logging.log4j.core.Appender) Optional(co.aikar.commands.annotation.Optional) Handler(com.voxelgameslib.voxelgameslib.internal.handler.Handler) Logger(java.util.logging.Logger) Singleton(javax.inject.Singleton) Lang(com.voxelgameslib.voxelgameslib.internal.lang.Lang) ErrorHandler(com.voxelgameslib.voxelgameslib.internal.error.ErrorHandler) Level(java.util.logging.Level) Syntax(co.aikar.commands.annotation.Syntax) LangKey(com.voxelgameslib.voxelgameslib.internal.lang.LangKey) 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 User (com.voxelgameslib.voxelgameslib.components.user.User)1 ErrorHandler (com.voxelgameslib.voxelgameslib.internal.error.ErrorHandler)1 Handler (com.voxelgameslib.voxelgameslib.internal.handler.Handler)1 Lang (com.voxelgameslib.voxelgameslib.internal.lang.Lang)1 LangKey (com.voxelgameslib.voxelgameslib.internal.lang.LangKey)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