Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's possible to use two <code>Logger</code>s in one class. </p> <p>First idea: get the two loggers with different names:</p> <pre><code>package com.mycompany.apackage.MyClass; public class MyClass { private static final logger = Logger.getLogger(Myclass.class) private static final dbLogger = Logger. getLogger(Myclass.class.getName() + ".dblogger") } </code></pre> <p>Config for the package of the <code>dbLogger</code>:</p> <pre><code>&lt;root&gt; &lt;appender-ref ref="mainlog" /&gt; &lt;/root&gt; &lt;logger name="com.mycompany.apackage.MyClass.dblogger"&gt; &lt;appender-ref ref="dbappender" /&gt; &lt;/logger&gt; </code></pre> <p>(Not tested.) In that case the <code>dbLogger</code> also logs to the <code>mainlog</code> appender. If it's not appropriate you could use a custom filter in the <code>mainlog</code> (and other) appenders which filters out the messages of the <code>dbLogger</code>. Another solution is using a completely different prefix for the <code>dbLogger</code>:</p> <pre><code> private static final logger = Logger.getLogger(Myclass.class) private static final dbLogger = Logger. getLogger("dblogger." + Myclass.class.getName()) </code></pre> <p>Log4j config:</p> <pre><code>&lt;root&gt; &lt;/root&gt; &lt;logger name="com.mycompany"&gt; &lt;appender-ref ref="mainlog" /&gt; &lt;/logger&gt; &lt;logger name="dblogger.com.mycompany"&gt; &lt;appender-ref ref="dbappender" /&gt; &lt;/logger&gt; </code></pre> <p>Note that if you pass the same parameter to the <code>getLogger()</code> method you will get same <code>Logger</code> object, so you have to use different names.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload