spring mvc中log4j的配置与使用

时间:2023-01-28 21:53:28

文章来源:http://rockelixir.iteye.com/blog/1902352


如果使用spring插件创建一个spring template project,它会默认带log4j,只要改下log4j的配置就可以使用了,如果自己创建的project,就要加载下log4f的包了,使用步骤如下:

1.pom.xml加入log4j的依赖包 

<!-- Logging -->    
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.6</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.6</version>
<scope>runtime</scope>
</dependency>

2.在src/main/resources下创建log4j.xml  

<?xml version="1.0" encoding="UTF-8"?>    <!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">  <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">    <!-- [控制台STDOUT] -->    <appender name="console" class="org.apache.log4j.ConsoleAppender">       <param name="encoding" value="GBK" />       <param name="target" value="System.out" />       <layout class="org.apache.log4j.PatternLayout">         <param name="ConversionPattern" value="%-5p %c{2} - %m%n" />       </layout>    </appender>      <!-- [公共Appender] -->    <appender name="DEFAULT-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">       <param name="File" value="${webapp.root}/logs/common-default.log" />       <param name="Append" value="true" />       <param name="encoding" value="GBK" />       <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />       <layout class="org.apache.log4j.PatternLayout">      <param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />       </layout>     </appender>       <!-- [错误日志APPENDER] -->     <appender name="ERROR-APPENDER" class="org.apache.log4j.DailyRollingFileAppender">       <param name="File" value="${webapp.root}/logs/common-error.log" />       <param name="Append" value="true" />       <param name="encoding" value="GBK" />       <param name="threshold" value="error" />       <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />       <layout class="org.apache.log4j.PatternLayout">          <param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />       </layout>     </appender>       <!-- [组件日志APPENDER] -->     <appender name="COMPONENT-APPENDER"  class="org.apache.log4j.DailyRollingFileAppender">       <param name="File" value="${webapp.root}/logs/logistics-component.log" />       <param name="Append" value="true" />       <param name="encoding" value="GBK" />       <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />       <layout class="org.apache.log4j.PatternLayout">      <param name="ConversionPattern" value="%d %-5p %c{2} - %m%n" />       </layout>     </appender>       <!-- [组件日志] -->     <logger name="LOGISTICS-COMPONENT">        <level value="${loggingLevel}" />        <appender-ref ref="COMPONENT-APPENDER" />        <appender-ref ref="ERROR-APPENDER" />     </logger>       <!-- Root Logger -->     <root>         <level value="${rootLevel}"></level>         <appender-ref ref="DEFAULT-APPENDER" />         <appender-ref ref="ERROR-APPENDER" />     </root>  </log4j:configuration>  
log4j.xml中定义了4个appender,就是log输出的位置,一个是控制台,三个是文件,但有一个文件common-error.log是专用于收集特殊错误的文件,可以定义多个分别为不同的场景使用,log4j的相关定义如下: 
(1). 输出方式appender一般有5种:  
org.apache.log4j.RollingFileAppender(滚动文件,自动记录最新日志)   
org.apache.log4j.ConsoleAppender (控制台)    
org.apache.log4j.FileAppender (文件)   
org.apache.log4j.DailyRollingFileAppender (每天产生一个日志文件)   
org.apache.log4j.WriterAppender (将日志信息以流格式发送到任意指定的地方)   
(2). 日记记录的优先级priority,优先级由高到低分为  
OFF ,FATAL ,ERROR ,WARN ,INFO ,DEBUG ,ALL。   
Log4j建议只使用FATAL ,ERROR ,WARN ,INFO ,DEBUG这五个级别。   
(3). 格式说明layout中的参数都以%开始,后面不同的参数代表不同的格式化信息(参数按字母表顺序列出):   
%c        输出所属类的全名,可在修改为 %d{Num} ,Num类名输出的维(如:"org.apache.elathen.ClassName",%C{2}将输出elathen.ClassName)   
%d       输出日志时间其格式为 %d{yyyy-MM-dd HH:mm:ss,SSS},可指定格式 如 %d{HH:mm:ss}   
%l        输出日志事件发生位置,包括类目名、发生线程,在代码中的行数   
%n       换行符   
%m      输出代码指定信息,如info(“message”),输出message   
%p       输出优先级,即 FATAL ,ERROR 等   
%r        输出从启动到显示该log信息所耗费的毫秒数   
%t        输出产生该日志事件的线程名 

3. web.xml中配置log4j 
<!-- 配置加载log4j.xml文件路径 --> 
<context-param> 
  <param-name>log4jConfigLocation</param-name> 
  <param-value>classpath:config/log4j.xml</param-value> 
</context-param> 

<!-- 配置log4j.xml变量 --> 
<context-param> 
  <param-name>rootLevel</param-name> 
  <param-value>DEBUG</param-value> 
</context-param> 

<context-param> 
  <param-name>loggingLevel</param-name> 
  <param-value>INFO</param-value> 
</context-param> 

<!-- 配置log4j.xml监听器 --> 
<listener> 
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
</listener> 

4. 类使用log4j 
(1)包引入 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
(2)日志常量定义 
/** 组件日志 */ 
private static final Logger        logger = LoggerFactory                                            .getLogger(LoggerNames.LOGISTICS_COMPONENT); 
(3)日志打印 
logger.info("日志打印");