springMvc+MyBatis+Maven配置和log4j相似的Logback

  实际上本文是配置与log4j相似的Logback!
pom.xml加载日志相关包:
<!-- log begin -->
<dependency>
	<groupId>com.googlecode.log4jdbc</groupId>
	<artifactId>log4jdbc</artifactId>
	<version>1.2</version>
	<scope>runtime</scope>
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-api</artifactId>
	<version>${slf4j.version}</version>
	<scope>runtime</scope>
</dependency>
<!-- 代码直接调用log4j会被桥接到slf4j -->
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>log4j-over-slf4j</artifactId>
	<version>${slf4j.version}</version>
	<scope>runtime</scope>
</dependency>
<!-- 代码直接调用common-logging会被桥接到slf4j -->
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>jcl-over-slf4j</artifactId>
	<version>${slf4j.version}</version>
	<scope>runtime</scope>
</dependency>
<!-- 代码直接调用java.util.logging会被桥接到slf4j -->
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>jul-to-slf4j</artifactId>
	<version>${slf4j.version}</version>
	<scope>runtime</scope>
</dependency>
<!-- log end -->




web.xml配置加载log4j:
<!-- login4j begin -->
<!-- 设置log4j存放Log文件位置(通过spring统一进行管理) -->
 <context-param>
  <param-name>webAppRootKey</param-name>
  <param-value>log.root</param-value>
 </context-param>


 <!-- 加载log4j的配置文件 -->
 <context-param>
  <param-name>log4jConfigLocation</param-name>
  <param-value>classpath:/logback.xml</param-value>
 </context-param>


 <!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond-->
 <context-param>
  <param-name>log4jRefreshInterval</param-name>
  <param-value>60000</param-value>
 </context-param>


 <!--Spring用于log4j初始化的监听器-->
 <listener>
  <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
 </listener>
<!-- login4j end -->



根据需求logback.xml相关log4j配置:
<?xml version="1.0" encoding="UTF-8"?>  
    <configuration info="true">  
        <!-- 应用名称 -->  
        <property name="APP_NAME" value="wanxue" />  
        <!--日志文件的保存路径,首先查找系统属性-Dlog.dir,如果存在就使用其;否则,在当前目录下创建名为logs目录做日志存放的目录 -->  
        <property name="LOG_HOME" value="D:\\wanxueLog4j" />  
        <!-- 日志输出格式 -->  
        <property name="ENCODER_PATTERN"  
           value="%d{yyyy-MM-dd  HH:mm:ss.SSS} [%thread] %-5level 类名:%logger{80} 方法名:%M 行号:%L- %msg%n" />  
        <contextName>${APP_NAME}</contextName>  
      
        <!-- 控制台日志:输出全部日志到控制台 -->  
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">  
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">  
                <Pattern>${ENCODER_PATTERN}</Pattern>  
            </encoder>  
        </appender>  
      
     
        <!-- 文件日志:输出全部日志到文件 -->  
        <appender name="FILE"  
            class="ch.qos.logback.core.rolling.RollingFileAppender">  
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
                <fileNamePattern>${LOG_HOME}/output.%d{yyyy-MM-dd}.log</fileNamePattern>
				<!-- 保存30天日志文件   -->
                <maxHistory>30</maxHistory>  
            </rollingPolicy>  
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">  
                <pattern>${ENCODER_PATTERN}</pattern>  
            </encoder>  
        </appender>  
      
        <!-- 错误日志:用于将错误日志输出到独立文件 -->  
        <appender name="ERROR_FILE"  
            class="ch.qos.logback.core.rolling.RollingFileAppender">  
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
                <fileNamePattern>${LOG_HOME}/error.%d{yyyy-MM-dd}.log</fileNamePattern>  
                <maxHistory>30</maxHistory>  
            </rollingPolicy>  
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">  
                <pattern>${ENCODER_PATTERN}</pattern>  
            </encoder>  
            <filter class="ch.qos.logback.classic.filter.ThresholdFilter">  
                <level>WARN</level>  
            </filter>  
        </appender>  
          
        <!-- 独立输出的同步日志 -->  
        <appender name="SYNC_FILE"  class="ch.qos.logback.core.rolling.RollingFileAppender">  
            <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">  
                <fileNamePattern>${LOG_HOME}/sync.%d{yyyy-MM-dd}.log</fileNamePattern>  
                <maxHistory>30</maxHistory>  
            </rollingPolicy>  
            <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">  
                <pattern>${ENCODER_PATTERN}</pattern>  
            </encoder>  
        </appender>  
          
        <logger name="log.sync" level="INFO" addtivity="true">  
            <appender-ref ref="SYNC_FILE" />  
        </logger>  
        
        <!-- 全局输出的方式 -->
        <root level="INFO">  
            <appender-ref ref="STDOUT" />  
            <appender-ref ref="FILE" />  
            <appender-ref ref="ERROR_FILE" />  
        </root>  
</configuration>


详细可参考:
日志组件logback的介绍及配置使用方法:http://blog.csdn.net/zgmzyr/article/details/8267072
logback系列之三:输出日志到文件(滚动):http://czj4451.iteye.com/blog/1975937

你可能感兴趣的:(springMvc+MyBatis+Maven配置和log4j相似的Logback)