1. 程式人生 > >log4j logback slf4j配置

log4j logback slf4j配置

一. Log4j+commons-logging
JAR包
commons-logging-1.1.jar
log4j-1.2.15.jar
配置檔案
commons-logging.properties
log4j.xml
commons-logging.jar包讀取commons-logging.properties中指定的log,自動載入日誌配置
在commons-logging+log4j組合中commons-logging.properties指定的日誌為:
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger
log4j指定具體日誌輸出配置
如果未選用commons-logging,可以在啟動專案中,主動載入log4j的配置檔案
二. Logback+slf4j
JAR包
slf4j-api-1.5.2.jar
logback-classic-0.9.9.jar
logback-core-0.9.9.jar
配置檔案
Logback.xml
logback的載入採用編碼載入
String logbackCfg = "/config/logback.xml";
URL logURL = new ClassPathResource(logbackCfg).getURL();
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
LoggerContext loggerContext = (LoggerContext) loggerFactory;
loggerContext.shutdownAndReset();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(logURL);

三. Slf4j+log4j
JAR包
slf4j-api-1.5.2.jar
slf4j-log4j12-1.5.2.jar
log4j-1.2.15.jar
配置檔案
Logback.xml
四. Slf4j+logback+logback
注:這種情況下是專案中有log4j輸出的日誌以及logback資料的日誌.老專案改造,log4j和logback共存的情況下.(當選用spring+slf4j+logback的時候,spring的日誌輸出是log4j的,這個時候採用這種配置.)
JARbao
log4j-over-slf4j-1.5.2.jar
logback-classic-0.9.9.jar
logback-core-0.9.9.jar
slf4j-api-1.5.2.jar
配置檔案
Logback.xml
配置載入
String logbackCfg = "/config/logback.xml";
URL logURL = new ClassPathResource(logbackCfg).getURL();
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
LoggerContext loggerContext = (LoggerContext) loggerFactory;
loggerContext.shutdownAndReset();
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(loggerContext);
configurator.doConfigure(logURL);

五. Log4j配置檔案示例
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

 <!-- 控制檯輸出-->
 <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
  <param name="Target" value="System.out" />
  <param name="Threshold" value="INFO" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%d{yyyy-MM-dd HH:mm:ss sss}[%-p][%-c][line:%-L] \r\n  %m%n" />
  </layout>

<!— 以下是過濾一些不需要輸出的log資訊 -->
  <filter class="org.apache.log4j.varia.StringMatchFilter">
   <param name="StringToMatch" value="new a seq no spend" />
   <param name="AcceptOnMatch" value="false" />
  </filter>
  <filter class="org.apache.log4j.varia.StringMatchFilter">
   <param name="StringToMatch" value="new hi[type=" />
   <param name="AcceptOnMatch" value="false" />
  </filter>
  <filter class="org.apache.log4j.varia.StringMatchFilter">
            <param name="StringToMatch" value="FileTransfer.execCommand()" />
            <param name="AcceptOnMatch" value="false" />
        </filter>
 </appender>

 <!--檔案輸出 按照檔案大小滾動檔案-->
 <appender name="xxxx_file" class="org.apache.log4j.RollingFileAppender">
  <param name="Append" value="true" />
  <param name="Threshold" value="INFO" />
  <param name="File" value="xxxx.log" />
  <param name="MaxFileSize" value="100MB" />
  <param name="MaxBackupIndex" value="15" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%d{yyyy-MM-dd HH:mm:ss sss}[%-p][%-c][line:%-L] \r\n  %m%n" />
  </layout>
  <filter class="org.apache.log4j.varia.StringMatchFilter">
   <param name="StringToMatch" value="new a seq no spend" />
   <param name="AcceptOnMatch" value="false" />
  </filter>
  <filter class="org.apache.log4j.varia.StringMatchFilter">
   <param name="StringToMatch" value="new hi[type=" />
   <param name="AcceptOnMatch" value="false" />
  </filter>
  <filter class="org.apache.log4j.varia.StringMatchFilter">
            <param name="StringToMatch" value="FileTransfer.execCommand()" />
            <param name="AcceptOnMatch" value="false" />
        </filter>
 </appender>
 
 <!-- 錯誤輸出 過濾出錯誤日誌資訊-->
 <appender name="xxxx_error" class="org.apache.log4j.RollingFileAppender">
  <param name="Append" value="true" />
  <param name="Threshold" value="ERROR" />
  <param name="File" value="xxxx-error.log" />
  <param name="MaxFileSize" value="100MB" />
  <param name="MaxBackupIndex" value="4" />
  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern"
    value="%d{yyyy-MM-dd HH:mm:ss sss}[%-p][%-c][line:%-L] \r\n  %m%n" />
  </layout>
 </appender>
   
  <!-- 根loggr -->
 <root>
  <appender-ref ref="CONSOLE" />
  <appender-ref ref="xxxx_file" />
  <appender-ref ref="xxxx_file_error" />
 </root>
</log4j:configuration>

六. Logback配置檔案示例
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration>

<configuration>
    <jmxConfigurator />
    
    <!-- 控制檯輸出日誌 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </layout>
    </appender>
    
    <!-- 檔案輸出日誌 (檔案大小策略進行檔案輸出,超過指定大小對檔案備份)-->
    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <File>xxxx.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>xxxx.log.%i.bak</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>12</MaxIndex>
        </rollingPolicy>

        <triggeringPolicy
            class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MB</MaxFileSize>
        </triggeringPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </layout>
    </appender>
  
   <appender name="FILE-ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
         <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <OnMismatch>DENY</OnMismatch>
            <OnMatch>ACCEPT</OnMatch>
        </filter>
        
        <File>xxxx-err.log</File>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <FileNamePattern>xxxx-err.log.%i.bak</FileNamePattern>
            <MinIndex>1</MinIndex>
            <MaxIndex>3</MaxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <MaxFileSize>100MB</MaxFileSize>
        </triggeringPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
        </layout>
    </appender>

  <!--這裡指定logger name 是為jmx設定日誌級別做鋪墊 -->      
    <logger name="com.xxx.xxx">
        <level value="INFO" />
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
        <appender-ref ref="FILE-ERROR" />
    </logger>



</configuration>


七. 相關簡單說明
Log4j
Log4j是Apache的一個開放原始碼專案,通過使用Log4j,我們可以控制日誌資訊輸送的目的地是控制檯、檔案、GUI元件、甚至是套介面服務 器、NT的事件記錄器、UNIX Syslog守護程序等;使用者也可以控制每一條日誌的輸出格式;通過定義每一條日誌資訊的級別,使用者能夠更加細緻地控制日誌的生成過程。這些可以通過一個配置檔案來靈活地進行配置,而不需要修改程式程式碼

Slf4j
簡單日記門面(Facade)SLF4J是為各種loging APIs提供一個簡單統一的介面,從而使得終端使用者能夠在部署的時候配置自己希望的loging APIs實現。 Logging API實現既可以選擇直接實現SLF4J接的loging APIs如: NLOG4J、SimpleLogger。也可以通過SLF4J提供的API實現來開發相應的介面卡如Log4jLoggerAdapter、 JDK14LoggerAdapter。

Logback
Logback是由log4j創始人設計的又一個開源日記元件。logback當前分成三個模組:logback-core,logback- classic和logback-access。logback-core是其它兩個模組的基礎模組。logback-classic是log4j的一個改良版本。此外logback-classic完整實現SLF4J API使你可以很方便地更換成其它日記系統如log4j或JDK14 Logging。logback-access訪問模組與Servlet容器整合提供通過Http來訪問日記的功能。