当前位置:   article > 正文

13.7 SpringBoot集成日志系统logback的几个问题_org.springframework.util.resourceutils logback

org.springframework.util.resourceutils logback

问题1: Logging system failed to initialize using configuration from 'logback.xml '

application.properties配置文件中value后面有空格。

logging.config=logback.xml
  • 1

让人感到疑惑的是,SpringBoot居然没有对application.properties配置文件value末端作空格trim处理。

问题2:java.lang.IllegalStateException: java.io.FileNotFoundException: class path resource [/logback-lightsword.xml] cannot be resolved to URL because it does not exist

问题描述

配置内容:

  1. #logging
  2. logging.config=classpath:/logback-lightsword.xml
  • 1
  • 2

报错日志:

  1. java.lang.IllegalStateException: java.io.FileNotFoundException: class path resource [/logback-lightsword.xml] cannot be resolved to URL because it does not exist
  2. at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:311)
  3. at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:272)
  4. at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:235)
  5. at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:208)
  6. at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
  7. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
  8. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
  9. at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:72)
  10. at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
  11. at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:338)
  12. at org.springframework.boot.SpringApplication.run(SpringApplication.java:309)
  13. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1187)
  14. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1176)
  15. at com.springboot.in.action.LightSwordApplication$.delayedEndpoint$com$springboot$in$action$LightSwordApplication$1(LightSwordApplication.scala:6)
  16. at com.springboot.in.action.LightSwordApplication$delayedInit$body.apply(LightSwordApplication.scala:5)
  17. at scala.Function0.apply$mcV$sp(Function0.scala:34)
  18. at scala.Function0.apply$mcV$sp$(Function0.scala:34)
  19. at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
  20. at scala.App.$anonfun$main$1$adapted(App.scala:76)
  21. at scala.App$$Lambda$5/1451043227.apply(Unknown Source)
  22. at scala.collection.immutable.List.foreach(List.scala:389)
  23. at scala.App.main(App.scala:76)
  24. at scala.App.main$(App.scala:74)
  25. at com.springboot.in.action.LightSwordApplication$.main(LightSwordApplication.scala:5)
  26. at com.springboot.in.action.LightSwordApplication.main(LightSwordApplication.scala)
  27. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  28. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  29. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  30. at java.lang.reflect.Method.invoke(Method.java:497)
  31. at org_scala_tools_maven_executions.MainHelper.runMain(MainHelper.java:161)
  32. at org_scala_tools_maven_executions.MainWithArgsInFile.main(MainWithArgsInFile.java:26)
  33. Caused by: java.io.FileNotFoundException: class path resource [/logback-lightsword.xml] cannot be resolved to URL because it does not exist
  34. at org.springframework.util.ResourceUtils.getURL(ResourceUtils.java:135)
  35. at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:303)
  36. ... 30 common frames omitted
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

原因分析

涉及报错的源码在org.springframework.util.ResourceUtils

  1. /**
  2. * Resolve the given resource location to a {@code java.net.URL}.
  3. * <p>Does not check whether the URL actually exists; simply returns
  4. * the URL that the given location would correspond to.
  5. * @param resourceLocation the resource location to resolve: either a
  6. * "classpath:" pseudo URL, a "file:" URL, or a plain file path
  7. * @return a corresponding URL object
  8. * @throws FileNotFoundException if the resource cannot be resolved to a URL
  9. */
  10. public static URL getURL(String resourceLocation) throws FileNotFoundException {
  11. Assert.notNull(resourceLocation, "Resource location must not be null");
  12. if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
  13. String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length());
  14. ClassLoader cl = ClassUtils.getDefaultClassLoader();
  15. URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path));
  16. if (url == null) {
  17. String description = "class path resource [" + path + "]";
  18. throw new FileNotFoundException(description +
  19. " cannot be resolved to URL because it does not exist");
  20. }
  21. return url;
  22. }
  23. try {
  24. // try URL
  25. return new URL(resourceLocation);
  26. }
  27. catch (MalformedURLException ex) {
  28. // no URL -> treat as file path
  29. try {
  30. return new File(resourceLocation).toURI().toURL();
  31. }
  32. catch (MalformedURLException ex2) {
  33. throw new FileNotFoundException("Resource location [" + resourceLocation +
  34. "] is neither a URL not a well-formed file path");
  35. }
  36. }
  37. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

通过源码,我们可以看出spring配置文件里这个locations是uri表示,也就是说我们写的logback-dev.xml是当前相对路径。

解决方案

spring配置文件里这个locations是相对路径,要访问classpath,要使用相对路径:

logging.config=classpath:logback-dev.xml
  • 1

问题3: SpringBoot集成日志logback.groovy报错: Groovy classes are not available on the class path. ABORTING INITIALIZATION.

问题描述

SpringBoot集成日志logback.groovy报错: Groovy classes are not available on the class path. ABORTING INITIALIZATION.

logback.groovy配置文件内容如下:

  1. //https://logback.qos.ch/translator/asGroovy.html
  2. import ch.qos.logback.classic.encoder.PatternLayoutEncoder
  3. import ch.qos.logback.classic.filter.ThresholdFilter
  4. import ch.qos.logback.core.ConsoleAppender
  5. import ch.qos.logback.core.rolling.RollingFileAppender
  6. import ch.qos.logback.core.rolling.TimeBasedRollingPolicy
  7. import java.nio.charset.Charset
  8. import static ch.qos.logback.classic.Level.*
  9. def USER_HOME = System.getProperty("user.home")
  10. def APP_NAME = "lightsword"
  11. scan("60 seconds")
  12. context.name = "${APP_NAME}"
  13. jmxConfigurator()
  14. logger("org.springframework.web", INFO)
  15. logger("com.springboot.in.action", TRACE)
  16. logger("org.apache.velocity.runtime.log", INFO)
  17. appender("CONSOLE", ConsoleAppender) {
  18. encoder(PatternLayoutEncoder) {
  19. pattern = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n"
  20. charset = Charset.forName("utf8")
  21. }
  22. }
  23. appender("dailyRollingFileAppender", RollingFileAppender) {
  24. file = "${USER_HOME}/logs/${APP_NAME}"
  25. rollingPolicy(TimeBasedRollingPolicy) {
  26. fileNamePattern = "${APP_NAME}.%d{yyyy-MM-dd}.log"
  27. maxHistory = 30
  28. }
  29. filter(ThresholdFilter) {
  30. level = ERROR
  31. }
  32. encoder(PatternLayoutEncoder) {
  33. pattern = "%d{HH:mm:ss.SSS} [%thread] %-5level %logger{35} - %msg %n"
  34. }
  35. }
  36. root(DEBUG, ["CONSOLE", "dailyRollingFileAppender"])
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

详细日志如下:

  1. ERROR in ch.qos.logback.classic.LoggerContext[default] - Groovy classes are not available on the class path. ABORTING INITIALIZATION.
  2. at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:311)
  3. at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:272)
  4. at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:235)
  5. at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:208)
  6. at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
  7. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
  8. at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
  9. at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:72)
  10. at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
  11. at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:338)
  12. at org.springframework.boot.SpringApplication.run(SpringApplication.java:309)
  13. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1187)
  14. at org.springframework.boot.SpringApplication.run(SpringApplication.java:1176)
  15. at com.springboot.in.action.LightSwordApplication$.delayedEndpoint$com$springboot$in$action$LightSwordApplication$1(LightSwordApplication.scala:6)
  16. at com.springboot.in.action.LightSwordApplication$delayedInit$body.apply(LightSwordApplication.scala:5)
  17. at scala.Function0.apply$mcV$sp(Function0.scala:34)
  18. at scala.Function0.apply$mcV$sp$(Function0.scala:34)
  19. at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
  20. at scala.App.$anonfun$main$1$adapted(App.scala:76)
  21. at scala.App$$Lambda$5/1451043227.apply(Unknown Source)
  22. at scala.collection.immutable.List.foreach(List.scala:389)
  23. at scala.App.main(App.scala:76)
  24. at scala.App.main$(App.scala:74)
  25. at com.springboot.in.action.LightSwordApplication$.main(LightSwordApplication.scala:5)
  26. at com.springboot.in.action.LightSwordApplication.main(LightSwordApplication.scala)
  27. ... 6 more
  28. Caused by: java.lang.IllegalStateException: Logback configuration error detected:
  29. ERROR in ch.qos.logback.classic.LoggerContext[default] - Groovy classes are not available on the class path. ABORTING INITIALIZATION.
  30. at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:161)
  31. at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:57)
  32. at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:47)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

原因分析

在类路径中没有Groovy类。

解决方案

项目中添加groovy依赖:

  1. <!-- https://mvnrepository.com/artifact/org.codehaus.groovy/groovy-all -->
  2. <dependency>
  3. <groupId>org.codehaus.groovy</groupId>
  4. <artifactId>groovy-all</artifactId>
  5. <version>${groovy.version}</version>
  6. </dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/735005
推荐阅读
相关标签
  

闽ICP备14008679号