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

落地僧發表於2016-01-09
  實際上本文是配置與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

相關文章