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

posted @ 2020-03-19 14:29  朴仁  阅读(4507)  评论(1编辑  收藏  举报