Monday, January 20, 2014

Configuring log4j with Spring framework


  • Make sure that the commons-logging is there in the classpath of the projet. Including spring-core will automatically include commins-logging with it. I tried for spring-core version 3.1.1.RELEASE
  • First, define the log4j.xml with the desired log4j configuration for the project. Example is given below. Place the file along with log4j.dtd in the resources/config folder of the  project.

 <?xml version="1.0" encoding="UTF-8" ?>  
 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">  
 <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>  
      <!-- console logger -->  
      <appender name="stdout" class="org.apache.log4j.ConsoleAppender">  
           <layout class="org.apache.log4j.PatternLayout">  
                <param name="ConversionPattern"  
                     value="%-5p [%d{EEE MMM d HH:mm:ss z yyyy:}] [%t] [%X{ProcessId}] [%L] %c : %m%n" />  
           </layout>  
           <filter class="org.apache.log4j.varia.LevelRangeFilter">  
                <param name="LevelMin" value="DEBUG" />  
           </filter>  
      </appender>  
     <!-- Specify the path for the logFile -->  
      <appender name="FILE" class="org.apache.log4j.RollingFileAppender">  
           <param name="File" value="<path of the log file>" />  
           <param name="Threshold" value="DEBUG" />  
           <param name="MaxFileSize" value="10MB" />  
           <param name="MaxBackupIndex" value="10" />  
           <layout class="org.apache.log4j.PatternLayout">  
                <param name="ConversionPattern" value="%d{ISO8601}: [%t] %c, %p, %X{ProcessId}: %m%n" />  
           </layout>  
      </appender>  
      <!-- Place your application's package whose classes needs to be logger enabler at Debug level. -->  
      <logger name="<Your application package>" additivity="false">  
           <level value="DEBUG" />  
           <appender-ref ref="stdout" />  
           <appender-ref ref="FILE" />  
      </logger>  
      <logger name="org.springframework" additivity="false">  
           <level value="INFO" />  
           <appender-ref ref="stdout" />  
           <appender-ref ref="FILE" />  
      </logger>  
      <logger name="org.hibernate" additivity="false">  
           <level value="INFO" />  
           <appender-ref ref="stdout" />  
           <appender-ref ref="FILE" />  
      </logger>  
      <root>  
           <priority value="DEBUG" />  
           <appender-ref ref="stdout" />  
           <appender-ref ref="FILE" />  
      </root>  
 </log4j:configuration>  
  • In the spring configuration, define a bean of type 'org.springframework.beans.factory.config.MethodInvokingFactoryBean' like this:
 
   <bean id="log4jInitialization"  
           class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">  
           <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />  
           <property name="targetMethod" value="initLogging" />  
           <property name="arguments">  
                <list>  
                     <value>classpath:config/log4j.xml</value>  
                </list>  
           </property>  
      </bean>  
          The above bean is for the class org.springframework.beans.factory.config.MethodInvokingFactoryBean, which takes in 3 arguments. First the targetClass, which in this case is Spring's Log4jConfigurer class and the second argument will be the method "initLogging" of class Log4jConfigurer, which will be called with the third arguments passed in as the third argument.
We will pass the location of the log4j.xml.

During runtime, MethodInvokingFactoryBean will invoke the method "initLogging" of class Log4jConfigurer with the arguments passes to it through property "arguments"

  • Class in which the logging should be used should have a declaration like this.   org.apache.log4j.Logger logger = org.apache.log4j.Logger.getLogger(clazz);
  • Level of logging can be set in the log4j.xml.

No comments:

Post a Comment