Java - configure custom loggers for use
<p>Trying to use <code>java.util.logging</code> and failing.</p>

<p>In an attempt to make use of <a href="https://stackoverflow.com/a/8249319/3322533">https://stackoverflow.com/a/8249319/3322533</a> :</p>

<pre><code>handlers = mypackage.logging.RequestFileHandler, mypackage.logging.MainFileHandler
config   =

mainLogger.handlers       = mypackage.logging.MainFileHandler

requestLogger.handlers    = mypackage.logging.RequestFileHandler

java.util.logging.ConsoleHandler.level     = INFO
java.util.logging.ConsoleHandler.filter    =
java.util.logging.ConsoleHandler.formatter = mypackage.logging.VerySimpleFormatter
java.util.logging.ConsoleHandler.encoding  =

mypackage.RequestFileHandler.level     = SEVERE
mypackage.RequestFileHandler.filter    =
mypackage.RequestFileHandler.formatter = mypackage.logging.VerySimpleFormatter
mypackage.RequestFileHandler.encoding  =
mypackage.RequestFileHandler.limit     =
mypackage.RequestFileHandler.count     =
mypackage.RequestFileHandler.append    = false
mypackage.RequestFileHandler.pattern   = REQUESTS.%u.%g.log

mypackage.MainFileHandler.level     = INFO
mypackage.MainFileHandler.filter    =
mypackage.MainFileHandler.formatter = mypackage.logging.VerySimpleFormatter
mypackage.MainFileHandler.encoding  =
mypackage.MainFileHandler.limit     =
mypackage.MainFileHandler.count     =
mypackage.MainFileHandler.append    = false
mypackage.MainFileHandler.pattern   = MAIN.%u.%g.log
</code></pre>

<p>where</p>

<pre><code>public class MainFileHandler extends FileHandler {
    public MainFileHandler() throws IOException, SecurityException {
        super();
    }
}
</code></pre>

<p>and </p>

<pre><code>public class RequestFileHandler extends FileHandler {
    public RequestFileHandler() throws IOException, SecurityException {
        super();
    }
}
</code></pre>

<p>Intention: provide two loggers accessible through </p>

<pre><code>Logger.getLogger("mainLogger");
</code></pre>

<p>or</p>

<pre><code>Logger.getLogger("requestLogger");
</code></pre>

<p>respectively, one that will write (exclusively) to <code>MAIN[...].log</code> and the other to <code>REQUESTS[...].log</code></p>

<p>(No limits on the amount of messages that can be logged to either file and if necessary, use logging level to filter out unwanted msgs to either.)</p>

<p>However, neither file is created when I (for example)</p>

<pre><code>public static final Logger log = Logger.getLogger("mainLogger");
</code></pre>

<p>and then</p>

<pre><code>public void configureLogger(){
    try {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        InputStream config = classLoader.getResourceAsStream("logging.properties");

        LogManager.getLogManager().readConfiguration(config);

    }catch(Exception ex){
       throw new RuntimeException("logging properties failed");
    }
}
</code></pre>

<p>before I</p>

<pre><code>log.info("Hello World!")
</code></pre>

<p>I know the properties are loaded because when I include <code>java.util.logging.ConsoleHandler</code> in the <code>handlers = ...</code> list and use the global logger, instead, the formatter is applied for the console output.</p>

<p>So ... I guess my attempt at setting up the file loggers is faulty. How do I get this working?</p>

<p><strong>EDIT</strong></p>

<p>So I removed the <code>[...].pattern = [...]</code> lines and instead hardcoded the file names:</p>

<pre><code>public class MainFileHandler extends FileHandler implements FileHandlerProperties {
    public MainFileHandler() throws IOException, SecurityException {
        super("MAIN_" + new SimpleDateFormat(TIME_PATTERN).format(new Date()) + ".log");
    }
}
</code></pre>

<p>and</p>

<pre><code>public class RequestFileHandler extends FileHandler implements FileHandlerProperties {
    public RequestFileHandler() throws IOException, SecurityException {
        super("REQUESTS_" + new SimpleDateFormat(TIME_PATTERN).format(new Date()) + ".log");
    }
}
</code></pre>

<p>where</p>

<pre><code>public interface FileHandlerProperties {
    static final String TIME_PATTERN = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX";
}
</code></pre>

<p>Both files now get created BUT they <strong>both</strong> contain exactly the same (despite their different level settings and loggers) AND what they contain is in xml:</p>

<pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
&lt;!DOCTYPE log SYSTEM "logger.dtd"&gt;
&lt;log&gt;
&lt;record&gt;
  &lt;date&gt;2016-10-10T18:49:23&lt;/date&gt;
  &lt;millis&gt;1476118163654&lt;/millis&gt;
  &lt;sequence&gt;0&lt;/sequence&gt;
  &lt;logger&gt;mainLogger&lt;/logger&gt;
  &lt;level&gt;INFO&lt;/level&gt;
  &lt;class&gt;mypackage.main.Main&lt;/class&gt;
  &lt;method&gt;&amp;lt;init&amp;gt;&lt;/method&gt;
  &lt;thread&gt;1&lt;/thread&gt;
  &lt;message&gt;Hello World&lt;/message&gt;
&lt;/record&gt;
&lt;/log&gt;
</code></pre>

<p>Please help ...</p>