Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You may use NDC (Nested Diagnostic Context) or MDC (Mapped Diagnostic Context). Below is an example calling the <code>Logger</code> on controller :</p> <pre class="lang-java prettyprint-override"><code>public static StopWatch sw = new StopWatch(); public static void request001() { sw.start(); // start timer // logic of processing the request ... sw.stop(); // stop timer // using MDC to put elapsed time in milisecond MDC.put("processTime", sw.getElapsedTime()); Logger.info("this is request001"); } </code></pre> <p>And the <code>log4j.properties</code> example file should be like follow (using console appender):</p> <pre><code>log4j.rootLogger = INFO, loggerName log4j.appender.loggerName = org.apache.log4j.ConsoleAppender log4j.appender.loggerName.layout = org.apache.log4j.PatternLayout log4j.appender.loggerName.layout.ConversionPattern = %d{dd MMM yyyy HH:mm:ss,SSS} - [%X{processTime}] - %m </code></pre> <p><code>%d{dd MMM yyyy HH:mm:ss,SSS}</code> is used to output the date while<code>%X{processTime}</code> is used to get the <code>processTime</code> value stored in MDC.</p> <p>You can check the references here :</p> <ol> <li><a href="http://logging.apache.org/log4j/1.2/manual.html" rel="nofollow noreferrer">Log4j 1.2 Manual</a></li> <li><a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html" rel="nofollow noreferrer"><code>PatternLayout</code> JavaDoc</a></li> <li><a href="https://stackoverflow.com/questions/14134734/log4j-ndc-vs-mdc-difference">What is the difference between Log4j&#39;s NDC and MDC facilities?</a></li> </ol> <hr> <h3>Update</h3> <p><strong>Threshold</strong> properties is represents the <strong>minimum</strong> Level/Priority accepted, not an absolute Level/Priority. So, your <code>JUSTINFO</code> logger should log logger that have priority starting from <code>INFO</code> or higher. </p> <p>If you want to separate the logging message to exactly match the level (absolute level range filter), I think you should consider to replace the log4j.properties to log4j.xml (configuration using xml). I give you example :</p> <pre class="lang-xml prettyprint-override"><code>&lt;!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"&gt; &lt;log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt; &lt;!-- JUSTINFO logging --&gt; &lt;appender name="JUSTINFO" class="org.apache.log4j.FileAppender"&gt; &lt;param name="File" value="./info.log" /&gt; &lt;param name="Threshold" value="INFO" /&gt; &lt;layout class="org.apache.log4j.PatternLayout"&gt; &lt;param name="ConversionPattern" value="%-5p %t - %m%n"/&gt; &lt;/layout&gt; &lt;filter class="org.apache.log4j.varia.LevelRangeFilter"&gt; &lt;param name="LevelMin" value="INFO" /&gt; &lt;param name="LevelMax" value="INFO" /&gt; &lt;/filter&gt; &lt;/appender&gt; &lt;!-- JUSTERROR logging --&gt; &lt;appender name="JUSTERROR" class="org.apache.log4j.FileAppender"&gt; &lt;param name="File" value="./error.log" /&gt; &lt;layout class="org.apache.log4j.PatternLayout"&gt; &lt;param name="ConversionPattern" value="%-5p %t - %m%n"/&gt; &lt;/layout&gt; &lt;filter class="org.apache.log4j.varia.LevelRangeFilter"&gt; &lt;param name="LevelMin" value="ERROR" /&gt; &lt;param name="LevelMax" value="ERROR" /&gt; &lt;/filter&gt; &lt;/appender&gt; &lt;!-- define root logger priority to ERROR --&gt; &lt;root&gt; &lt;level value="ERROR"/&gt; &lt;appender-ref ref="JUSTINFO"/&gt; &lt;appender-ref ref="JUSTERROR"/&gt; &lt;/root&gt; &lt;/log4j:configuration&gt; </code></pre> <p>Here also a good reference for you :</p> <ol> <li><a href="https://stackoverflow.com/questions/1256835/why-chose-xml-over-properties-files-for-log4j-configuration">Why chose XML over properties files for Log4J configuration?</a></li> <li><a href="https://stackoverflow.com/questions/4825072/how-to-set-filter-in-the-log4j-xml">How to set filter in the log4j.xml</a></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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