Log4j的XML文件的配置详解

今天关注了一下log4j.xml,原来一直使用log4j.properties,发现它比properties功能强大,可以配置输出多个log文件。

转一个基本的配置:

 

log4j 有两种配置方法,大家熟知的是properties文件但是最近的项目实施中,每次去用户那里装系统,都要苦恼于log文件放在不同位置,要改property文件就要重打jar包,麻烦的紧。而如果采用xml配置的方法,直接放在WEB-INFO下,要修改路径,很方便。查了些资料,终于把系统的log4j改成在xml中配置啦。


附:log4j的API http://logging.apache.org/log4j/docs/api/index.html


log4i.xml 文件


<?xml version="1.0" encoding="GB2312" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
 <appender name="FILE"
  class="org.apache.log4j.RollingFileAppender">


  <!-- 设置通道file和输出方式:org.apache.log4j.RollingFileAppender -->


  <param name="File" value="D:/zhaotj/all.output.log" /><!-- 设置File参数:日志输出文件名 -->
  <param name="Append" value="true" /><!-- 设置是否在重新启动服务时,在原有日志的基础添加新日志 -->
  <param name="MaxBackupIndex" value="10" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%p (%c:%L)- %m%n" /><!-- 设置输出文件项目和格式 -->
  </layout>
 </appender>

 <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">


 <!-- 设置监视器输出方式 -->


  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%-4r [%t] %-5p %c %x - %m%n" />
  </layout>


  <!--滤镜设置输出的级别-->
  <filter class="org.apache.log4j.varia.LevelRangeFilter">
   <param name="levelMin" value="info" />
   <param name="levelMax" value="info" />
   <param name="AcceptOnMatch" value="true" />
  </filter>
 </appender>

 <root><!-- 设置接收所有输出的通道 -->
  <priority value="info" />
  <appender-ref ref="FILE" /><!-- 与前面的通道id相对应 -->
  <appender-ref ref="STDOUT" />
 </root>

</log4j:configuration>


建好xml文件后 要写一个servlet类继承actionservlet,当工程初始化时自动加载xml配置文件


package com.asiainfo;
import javax.servlet.ServletException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.xml.DOMConfigurator;
import org.apache.struts.action.ActionServlet;

public class ExtendedActionServlet extends ActionServlet {
        private Log log = LogFactory.getLog(this.getClass().getName());

        public ExtendedActionServlet() {}

        public void init() throws ServletException {
            log.info(
                    "Initializing, My MyActionServlet init this System's Const Variable");
            String prefix = this.getServletConfig().getServletContext().getRealPath(
                    "/");//读取项目的路径
            String file = this.getServletConfig().getInitParameter("log4j");
                       //读取log4j相对路径
            String filePath = prefix + file;
            DOMConfigurator.configure(filePath);//加载.xml文件      
            log.info("Initializing, end My Init");
            super.init();//应用了struts,此方法不能省,ActionServlet覆盖了的此方法中有很多重要操作
        }
}


我们可以看到 在此类中 用了相对路径来加载xml的方法,首先通过prefix 读取了项目的路径然后再通过读取web.xml中的log4j变量,获得log4j.xml的相对路径 两者结合就是他的绝对路径拉
最后在web.xml中配置action信息 就可以实现加载啦


web.xml
      <servlet>
  <servlet-name>action</servlet-name>
  <servlet-class>
   com.asiainfo.ExtendedActionServlet
  </servlet-class>
  <init-param>
   <param-name>config</param-name>
   <param-value>/WEB-INF/struts-config.xml</param-value>
  </init-param>

  <!-- tsExtend  -->
  <init-param>
   <param-name>config/tsextend</param-name><!--设备检测子模块-->
   <param-value>
    /WEB-INF/tsextend/struts-config.xml
   </param-value>
  </init-param>
                <init-param>
   <param-name>log4j</param-name><!--log4j.xml的路径-->
   <param-value>
    /WEB-INF/log4j.xml
   </param-value>
  </init-param>
  <init-param>
   <param-name>info</param-name>
   <param-value>3</param-value>
  </init-param>
  <init-param>
   <param-name>detail</param-name>
   <param-value>3</param-value>
  </init-param>
  <load-on-startup>0</load-on-startup><!--设置当工程初始时便执行-->
 </servlet>

 <servlet-mapping>
  <servlet-name>action</servlet-name>
  <url-pattern>*.do</url-pattern>
 </servlet-mapping>

 

依据各个级别的日志输出到不同文件

 

log4j支持这个功能,不过不能再使用Properties配置,必须使用XML


建一个log4j.xml
<?xml   version= "1.0 "   encoding= "UTF-8 "?>
<!DOCTYPE   log4j:configuration   SYSTEM   "log4j.dtd ">
<log4j:configuration   xmlns:log4j= "http://jakarta.apache.org/log4j/ ">
<appender   name= "STDOUT "   class= "org.apache.log4j.ConsoleAppender ">
<layout   class= "org.apache.log4j.PatternLayout "/>
</appender>
<appender   name= "DEBUG "   class= "org.apache.log4j.RollingFileAppender ">
<param   name= "File "   value= "debug.log "/>
<param   name= "Append "   value= "true "/>
<param   name= "MaxFileSize "   value= "500KB "/>
<param   name= "MaxBackupIndex "   value= "2 "/>
<layout   class= "org.apache.log4j.PatternLayout "/>
<filter   class= "org.apache.log4j.varia.LevelRangeFilter ">
<param   name= "LevelMax "   value= "DEBUG "   />
<param   name= "LevelMin "   value= "DEBUG "   />
</filter>
</appender>
<appender   name= "INFO "   class= "org.apache.log4j.RollingFileAppender ">
<param   name= "File "   value= "info.log "/>
<param   name= "Append "   value= "true "/>
<param   name= "MaxFileSize "   value= "500KB "/>
<param   name= "MaxBackupIndex "   value= "2 "/>
<layout   class= "org.apache.log4j.PatternLayout "/>
<filter   class= "org.apache.log4j.varia.LevelRangeFilter ">
<param   name= "LevelMax "   value= "INFO "   />
<param   name= "LevelMin "   value= "INFO "   />
</filter>
</appender>
<root>
<appender-ref   ref= "STDOUT "/>
<appender-ref   ref= "DEBUG "/>
<appender-ref   ref= "INFO "/>
</root>
</log4j:configuration>

代码中DOMConfigurator.configure( "log4j.xml ");

这样就可以在log.debug和log.info时分别打印到不同文件中,如果你还需要更多的文件,可以复制多几次appender就可以了

你可能感兴趣的:(xml,log4j,properties,File,Class,encoding)