1.Logback配置
1.配置步驟及默認配置
logback便可以經過編程式配置,也能夠經過xml的形式配置。web
logback配置步驟:spring
2.xml文件進行配置
在此經過xml文件建立一個上一小節中最小化配置,並將xml文件放到classpath中,代碼以下:編程
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <!-- encoders are assigned the type ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender>
<root level="debug">
<appender-ref ref="STDOUT" /> </root> </configuration>
3.自動打印警告和錯誤消息
示例:打印logback的內部信息狀態瀏覽器
1 package logback; 2 import org.slf4j.Logger; 3 import org.slf4j.LoggerFactory; 4 import ch.qos.logback.classic.LoggerContext; 5 import ch.qos.logback.core.util.StatusPrinter; 6 public class Demo2 { 7 private static Logger logger=LoggerFactory.getLogger(Demo2.class); 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 //得到logback的logger上下文 11 LoggerContext context=(LoggerContext) LoggerFactory.getILoggerFactory(); 12 //打印內部的日誌:context找配置文件的過程,以及最終的配置 13 StatusPrinter.print(context); 14 logger.info("enter application."); 15 //Foo foo=new Foo(); 16 //foo.doIt(); 17 logger.info("exit application."); 18 } 19 }
若是一切順利的話,控制檯會輸出如下內容:app
14:17:33,156 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml] 14:17:33,157 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy] 14:17:33,157 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/E:/code/myeclipse_workspace/spring_heima_video/logback/bin/logback.xml] 14:17:33,237 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set 14:17:33,237 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender] 14:17:33,246 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT] 14:17:33,252 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property 14:17:33,310 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG 14:17:33,310 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT] 14:17:33,311 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration. 14:17:33,312 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@198e2867 - Registering current configuration as safe fallback point 14:17:33.318 [main] INFO logback.Demo2 -enter application. 14:17:33.321 [main] INFO logback.Demo2 -exit application.
總結一下:在代碼中能夠經過ch.qos.logback.core.util.StatusPrinter對象方便的得到logback框架內部的狀態。在配置文件中一樣能夠達到得到logback框架內部狀態的這種效果,方法是設置configuration標籤的debug屬性爲true;請注意debug屬性只與狀態數據有關,它不影響logback的配置,更不會影響記錄級別。框架
注意:eclipse
4.配置文件修改後自動從新加載
注意:內部實現是這樣的,當設置掃描屬性爲 true 時,會安裝一個叫 ReconfigureOnChangeFilter的 TurboFilter。每次調用 logger 的打印方法時,都會進行掃描。ide
好比,當名爲 myLogger 的logger 執行「myLogger.debug("hello");」時,若是scan屬性爲true,則ReconfigureOnChangeFilter會被調用。並且,即便 myLogger 的 debug 級別被禁用了,仍然會調用上述過濾器。url
5.直接調用JoranConfigurator
6.查看狀態消息
要想在web程序中實現上述功能,能夠在WEB-INF/web.xml中添加以下內容:spa
<servlet> <servlet-name>ViewStatusMessages</servlet-name>
<servlet-class>ch.qos.logback.classic.ViewStatusMessagesServlet</servlet-class> </servlet>
<servlet-mapping>
<servlet-name>ViewStatusMessages</servlet-name>
<url-pattern>/lbClassicStatus</url-pattern> </servlet-mapping>
經過瀏覽器查看錶格形式的logback內部狀態信息的網址爲:http://host/yourWebapp/lbClassicStatus。
7.監聽狀態消息
1.爲StatusManager註冊StatusListener監聽器
2.xml方式註冊狀態監聽器
<configuration>
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" /> ... the rest of the configuration file </configuration>
經過xml配置文件註冊狀態監聽器,方法是在configuration標籤中添加子標籤statusListener,並指定實現類。