title: Printing thread id in log file using log4j
body: <p>I am trying to print the id of the thread for which the logging is being done in my logfile. I did it at code level by <code>log.info(Thread.currentThread().getId())</code> where "log" is Logger class object but this is not what i exactly want. Actually my application is a large distributed application and it is not possible to add <code>Thread.currentThread().getId()</code> with every <code>log.info("something")</code> in the code. Is there anyway by which i can just make any change in my log4j.xml file and print threads id for every log.info in my code. This is my log4j.xml</p>
  
  <pre><code>&lt;log4j:configuration debug="true"&gt;
  &lt;!-- File Appenders --&gt;
  
  &lt;appender name="EventsAndErrorsFileAppender" class="org.apache.log4j.RollingFileAppender"&gt;
      &lt;param name="File" value="EventsAndErrors.xml" /&gt;
      &lt;param name="Datepattern" value="'.'yyyy-MM-dd-HH" /&gt;
      &lt;param name="MaxFileSize" value="1000KB" /&gt;
      &lt;param name="MaxBackupIndex" value="140" /&gt;
      &lt;layout class="org.apache.log4j.PatternLayout"&gt;
          &lt;param name="ConversionPattern" value="%d{ISO8601} %-5p [%C] %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="ERROR" /&gt;
      &lt;/filter&gt;
  &lt;/appender&gt;
  
  &lt;root&gt;
      &lt;priority value="debug" /&gt;
      &lt;appender-ref ref="EventsAndErrorsFileAppender" /&gt;
      &lt;appender-ref ref="ExceptionFileAppender" /&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p></p>
  
  <p>Now i am assuming that I can add something in my Layout in xml to print the thread.
  I am also attaching the sample code in which i am trying this just for reference</p>
  
  <pre><code>import org.apache.log4j.Logger;
  
  class MyThread extends Thread implements MyInterface
    {
     public void run()
      {
      int i = 0;
      while(i &lt; 10)
      {
          System.out.println(Thread.currentThread().getId()+"In first        thread");
          log.info(Thread.currentThread().getId());
          log.error(Thread.currentThread().getId());
          System.out.println();
          i++;
      }   
  
    }
  }
  class MyThread1 extends Thread implements MyInterface
  {
  
  public void run()
  {
      int i = 0;
      while(i &lt; 10)
      {
          System.out.println(Thread.currentThread().getId()+"In secound thread");
          log.info(Thread.currentThread().getId());
          log.debug("debug");
          System.out.println();
          i++;
      }
  }
  }
  
  public class MyClass implements MyInterface
  {
   public static void main(String[] args) {
      // TODO Auto-generated method stub
  
  
      MyThread thread1 = new MyThread();
      MyThread1 thread2 = new MyThread1();
  
  
      log.info("logging stareted");
      thread1.start();
      thread2.start();
   } 
  }
  </code></pre>
  
  <p>Any guidance would be aprreciated.
  Thanks </p>