title: Log4j write spring-security username
body: <p>I am using log4j and spring-security in application, and the logger should write logs with username in every message. I am very new to all of this, can somebody help with advice or links? Maybe there is some standart ways of solving this?
  Thank you.</p>
  
  <p>EDIT
  Using spring framework 3.1
  My spring-security.xml is:</p>
  
  <pre><code>&lt;beans:beans xmlns="http://www.springframework.org/schema/security"
  xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/security 
      http://www.springframework.org/schema/security/spring-security-3.1.xsd"&gt;
  
      &lt;http pattern="/favicon.ico" security="none" /&gt;
      &lt;http auto-config="true"&gt;
              &lt;intercept-url pattern="/**" access="ROLE_ADMIN"/&gt;
      &lt;/http&gt;
  
  &lt;authentication-manager&gt;
      &lt;authentication-provider&gt;
          &lt;user-service&gt;
              &lt;user name="detect_me" password="1111" authorities="ROLE_ADMIN" /&gt;
          &lt;/user-service&gt;
      &lt;/authentication-provider&gt;
  &lt;/authentication-manager&gt;
  </code></pre>
  
  <p>And log4j.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
  &lt;!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"&gt;
  &lt;log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'&gt;    
      &lt;appender name="stdout" class="org.apache.log4j.ConsoleAppender"&gt;
          &lt;layout class="org.apache.log4j.PatternLayout"&gt;
              &lt;param name="ConversionPattern" value="%d %p [%c] - %m%n" /&gt;
          &lt;/layout&gt;
      &lt;/appender&gt;
      &lt;appender name="R" class="org.apache.log4j.DailyRollingFileAppender"&gt;
          &lt;param name="File" value="/logs/urlbuilderweb.log"/&gt;
          &lt;param name="DatePattern" value="'.'yyyy-MM-dd'.log'" /&gt;
          &lt;layout class="org.apache.log4j.PatternLayout"&gt;
              &lt;param name="ConversionPattern" value="%d %p %t %c - %m%n" /&gt;            
          &lt;/layout&gt;
      &lt;/appender&gt;
      &lt;appender name="ASYNC" class="org.apache.log4j.AsyncAppender"&gt;
          &lt;param name="BufferSize" value="10000"/&gt;
          &lt;appender-ref ref="R"/&gt;
      &lt;/appender&gt;
      &lt;logger name="org.springframework"&gt;
          &lt;level value="WARN"/&gt;
      &lt;/logger&gt;
      &lt;logger name="org.directwebremoting"&gt;
          &lt;level value="WARN"/&gt;
      &lt;/logger&gt;
      &lt;logger name="org.apache.http"&gt;
          &lt;level value="WARN"/&gt;
      &lt;/logger&gt;
      &lt;logger name="org.hibernate"&gt;
          &lt;level value="WARN"/&gt;
      &lt;/logger&gt;
      &lt;root&gt;
          &lt;level value="INFO" /&gt;
          &lt;appender-ref ref="ASYNC"/&gt;
      &lt;/root&gt;
  &lt;/log4j:configuration&gt;
  </code></pre>