SpringBoot logback-spring.xml does not take effect solution,

Source: Internet
Author: User

SpringBoot logback-spring.xml does not take effect solution,
I. Preface

This is the case for new applications. There will be various problems. Yesterday we just solved the problem of throwing the class is not visible from class loader when loading a class, today, I encountered a problem where the log file could not be found. It is still related to the library of the second party. Here we will discuss it one by one.

Ii. Problem generation

Normally, put the logback-spring.xml's configuration file (using the logback log system) in the src/main/resources Directory, as shown in

 

Set spring. application. name = spring-boot-demo-application in application. properties.

A second-party package is introduced, which contains logback. xml.

According to the above configuration, we want. the home/spring-boot-demo-application/logs Directory should contain applicaiton. the log file, however, does not exist. The folder spring-boot-demo-application is not generated.

Iii. Problem Analysis

Then let's look at how the log system looks for and parses the log configuration file. In SpringBoot, The LoggingApplicationListener class is used to initialize the log system. LoggingApplicationListener implements the ApplicationListener interface, so we can see through the time sequence diagram what the onApplicationEvent method of LoggingApplicationListener has done:

 

Code (8) Search for the standard log configuration file. For what is the standard, check the code (9:

protected String[] getStandardConfigLocations() {  return new String[] { "logback-test.groovy", "logback-test.xml", "logback.groovy",    "logback.xml" }; }

Like "logback-test.groovy", "logback-test.xml", "logback. groovy", "logback. xml", these are standard.

So how to find it depends on the Code (10 ):

private String findConfig(String[] locations) {  for (String location : locations) {   ClassPathResource resource = new ClassPathResource(location,     this.classLoader);   if (resource.exists()) {    return "classpath:" + location;   }  }  return null; }

We can see that the ClassPathResource class is used for search. The following describes the exists method of ClassPathResource:

public boolean exists() {  return (resolveURL() != null); }  protected URL resolveURL() {  if (this.clazz != null) {   return this.clazz.getResource(this.path);  }  else if (this.classLoader != null) {   return this.classLoader.getResource(this.path);  }  else {   return ClassLoader.getSystemResource(this.path);  } }

We can see that this. classLoader. getResource (this. path) is used; find that classLoader is AppClassloader.

If no configuration is found in code (8), run the following command: (12). The logic of code 12 is similar to that of code (8), but the file name is different. Let's take a look at the following:

protected String[] getSpringConfigLocations() {  String[] locations = getStandardConfigLocations();  for (int i = 0; i < locations.length; i++) {   String extension = StringUtils.getFilenameExtension(locations[i]);   locations[i] = locations[i].substring(0,     locations[i].length() - extension.length() - 1) + "-spring."     + extension;  }  return locations; }

We can see that spring is spliced on the getStandardConfigLocations file name. The spliced file name is:

'Logback-test-spring.groovy, logback-test-spring.xml, logback-spring.groovy, logback-spring.xml'

To sum up, SpringBoot first looks for the standard log configuration file. If it cannot find the file for splicing Spring configuration.

As mentioned above, a logback is introduced in the application. xml jar package, which is also loaded using appclassloader. Therefore, the logback in the jar package is found during step (8. xml, so we won't execute step (12) again to find our custom logback-spring.xml.

Iv. Problem Solving

Solution 1: modify the configuration file to logback. xml. In this way, logback. xml is first searched in step (8), which can be found.

Solution 2: Avoid the second-party package containing logback. xml, in this case, no matter whether our own configuration is logback-spring.xml or logback. xml there will be no problem.

V. Summary

In daily development, the second-party package does not contain a log configuration file. logs used in the second-party library are generally created using code.

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.