Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Log4J MDC approach</strong></p> <p>This is how to do it with MDC</p> <pre><code>public class MdcExample implements Runnable { private static final Logger logger = Logger.getLogger(MdcExample.class); public static void main(final String[] args) { final MdcExample threadLocalExample = new MdcExample(); final Thread thread1 = new Thread(threadLocalExample); final Thread thread2 = new Thread(threadLocalExample); final Thread thread3 = new Thread(threadLocalExample); thread1.start(); thread2.start(); thread3.start(); } @Override public void run() { while (true) { MDC.put("random", "" + Math.random()); try { logger.info("My log message, prefixed with MDC"); TimeUnit.SECONDS.sleep(1); } catch (final InterruptedException e) { } } } } </code></pre> <p>Log4J settings:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd"&gt; &lt;log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt; &lt;!-- Appenders --&gt; &lt;appender name="console" class="org.apache.log4j.ConsoleAppender"&gt; &lt;param name="Target" value="System.out" /&gt; &lt;layout class="org.apache.log4j.PatternLayout"&gt; &lt;param name="ConversionPattern" value="%-5p %X{random}:- %m%n" /&gt; &lt;/layout&gt; &lt;/appender&gt; &lt;!-- Root Logger --&gt; &lt;root&gt; &lt;priority value="info" /&gt; &lt;appender-ref ref="console" /&gt; &lt;appender-ref ref="file" /&gt; &lt;/root&gt; &lt;/log4j:configuration&gt; </code></pre> <p><strong>Thread Local approach (if your Businesslogic requires it)</strong></p> <p>If you like to store values (like users) locally to a thread, then use <code>ThreadLocal</code>:</p> <pre><code>import java.util.concurrent.TimeUnit; public class ThreadLocalExample implements Runnable { ThreadLocal&lt;Double&gt; myRandom = new ThreadLocal&lt;&gt;(); public static void main(final String[] args) { final ThreadLocalExample threadLocalExample = new ThreadLocalExample(); final Thread thread1 = new Thread(threadLocalExample); final Thread thread2 = new Thread(threadLocalExample); final Thread thread3 = new Thread(threadLocalExample); thread1.start(); thread2.start(); thread3.start(); } @Override public void run() { this.myRandom.set(Math.random()); while (true) { System.out.println(Thread.currentThread().getId() + " " + this.myRandom.get()); try { TimeUnit.SECONDS.sleep(1); } catch (final InterruptedException e) { } } } } </code></pre> <p><a href="http://docs.oracle.com/javase/6/docs/api/java/lang/ThreadLocal.html" rel="nofollow">http://docs.oracle.com/javase/6/docs/api/java/lang/ThreadLocal.html</a></p> <p><strong>Adding the Thread ID to log4j output</strong></p> <p>Regarding your log4j question, you can use th <code>t</code>-Placeholder for the Thread name:</p> <p>This is an example Appender you can use:</p> <pre><code>&lt;appender name="console" class="org.apache.log4j.ConsoleAppender"&gt; &lt;param name="Target" value="System.out" /&gt; &lt;layout class="org.apache.log4j.PatternLayout"&gt; &lt;param name="ConversionPattern" value="%5p %d{ISO8601} [%t][%x] %c - %m%n" /&gt; &lt;/layout&gt; &lt;/appender&gt; </code></pre> <p><a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html" rel="nofollow">http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html</a></p>
    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. 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