Logback日志組件的配置文件的存放位置及加載


Logback日志組件的配置文件的存放位置及加載

介紹 Logback是由log4j創始人設計的另一個開源日志組件,本文介紹日志配置文件存放位置,日志配置文件加載過程

參考

logback的使用和logback.xml詳解:來自 <https://blog.csdn.net/lhl1124281072/article/details/79852582>

解決 logback.xml 配置不起作用的問題: 來自 <https://blog.csdn.net/cnwyt/article/details/80462896>

logback: http://logback.qos.ch/manual/configuration.html#auto_configuration

 

logback配置加載過程:

Let us begin by discussing the initialization steps that logback follows to try to configure itself:

  1. Logback tries to find a file called logback-test.xml in the classpath.
  2. If no such file is found, logback tries to find a file called logback.groovy in the classpath.
  3. If no such file is found, it checks for the file logback.xml in the classpath..
  4. If no such file is found, service-provider loading facility (introduced in JDK 1.6) is used to resolve the implementation of com.qos.logback.classic.spi.Configurator interface by looking up the file META-INF\services\ch.qos.logback.classic.spi.Configurator in the class path. Its contents should specify the fully qualified class name of the desired Configurator implementation.
  5. If none of the above succeeds, logback configures itself automatically using the BasicConfigurator which will cause logging output to be directed to the console.

The last step is meant as last-ditch effort to provide a default (but very basic) logging functionality in the absence of a configuration file.

 

Class path:

Where should the configuration files such as logback.groovylogback-test.xml or logback.xml be located on the classpath?

Configuration files such as logback.groovylogback-test.xml or logback.xml can be located directly under any folder declared in the class path. For example, if the class path reads "c:/java/jdk15/lib/rt.jar;c:/mylibs/" then the logback.xml file should be located directly under "c:/mylibs/", that is as "c:/mylibs/logback.xml". Placing it under a sub-folder of c:/mylibs/, say, c:/mylibs/other/, will not work.

For web-applications, configuration files can be placed directly under WEB-INF/classes/.

 

總結:logback.xml必須存放在class path所在的目錄,其下的子目錄不可以

查看classpath

查看classpath code接口:

importch.qos.logback.core.util.StatusPrinter;

importch.qos.logback.classic.LoggerContext;

 

//打印Logback內部狀態

LoggerContextlc=(LoggerContext)LoggerFactory.getILoggerFactory();

StatusPrinter.print(lc);

//獲取classpath路徑

Strings=Thread.currentThread().getContextClassLoader().getResource("").getPath();

System.out.println("classpath=>"+s);

 

1.src\main\resources 目錄下沒有logback.xml文件,或者配置文件存放在resources子目錄下,啟動程序,未找到配置文件,logback使用組件默認的日志配置

14:39:29,493 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]

14:39:29,494 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]

14:39:29,494 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.xml]

14:39:29,502 |-INFO in ch.qos.logback.classic.BasicConfigurator@77a567e1 - Setting up default configuration.

 

classpath => */out/production/classes/

 

2.把logback .xml文件放置在resource目錄下,src\main\resources\logback.xml,找到了 配置文件logback.xml

14:55:03,053 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]

14:55:03,053 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]

14:55:03,053 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/D:/*/out/production/resources/logback.xml]

小結

  1. logback組件啟動會自動到classpath目錄下依次查找logback-test.xml,logback.groovy,logback.xml,查找到就加載,沒有查找到加載組件默認的
  2. 一般日志配置文件放在java項目的resource根目錄src\main\resources\logback.xml

轉載自CSDN:https://blog.csdn.net/lanmolei814/article/details/102839755


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM