歡迎加入DUBBO交流羣:259566260
這裏將對如何在本身的項目裏面開啓dubbo框架本身的日誌,並對輸出的日誌進行控制。在講這些以前,先看看dubbo在處理日誌的時候是怎麼作的? java
在dubbo框架內全部的日誌輸出都是經過 LoggerFactory這個靜態工廠類來得到Logger的對象實體,而且抽離了一個LoggerAdapter用於對接第三方日誌框架,因此就有了JDKLoggerAdapter,Log4jLoggerAdapter,SLF4JLoggerAdapter等一些實現子類,分別對接了不一樣Log第三方實現。既然dubbo可以支持這麼多log實現,那麼這些實如今dubbo中優先級是在呢麼樣的呢?這裏的優先級是隻未配置指定的logger提供方的狀況下,由dubbo框架本身選擇。優先級以下:第三方日誌框架 | 優先級 |
Log4j | 最高(默認就用這個) |
SLF4J | 次高(上面沒有采用這個) |
Common Logging(jcl就是common logging) | 次低(Log4j和SLF4J在項目中均沒有就用這個) |
JDK log | 最低(最後的選擇) |
上面說的有和沒有是指你的項目classpath下面有沒有對應的jar包,若是有則表示支持對應的日誌實現。下面粘貼出Dubbo選擇日誌提供方的代碼: spring
// 查找經常使用的日誌框架 static { String logger = System.getProperty("dubbo.application.logger"); if ("slf4j".equals(logger)) { setLoggerAdapter(new Slf4jLoggerAdapter()); } else if ("jcl".equals(logger)) { setLoggerAdapter(new JclLoggerAdapter()); } else if ("log4j".equals(logger)) { setLoggerAdapter(new Log4jLoggerAdapter()); } else if ("jdk".equals(logger)) { setLoggerAdapter(new JdkLoggerAdapter()); } else { try { setLoggerAdapter(new Log4jLoggerAdapter()); } catch (Throwable e1) { try { setLoggerAdapter(new Slf4jLoggerAdapter()); } catch (Throwable e2) { try { setLoggerAdapter(new JclLoggerAdapter()); } catch (Throwable e3) { setLoggerAdapter(new JdkLoggerAdapter()); } } } } }
<appender name="dubboAppender" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="E:/dubbo.log" /> <param name="DatePattern" value="'.'yyyy-MM-dd'.log'" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{MMdd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" /> </layout> </appender> <logger name="com.alibaba.dubbo" additivity="false"> <priority value ="info"/> <appender-ref ref="dubboAppender" /> </logger>
public class CustomContextLoaderListener extends ContextLoaderListener { static{ //設置dubbo使用slf4j來記錄日誌 System.setProperty("dubbo.application.logger","slf4j"); } }
<listener> <listener-class> com.xxx.xxx.CustomContextLoaderListener</listener-class> </listener>
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <substitutionProperty name="LOG_HOME_DUBBO" value="C:/wls/dubbo"/> <timestamp key="byDate" datePattern="yyyyMMdd"/> <!-- dubbo log --> <appender name="dubboRolling" class="ch.qos.logback.core.rolling.RollingFileAppender"> <Encoding>UTF-8</Encoding> <file>${LOG_HOME_DUBBO}/MTP-DUBBO.log</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_HOME_DUBBO}/DEMO-%d{yyyy-MM-dd}.%i-DUBBO.zip</fileNamePattern> <maxHistory>30</maxHistory> <TimeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <MaxFileSize>100MB</MaxFileSize> </TimeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> <immediateFlush>true</immediateFlush> </encoder> </appender> <logger name="com.alibaba.dubbo" level="DEBUG"> <appender-ref ref="dubboRolling"/> </logger> </configuration>