title: log4j: Issue with MDC configuration
body: <p>I am trying to add the username for each log. I found that MDC is the appropriate solution to do that and I followed <a href="http://veerasundar.com/blog/2009/11/log4j-mdc-mapped-diagnostic-context-example-code/" rel="nofollow">that tutorial</a>. </p>
  
  <p>Here is a log snippet, for testing purpose; I print the word "TEST" instead of the username :</p>
  
  <pre><code>2013-09-30 11:24:03,087 INFO  company.filter.AuthenticationFilter - TEST - AuthenticationFilter is called
  2013-09-30 11:24:03,089 INFO  company.filter.AuthenticationFilter - TEST - userName is removed
  2013-09-30 11:24:03,089 INFO  company.filter.AuthenticationFilter - TEST - AuthenticationFilter is called
  2013-09-30 11:24:03,089 INFO  company.filter.AuthenticationFilter - TEST - userName is removed
  2013-09-30 11:24:03,093 INFO  company.interceptors.AuthentificationInterceptor -  - Interceptor, actionName : Company_getTab redirect to Login_timeOut : false
  2013-09-30 11:24:03,094 INFO  company.interceptors.AuthentificationInterceptor -  - Interceptor, actionName : Company_getTop redirect to Login_timeOut : false
  2013-09-30 11:24:03,095 INFO  company.interceptors.AuthentificationInterceptor -  - Interceptor, actionName : Company_getBottom redirect to Login_timeOut : false
  2013-09-30 11:24:03,124 INFO  company.actions.CompanyAction -  - sample log
  </code></pre>
  
  <p>As you can see, it seems to work only in <code>AuthenticationFilter</code>, but not in the Struts2 interceptors or actions.  I implemented <code>AuthenticationFilter</code> this way:</p>
  
  <pre><code>public class AuthenticationFilter implements Filter {
    private final Logger logger = Logger.getLogger(AuthenticationFilter.class);
  
    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
          FilterChain chain) throws IOException, ServletException {
  
      try {
          MDC.put("userName", "TEST");
          logger.info("AuthenticationFilter is called");
          chain.doFilter(request, response);
      } finally {
          logger.info("userName is removed.");
          MDC.remove("userName");
      }
    }
  
    @Override
    public void destroy() {}
  
    @Override
    public void init(FilterConfig arg0) throws ServletException {}
  }
  </code></pre>
  
  <p>I added this to the file <code>web.xml</code>:</p>
  
  <pre><code>    &lt;filter&gt;
          &lt;filter-name&gt;AuthFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;company.filter.AuthenticationFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;AuthFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>And this this the pattern that I use in <code>log4j.xml</code></p>
  
  <pre><code>&lt;layout class="org.apache.log4j.PatternLayout"&gt;
              &lt;param name="ConversionPattern" value="%d %-5p %c - %X{userName} - %m%n"/&gt;
  &lt;/layout&gt;
  </code></pre>
  
  <p>I guess that there is something that I don’t understand about MDC. I thought that MDC would be called for each request so it can set the map which is <code>ThreadLocal</code> and each request has it's own thread (I might not understand <code>ThreadLocal</code> properly as well.). </p>
  
  <p>What should I look for/modify so I can use MDC in my configuration?</p>
  
  <p>EDIT:</p>
  
  <p>I've added a log so I can see when the data from the map is removed. Their is nothing in the map before the first action/interceptor log. I expected to see something like this:</p>
  
  <ul>
  <li>AuthenticationFilter - TEST - </li>
  <li>Interceptor log </li>
  <li>AuthenticationFilter userName removed</li>
  </ul>