package com.gmail.inverseconduit;

import java.io.IOException;
import java.io.Reader;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.Policy;
import java.util.HashSet;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.gmail.inverseconduit.bot.Program;
import com.gmail.inverseconduit.security.ScriptSecurityManager;
import com.gmail.inverseconduit.security.ScriptSecurityPolicy;

public class Main {

    // Prevents GC of loggers before htmlUnit can get them again...
    private static final Set<Logger> disabledLoggers = new HashSet<>();

    static {
        disabledLoggers.add(Logger.getLogger("com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter"));
        disabledLoggers.add(Logger.getLogger("com.gargoylesoftware.htmlunit.DefaultCssErrorHandler"));
        disabledLoggers.add(Logger.getLogger("com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl"));
        disabledLoggers.add(Logger.getLogger("com.gargoylesoftware.htmlunit.html.InputElementFactory"));
    }

    public static void main(String[] args) throws Exception {
        // HtmlUnit didn't properly clean up, so we have to
        disabledLoggers.forEach(l -> l.setLevel(Level.OFF));

        //sandbox this ...
        Policy.setPolicy(ScriptSecurityPolicy.getInstance());
        System.setSecurityManager(ScriptSecurityManager.getInstance());
        
        BotConfig config = loadConfig();
        AppContext.INSTANCE.add(config);

        Program p = new Program();
        p.startup();
    }
    
    private static BotConfig loadConfig() throws IOException{
    	Path file = Paths.get("bot.properties");
    	Properties properties = new Properties();
    	try (Reader reader = Files.newBufferedReader(file, Charset.forName("UTF-8"))){
    		properties.load(reader);
    	}
    	return new BotConfig(properties);
    }
}

@@ -241,23 +241,26 @@
 va.util.
-HashSet
+Properties
 ;%0Aimport
@@ -266,34 +266,38 @@
 t java.util.
-Properties
+logging.Filter
 ;%0Aimport jav
@@ -303,19 +303,31 @@
 va.util.
-Set
+logging.Handler
 ;%0Aimport
@@ -346,20 +346,24 @@
 ogging.L
-evel
+ogRecord
 ;%0Aimport
@@ -587,1087 +587,939 @@
 n %7B%0A
-%0A    // Prevents GC of loggers before htmlUnit can get them again...%0A    private static final Set%3CLogger%3E disabledLoggers = new HashSet%3C%3E();%0A%0A    static %7B%0A        disabledLoggers.add(Logger.getLogger(%22com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter%22));%0A        disabledLoggers.add(Logger.getLogger(%22com.gargoylesoftware.htmlunit.DefaultCssErrorHandler%22));%0A        disabledLoggers.add(Logger.getLogger(%22com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl%22));%0A        disabledLoggers.add(Logger.getLogger(%22com.gargoylesoftware.htmlunit.html.InputElementFactory%22));%0A    %7D%0A%0A    public static void main(String%5B%5D args) throws Exception %7B%0A        // HtmlUnit didn't properly clean up, so we have to%0A        disabledLoggers.forEach(l -%3E l.setLevel(Level.OFF));%0A%0A        //sandbox this ...%0A        Policy.setPolicy(ScriptSecurityPolicy.getInstance());%0A        System.setSecurityManager(ScriptSecurityManager.getInstance());%0A        %0A        BotConfig config = loadConfig();%0A        AppContext.INSTANCE.add(config);%0A%0A        Program p = new Program();%0A        p.startup();
+    public static void main(String%5B%5D args) throws Exception %7B%0A    %09setupLogging();%0A    %09%0A        //sandbox this ...%0A        Policy.setPolicy(ScriptSecurityPolicy.getInstance());%0A        System.setSecurityManager(ScriptSecurityManager.getInstance());%0A        %0A        BotConfig config = loadConfig();%0A        AppContext.INSTANCE.add(config);%0A%0A        Program p = new Program();%0A        p.startup();%0A    %7D%0A    %0A    private static void setupLogging()%7B%0A    %09Filter filter = new Filter() %7B%0A    %09%09private final String packageName = getClass().getPackage().getName();%0A    %09%09%0A%09%09%09@Override%0A%09%09%09public boolean isLoggable(LogRecord record) %7B%0A%09%09%09%09//only log messages from this app%0A%09%09%09%09String name = record.getLoggerName();%0A%09%09%09%09return (name == null) ? false : name.startsWith(packageName);%0A%09%09%09%7D%0A%09%09%7D;%0A%09%09%0A    %09Logger global = Logger.getLogger(%22%22);%0A        for (Handler handler : global.getHandlers())%7B%0A        %09handler.setFilter(filter);%0A        %7D
 %0A   

Use a logging Filter to only show log messages from our app.