title: Injecting Log4J loggers with Spring
body: <p>I have a spring 2.5 webapp with the following web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  version="2.4"&gt;
  
  &lt;display-name&gt;Spring BlazeDS Integration Samples&lt;/display-name&gt;
  
  
  &lt;context-param&gt;
      &lt;param-name&gt;webAppRootKey&lt;/param-name&gt;
      &lt;param-value&gt;ServerBlaze&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;
          /WEB-INF/spring/*-context.xml
      &lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;log4jConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/config/log4j.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.util.Log4jConfigListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;flex.messaging.HttpFlexSession&lt;/listener-class&gt;
  &lt;/listener&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;serverBlaze&lt;/servlet-name&gt;
      &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  
  
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;serverBlaze&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/messagebroker/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;welcome-file-list&gt;
      &lt;welcome-file&gt;index.html&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>and I declared this bean</p>
  
  <pre><code>    &lt;bean id="mylog"
      class="org.springframework.beans.factory.config.CommonsLogFactoryBean"&gt;
      &lt;property name="logName" value="mylog" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>inside services-context.xml (it's a blazeds/spring project).</p>
  
  <p>I inject it this way into UserDAO bean :</p>
  
  <pre><code>    &lt;bean id="user" class="com.acotel.msp.database.UserDAO" &gt;
      &lt;property name="mylog" ref="mylog" /&gt;
      &lt;property name="jsonClient" ref="jsonClient" /&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>This is log4j.xml config file :</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="FILE" class="org.apache.log4j.DailyRollingFileAppender"&gt;
          &lt;param name="file" value="${catalina.home}\\logs\\serverBlaze.log" /&gt;
          &lt;param name="datePattern" value="'.'yyyy-MM" /&gt;
          &lt;param name="append" value="true" /&gt;
          &lt;layout class="org.apache.log4j.PatternLayout"&gt;
              ...cut...
          &lt;/layout&gt;
      &lt;/appender&gt;
  
      &lt;appender name="ROOT" class="org.apache.log4j.DailyRollingFileAppender"&gt;
          &lt;param name="file" value="${catalina.home}\\logs\\serverBlazeRoot.log" /&gt;
          &lt;param name="datePattern" value="'.'yyyy-MM" /&gt;
          &lt;param name="append" value="true" /&gt;
          &lt;layout class="org.apache.log4j.PatternLayout"&gt;
              &lt;param name="ConversionPattern" value="%d [%t] %-5p %C{6} (%F:%L) - %m%n" /&gt;
          &lt;/layout&gt;
      &lt;/appender&gt;
  
      &lt;logger name="com.bla.database"&gt;
          &lt;level value="info" /&gt;
          &lt;appender-ref ref="FILE" /&gt;
      &lt;/logger&gt;
  
      &lt;root&gt;
          &lt;priority value="info" /&gt;
          &lt;appender-ref ref="ROOT" /&gt;
      &lt;/root&gt;
  &lt;/log4j:configuration&gt;
  </code></pre>
  
  <p>In my class I have this :</p>
  
  <pre><code>package com.bla.database;
  
  import java.util.ArrayList;
  
  import json.Client;
  
  import org.apache.commons.logging.Log;
  import org.apache.log4j.Logger;
  import org.apache.log4j.PropertyConfigurator;
  
  import com.bla.MessageReceiver;
  import com.bla.PropertiesManager;
  import com.bla.interfaces.Users;
  import com.bla.vo.User;
  
  public class UserDAO {
  
  private Log mylog;
  
  private Client jsonClient;
  
  public User getUser(String username, String password) {
  
      User result = null;
  
      try {
  
      Users users = jsonClient.openProxy("userDAO", Users.class);
      result = users.getUser(username, password);
      mylog.info("Esito invio bean ["+result+"]");    
      } catch (Exception e) {
          // TODO: handle exception
          e.printStackTrace();
      }
      return result;
  }}
  </code></pre>
  
  <p>I hoped that logging in class UserDAO would log to the FILE appender, but It does not. The file is created but is empty. ROOT appender works correctly.
  What am I doing wrong?</p>