升级到Spring 4.2后,logback配置抛出FileNotFound异常

时间:2019-06-17 19:23:31

标签: spring mongodb spring-boot upgrade logback

自从我从spring-boot-starter-parent 1.2.3-RELEASE升级到1.3.4-RELEASE并将spring.version 4.1.6升级到4.2.5以来,我的项目一直抛出以下异常:

Logging system failed to initialize using configuration from 'config/logback.xml'
java.io.FileNotFoundException: C:\Documents\workspace\application\config\logback.xml (The system cannot find the path specified)

首先初始化位于C:\ Documents \ workspace \ application \ src \ main \ config \ logback.xml的我的资源文件夹中的logback配置后,将引发异常。

我正在升级,因为我的mongodb数据库现在需要使用1.2.3无法提供的SCRAM-SHA-1机制进行身份验证。 1.3.4似乎是带有authMechanism的spring-boot-starter-data-mongodb的最小版本。

我尝试升级到spring-boot-starter-parent 2.0.X,并且在登录初始化后没有得到FileNotFoundException,但是我的应用程序没有启动。

我什至尝试更改我的application.properties文件中logging.config属性的值,但出现了另一个错误。

有人经历过吗?您对我如何克服这些困难以及启动我的应用程序有任何建议吗?

这是我的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.workspace</groupId>
    <artifactId>application</artifactId>
    <version>1.0-SNAPSHOT</version>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.4.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <tomcat.version>8.0.21</tomcat.version>
        <spring.version>4.2.5.RELEASE</spring.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.github.fakemongo</groupId>
            <artifactId>fongo</artifactId>
            <version>1.6.2</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <!--<version>4.0.2.RELEASE</version>-->
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
            <!--<version>1.2.3.RELEASE</version>-->
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-integration</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <!--<version>${spring-boot.version}</version>-->
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-jwt</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <!--<version>2.0.7.RELEASE</version>-->
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>2.1.4.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.mangofactory</groupId>
            <artifactId>swagger-springmvc</artifactId>
            <version>0.9.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>4.6</version>
        </dependency>
        <dependency>
            <groupId>net.bull.javamelody</groupId>
            <artifactId>javamelody-core</artifactId>
            <version>1.56.0</version>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/assembly</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/config</directory>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>${java.version}</source>
                    <target>${java.version}</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.shared</groupId>
                        <artifactId>maven-filtering</artifactId>
                        <version>1.3</version>
                    </dependency>
                </dependencies>
                <configuration>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>jks</nonFilteredFileExtension>
                        <nonFilteredFileExtension>jceks</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-assembly-plugin</artifactId>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <finalName>${project.build.finalName}.${BUILD_NUMBER}</finalName>
                    <appendAssemblyId>false</appendAssemblyId>
                    <descriptors>
                        <descriptor>src/main/assembly/bin.xml</descriptor>
                    </descriptors>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <repositories>
    </repositories>

    <pluginRepositories>
    </pluginRepositories>

</project>

和application.properties中的日志记录属性

spring.data.mongodb.uri=mongodb://application:application@mongo_cluster1.com,mongo_cluster_2.com,mongo_cluster3.com/application?replicaSet=uat-RepSet&authMechanism=SCRAM-SHA-1
database.name=application
logging.config=config/logback.xml

这是Logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
    <property resource="application.properties" />

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d %5p | %t | %-55logger{55} | %m %n</pattern>
        </encoder>
    </appender>

    <appender name="application" class="net.logstash.logback.appender.LogstashSocketAppender">
        <host>port</host>
        <!-- port is optional (default value shown) -->
        <port>port</port>

        <encoder>
            <pattern>%%d %p %c{1.} [%t] %m%n</pattern>
        </encoder>
    </appender>

    <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>logs/${pom.name}.log</file>
        <append>true</append>
        <encoder>
            <pattern>%d %5p | %t | %-55logger{55} | %m %n</pattern>
        </encoder>
        <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
            <fileNamePattern>logs/${pom.name}.%i.log.zip</fileNamePattern>
            <minIndex>1</minIndex>
            <maxIndex>13</maxIndex>
        </rollingPolicy>
        <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
            <maxFileSize>10MB</maxFileSize>
        </triggeringPolicy>
    </appender>

    <!--<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashSocketAppender">
        <host>host</host>
        <port>port</port>
    </appender>-->

    <logger name="Logger" additivity="false" level="INFO">
        <appender-ref ref="application"/>
        <appender-ref ref="FILE"/>
        <appender-ref ref="CONSOLE"/>
    </logger>

    <logger name="org.springframework" level="INFO">
        <appender-ref ref="CONSOLE"/>
    </logger>

    <logger name="com.application.techquest.service.trace.logger.MethodBodyLogger" additivity="false" level="OFF">
        <appender-ref  ref="application"/>
    </logger>

    <root>
        <level value="INFO"/>
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE"/>
        <appender-ref ref="application"/>
    </root>
</configuration>


这是控制台日志

19:53:42,800 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/C:/Documents/workspace/application/target/classes/logback.xml]
19:53:42,908 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
19:53:42,922 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Will scan for changes in [file:/C:Documents/workspace/application/target/classes/logback.xml] 
19:53:42,922 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeTask scanning period to 30 seconds
19:53:42,944 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
19:53:42,949 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [CONSOLE]
19:53:42,960 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
19:53:43,055 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [net.logstash.logback.appender.LogstashSocketAppender]
19:53:43,307 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [application]
19:53:43,309 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@16:18 - no applicable action for [encoder], current ElementPath  is [[configuration][appender][encoder]]
19:53:43,309 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@17:22 - no applicable action for [pattern], current ElementPath  is [[configuration][appender][encoder][pattern]]
19:53:43,313 |-INFO in net.logstash.logback.appender.LogstashSocketAppender[application] - Defaulting maxMessageSize to [65000]
19:53:43,328 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.rolling.RollingFileAppender]
19:53:43,330 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [FILE]
19:53:43,332 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
19:53:43,340 |-INFO in ch.qos.logback.core.rolling.FixedWindowRollingPolicy@3439f68d - Will use zip compression
19:53:43,348 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - Active log file name: logs/pom.name_IS_UNDEFINED.log
19:53:43,348 |-INFO in ch.qos.logback.core.rolling.RollingFileAppender[FILE] - File property is set to [logs/pom.name_IS_UNDEFINED.log]
19:53:43,351 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [iswLogger] to INFO
19:53:43,351 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [iswLogger] to false
19:53:43,351 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [application] to Logger[iswLogger]
19:53:43,352 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[iswLogger]
19:53:43,352 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[iswLogger]
19:53:43,352 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.springframework] to INFO
19:53:43,352 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[org.springframework]
19:53:43,352 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.workspace.techquest.service.trace.logger.MethodBodyLogger] to OFF
19:53:43,352 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [com.workspace.techquest.service.trace.logger.MethodBodyLogger] to false
19:53:43,352 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [application] to Logger[com.workspace.techquest.service.trace.logger.MethodBodyLogger]
19:53:43,352 |-INFO in ch.qos.logback.classic.joran.action.LevelAction - ROOT level set to INFO
19:53:43,352 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [CONSOLE] to Logger[ROOT]
19:53:43,352 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [FILE] to Logger[ROOT]
19:53:43,352 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [application] to Logger[ROOT]
19:53:43,352 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
19:53:43,354 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@dbd940d - Registering current configuration as safe fallback point

Logging system failed to initialize using configuration from 'config/logback.xml'
java.io.FileNotFoundException: C:\Documents\workspace\application\config\logback.xml (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
    at java.net.URL.openStream(URL.java:1045)
    at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:281)
    at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:255)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:224)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:200)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:119)
    at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:111)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:65)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:330)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
    at com.workspace.Application.main(Application.java:14)
Exception in thread "main" java.lang.IllegalStateException: java.io.FileNotFoundException: C:\Documents\workspace\application\config\logback.xml (The system cannot find the path specified)
    at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:289)
    at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:255)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:224)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:200)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:119)
    at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:111)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:65)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:330)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
    at com.workspace.Application.main(Application.java:14)
Caused by: java.io.FileNotFoundException: C:\Documents\workspace\application\config\logback.xml (The system cannot find the path specified)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:93)
    at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90)
    at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188)
    at java.net.URL.openStream(URL.java:1045)
    at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:281)
    ... 14 more

Process finished with exit code 1

当我尝试将logging.config属性更改为src \ main \ config \ logback.xml时,异常更改为

Logging system failed to initialize using configuration from 'src/main/config/logback.xml'
java.lang.IllegalStateException: Logback configuration error detected: 
ERROR in ch.qos.logback.core.joran.spi.Interpreter@16:18 - no applicable action for [encoder], current ElementPath  is [[configuration][appender][encoder]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@17:22 - no applicable action for [pattern], current ElementPath  is [[configuration][appender][encoder][pattern]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@16:18 - no applicable action for [encoder], current ElementPath  is [[configuration][appender][encoder]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@17:22 - no applicable action for [pattern], current ElementPath  is [[configuration][appender][encoder][pattern]]
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.loadConfiguration(LogbackLoggingSystem.java:151)
    at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithSpecificConfig(AbstractLoggingSystem.java:57)
    at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:47)
    at org.springframework.boot.logging.logback.LogbackLoggingSystem.initialize(LogbackLoggingSystem.java:106)
    at org.springframework.boot.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:282)
    at org.springframework.boot.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:255)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:224)
    at org.springframework.boot.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:200)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:163)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:136)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:119)
    at org.springframework.boot.context.event.EventPublishingRunListener.publishEvent(EventPublishingRunListener.java:111)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:65)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:54)
    at org.springframework.boot.SpringApplication.createAndRefreshContext(SpringApplication.java:330)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1191)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1180)
    at com.workspace.Application.main(Application.java:14)

1 个答案:

答案 0 :(得分:0)

我认为这主要是由于您过滤了pom中的logback.xml引起的。要么

<resource>
    <directory>src/main/config</directory>
    <filtering>false</filtering>
</resource>

或:

<resource>
    <directory>src/main/config</directory>
    <filtering>true</filtering>
    <includes>
            <include>logback.xml</include>
    </includes>
</resource>

都应该解决您的问题。