logback配置

好吧,項目中一直使用的是logback作日誌記錄。java

開始跑Demo的時候,一直會報Failed to load class org.slf4j.impl.StaticLogger的錯誤。後來google下,發現是這個緣由:app

This error is reported when the org.slf4j.impl.StaticLoggerBinder class could not be loaded into memory. This happens when no appropriate SLF4J binding could be found on the class path. Placing one (and only one) of slf4j-nop.jar , slf4j-simple.jar , slf4j-log4j12.jar , slf4j-jdk14.jar or logback-classic.jar on the class path should solve the problem.eclipse

原來我只導了slf4j-ai-1.7.2.jar,logback-core-1.0.7.jar,後來加上logback-classic-1.0.7.jar,就不報錯了。ide

具體的配置呢,參考這個blog就行了呢。http://aub.iteye.com/blog/1101260 根據blog去作配置的時候,又發現了一個問題。ui

就是logback.xml裏面的配置文件一直木有生效哈。google

先借用剛纔那個blog裏面的demo吧spa

java codedebug

 1 package demo;
 2 
 3 import org.slf4j.Logger;
 4 import org.slf4j.LoggerFactory;
 5 
 6 import ch.qos.logback.classic.LoggerContext;
 7 import ch.qos.logback.core.util.StatusPrinter;
 8 
 9 public class LogbackDemo {
10     private static Logger log = LoggerFactory.getLogger(LogbackDemo.class);
11 
12     public static void main(String[] args) {
13         log.trace("====trace");
14         log.debug("====debug");
15         log.info("====info");
16         log.warn("====warn");
17         log.error("====error");
18 //        String URL = "logback.xml";
19 //        System.out.println(ClassLoader.getSystemResource(URL));
20 //        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
21 //        // print logback's internal status
22 //        StatusPrinter.print(lc);
23     }
24 }
View Code

logback.xml日誌

 1 <configuration>
 2   <appender name="FILE" class="ch.qos.logback.core.FileAppender">
 3     <file>./testFile.log</file>
 4     <append>true</append>
 5     <encoder>
 6       <pattern>%-4relative [%thread] %-5level %logger{35} -%msg%n</pattern>
 7     </encoder>
 8   </appender>
 9   <root level="INFO">
10     <appender-ref ref="FILE" />
11   </root>
12 </configuration>
View Code

明明個人logback.xml的文件中配置的是將log打印到文件中,實際運行的時候,卻一直在控制檯打印下面的信息。code

output in console

1 17:15:38.444 [main] DEBUG demo.LogbackDemo - ====debug
2 17:15:38.448 [main] INFO  demo.LogbackDemo - ====info
3 17:15:38.448 [main] WARN  demo.LogbackDemo - ====warn
4 17:15:38.448 [main] ERROR demo.LogbackDemo - ====error
View Code

奇怪吧。以前是把logback.xml放在conf這個文件夾下面的,後來把logback.xml直接放在src下就能夠了。知道緣由了吧,logback的運行機制是默認去classpath下面去找配置文件,是怎樣去查找的呢?

1.Logback 嘗試在classpath中(in the classpath)尋找一個名爲 logback.groovy  的文件。
2.若是沒有找到該文件, logback 嘗試在classpath中(in the classpath)尋找一個名爲 logback-test.xml 的文件。
3.若是沒有找到該文件,嘗試在classpath中(in the classpath)尋找一個名爲 logback.xml 的文件。
4.若是沒有找到任何一個文件,logback 本身自動使用 BasicConfigurator 配置,它使 logging output 被定向到 console

因爲,demo中我並無配置logback.groovy,logback-test.xml,而logback.xml文件又被我放到了conf文件夾下,因此,也沒有找到,最後,logback只好將日誌以默認的配置輸出了。

默認配置

 1 <configuration>
 2   <appender name="STDOUT"
 3   class="ch.qos.logback.core.ConsoleAppender">
 4     <!-- encoders are assigned the type
 5          ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
 6     <encoder>
 7       <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} -
 8       %msg%n</pattern>
 9     </encoder>
10   </appender>
11   <root level="debug">
12     <appender-ref ref="STDOUT" />
13   </root>
14 </configuration>
View Code

因此,上面的output是否是能夠理解了呢。

好吧,把logback.xml放在了conf文件夾下後,怎麼讓logback找到它呢?剛纔說了,logback的運行機制是默認去classpath下面去找配置文件,因此,把文件夾conf加入到classpath中就行了。在eclipse的java build path中,找到source,add folder就行了。再從新跑demo的話就一切OK了。

有時,爲了更好的維護代碼,增長靈活性,須要把logback.xml的一些配置項的值寫在另外一個配置文件中,通常是properties文件哈。

config.properties

1 LOG_LEVEL=DEBUG
2 APP_USER_HOME=./

相應的logaback.xml

 1 <configuration scan="true" debug="true">
 2   <property resource="conf.properties" />
 3   <appender name="FILE" class="ch.qos.logback.core.FileAppender">
 4     <file>${APP_USER_HOME}/testFile.log</file>
 5     <append>true</append>
 6     <encoder>
 7       <pattern>%-4relative [%thread] %-5level %logger{35} -%msg%n</pattern>
 8     </encoder>
 9   </appender>
10   <root level="${LOG_LEVEL}">
11     <appender-ref ref="FILE" />
12   </root>
13 </configuration>
View Code

這樣在src下面就會產生一個日誌文件testFile.log;

同時,將configuration的屬性debug設置爲"true"時,這樣容許訪問logback內部的運行狀態,這時候將會在控制檯打印下面的信息

 1 17:38:27,562 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
 2 17:38:27,562 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
 3 17:38:27,562 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/D:/workspace/LogbackDemo/bin/logback.xml]
 4 17:38:27,626 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[D:\workspace\LogbackDemo\bin\logback.xml]] every 60 seconds. 
 5 17:38:27,626 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
 6 17:38:27,639 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.FileAppender]
 7 17:38:27,641 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE]
 8 17:38:27,650 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
 9 17:38:27,675 |-INFO in ch.qos.logback.core.FileAppender[FILE] - File property is set to [.//testFile.log]
10 17:38:27,676 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
11 17:38:27,676 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[ROOT]
12 17:38:27,677 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
13 17:38:27,678 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@13c468a - Registering current configuration as safe fallback point

固然,logback中還有不少別的設置,最最詳細的看下面這兩篇blog最好了。

一個是以前提到的那個blog作基礎入門:http://aub.iteye.com/blog/1101260

還有一篇blog作深刻了解:http://www.yulezhandian.com/?p=324 

相關文章
相關標籤/搜索