使用STS和Spring启动创建的项目中缺少logback.xml

时间:2017-04-28 16:11:05

标签: java spring-boot

我在STS中创建了一个Spring引导Web项目,使用的是上一版本 我收到错误,即logback.xml丢失,但我在src / main / resources

上创建
03:37:08,276 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
03:37:08,276 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
03:37:08,277 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/D:/Sites/sts/GoogleCustomSearchBoot/target/classes/logback.xml]
03:37:08,385 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
03:37:08,388 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
03:37:08,396 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
03:37:08,445 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
03:37:08,446 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
03:37:08,447 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
03:37:08,448 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@61a485d2 - Registering current configuration as safe fallback point
Logging system failed to initialize using configuration from 'classpath:/logback.xml'
java.io.FileNotFoundException: class path resource [/logback.xml] cannot be resolved to URL because it does not exist
    at org.springframework.util.ResourceUtils.getURL(ResourceUtils.java:135)
    at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:307)
    at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:276)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:212)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:73)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:336)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
    at com.example.GoogleCustomSearchBootApplication.main(GoogleCustomSearchBootApplication.java:10)
03:37:08.825 [main] DEBUG o.s.b.l.ClasspathLoggingApplicationListener - Application failed to start with classpath: [file:/D:/Sites/sts/GoogleCustomSearchBoot/target/classes/, file:/D:/java/m2/org/springframework/boot/spring-boot-starter-web/1.5.3.RELEASE/spring-boot-starter-web-1.5.3.RELEASE.jar, file:/D:/java/m2/org/springframework/boot/spring-boot-starter/1.5.3.RELEASE/spring-boot-starter-1.5.3.RELEASE.jar, file:/D:/java/m2/org/springframework/boot/spring-boot/1.5.3.RELEASE/spring-boot-1.5.3.RELEASE.jar, file:/D:/java/m2/org/springframework/boot/spring-boot-autoconfigure/1.5.3.RELEASE/spring-boot-autoconfigure-1.5.3.RELEASE.jar, file:/D:/java/m2/org/springframework/boot/spring-boot-starter-logging/1.5.3.RELEASE/spring-boot-starter-logging-1.5.3.RELEASE.jar, file:/D:/java/m2/ch/qos/logback/logback-classic/1.1.11/logback-classic-1.1.11.jar, file:/D:/java/m2/ch/qos/logback/logback-core/1.1.11/logback-core-1.1.11.jar, file:/D:/java/m2/org/slf4j/jcl-over-slf4j/1.7.25/jcl-over-slf4j-1.7.25.jar, file:/D:/java/m2/org/slf4j/jul-to-slf4j/1.7.25/jul-to-slf4j-1.7.25.jar, file:/D:/java/m2/org/slf4j/log4j-over-slf4j/1.7.25/log4j-over-slf4j-1.7.25.jar, file:/D:/java/m2/org/yaml/snakeyaml/1.17/snakeyaml-1.17.jar, file:/D:/java/m2/org/springframework/boot/spring-boot-starter-tomcat/1.5.3.RELEASE/spring-boot-starter-tomcat-1.5.3.RELEASE.jar, file:/D:/java/m2/org/apache/tomcat/embed/tomcat-embed-core/8.5.14/tomcat-embed-core-8.5.14.jar, file:/D:/java/m2/org/apache/tomcat/embed/tomcat-embed-el/8.5.14/tomcat-embed-el-8.5.14.jar, file:/D:/java/m2/org/apache/tomcat/embed/tomcat-embed-websocket/8.5.14/tomcat-embed-websocket-8.5.14.jar, file:/D:/java/m2/org/hibernate/hibernate-validator/5.3.5.Final/hibernate-validator-5.3.5.Final.jar, file:/D:/java/m2/javax/validation/validation-api/1.1.0.Final/validation-api-1.1.0.Final.jar, file:/D:/java/m2/org/jboss/logging/jboss-logging/3.3.1.Final/jboss-logging-3.3.1.Final.jar, file:/D:/java/m2/com/fasterxml/classmate/1.3.3/classmate-1.3.3.jar, file:/D:/java/m2/com/fasterxml/jackson/core/jackson-databind/2.8.8/jackson-databind-2.8.8.jar, file:/D:/java/m2/com/fasterxml/jackson/core/jackson-annotations/2.8.0/jackson-annotations-2.8.0.jar, file:/D:/java/m2/com/fasterxml/jackson/core/jackson-core/2.8.8/jackson-core-2.8.8.jar, file:/D:/java/m2/org/springframework/spring-web/4.3.8.RELEASE/spring-web-4.3.8.RELEASE.jar, file:/D:/java/m2/org/springframework/spring-aop/4.3.8.RELEASE/spring-aop-4.3.8.RELEASE.jar, file:/D:/java/m2/org/springframework/spring-beans/4.3.8.RELEASE/spring-beans-4.3.8.RELEASE.jar, file:/D:/java/m2/org/springframework/spring-context/4.3.8.RELEASE/spring-context-4.3.8.RELEASE.jar, file:/D:/java/m2/org/springframework/spring-webmvc/4.3.8.RELEASE/spring-webmvc-4.3.8.RELEASE.jar, file:/D:/java/m2/org/springframework/spring-expression/4.3.8.RELEASE/spring-expression-4.3.8.RELEASE.jar, file:/D:/java/m2/org/slf4j/slf4j-api/1.7.25/slf4j-api-1.7.25.jar, file:/D:/java/m2/org/springframework/spring-core/4.3.8.RELEASE/spring-core-4.3.8.RELEASE.jar, file:/D:/java/m2/org/springframework/boot/spring-boot-configuration-processor/1.5.3.RELEASE/spring-boot-configuration-processor-1.5.3.RELEASE.jar, file:/D:/java/m2/com/vaadin/external/google/android-json/0.0.20131108.vaadin1/android-json-0.0.20131108.vaadin1.jar, file:/D:/java/m2/joda-time/joda-time/2.9.9/joda-time-2.9.9.jar]
03:37:08.830 [main] ERROR o.s.boot.SpringApplication - Application startup failed
java.lang.IllegalStateException: java.io.FileNotFoundException: class path resource [/logback.xml] cannot be resolved to URL because it does not exist
    at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:315)
    at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:276)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:239)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:212)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:167)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:122)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:73)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:336)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151)
    at com.example.GoogleCustomSearchBootApplication.main(GoogleCustomSearchBootApplication.java:10)
Caused by: java.io.FileNotFoundException: class path resource [/logback.xml] cannot be resolved to URL because it does not exist
    at org.springframework.util.ResourceUtils.getURL(ResourceUtils.java:135)
    at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:307)
    ... 13 common frames omitted

我正在附加项目结构,我包括logb​​ack.xml

Project Structure

编辑:我在我的logback.xml文件中添加了debug="true",日志显示了更多内容,我刚刚更新了。我可以看到首先找到logback.xml,但在某些时候它再次被要求,但是这次没有找到。

<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <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>

4 个答案:

答案 0 :(得分:1)

您已使用值logging.config配置classpath:logback.xml属性,但类路径中没有logback.xml文件。

您需要提供logback.xml配置文件或删除logging.config的配置,并允许Boot使用其默认的Logback配置。

答案 1 :(得分:0)

上一版本的工作似乎是1.2.8。 在我的Windows机器上,Spring boot 1.3和1.4根本不起作用。

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

答案 2 :(得分:0)

我遇到了同样的问题。在我的例子中,pom.xml是过滤文件,因此它没有看到.xml的logback文件。

<resource>
            <directory>src/main/resources/</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.jpg</include>
                <include>**/*.properties</include>
            </includes>
</resource>

您可以将.xml包含在包含会话中。

答案 3 :(得分:-1)

如果您使用Maven构建应用程序,则需要确保告诉Maven在jar中包含resources文件夹。

我遇到了这个问题并在我们的科技博客中写了一篇名为Switching logging to logback的文章。博客(点)artifact-software(dot)com(斜杠)tech是网站。

<testResources>
  <testResource>
    <directory>${project.basedir}/src/test/resources</directory>
  </testResource>
</testResources>

需要在构建部分中,以便将测试资源放入测试设置中。

需要添加类似的部分才能将其放入生产罐中。