好吧,項目中一直使用的是logback做日志記錄。
開始跑Demo的時候,一直會報Failed to load class org.slf4j.impl.StaticLogger的錯誤。后來google下,發現是這個原因:
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.
原來我只導了slf4j-ai-1.7.2.jar,logback-core-1.0.7.jar,后來加上logback-classic-1.0.7.jar,就不報錯了。
具體的配置呢,參考這個blog就好了呢。http://aub.iteye.com/blog/1101260 根據blog去做配置的時候,又發現了一個問題。
就是logback.xml里面的配置文件一直木有生效哈。
先借用剛才那個blog里面的demo吧
java code

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 }
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>
明明我的logback.xml的文件中配置的是將log打印到文件中,實際運行的時候,卻一直在控制台打印下面的信息。
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
奇怪吧。之前是把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>
所以,上面的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>
這樣在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