Spring Boot忽略logback-spring.xml

Chr*_*ein 11 java logback maven spring-boot

我有2个使用Logback的Spring Boot(1.4.1-RELEASE)控制台应用程序.两个配置文件或多或少相同,位于my / src/main/resources文件夹中,名为logback-spring.xml.

这两个项目都在其pom.xml中包含maven依赖spring-boot-starter-logging并获取logback Version 1.1.7.

两个poms中定义的Spring Boot配置:

<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>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.1.RELEASE</version>
    <relativePath />
</parent>

<groupId>d.m.v.app-a</groupId>
<artifactId>my-app-a</artifactId>
<version>1.0.16-SNAPSHOT</version>
<packaging>jar</packaging>  

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</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-logging</artifactId>
    </dependency>
</dependencies>
Run Code Online (Sandbox Code Playgroud)

但是,在运行应用程序时,其中一个似乎完全忽略了logback配置,而另一个则像预期的那样选择了它.

如果我将文件名更改为无法正常工作的应用程序的logback.xml,它突然正常工作(即使使用我正在使用的弹簧配置文件).

所涉及的任何配置(意味着pom.xml,application.properties等)没有明显差异.

有人知道为什么会这样吗?我发现这种行为相当令人困惑.

ltu*_*ska 16

我知道它有些陈旧,但我有同样的问题并想出来......所以原因很简单就是你的类路径上有一个logback.xml(在某些地方,不一定在你开始的项目中,就我的情况而言)这是一种依赖).

看看这里: org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(LoggingInitializationContext, LogFile)

设置断点,然后你会看到.

如果spring boot在类路径上找不到任何回溯配置("logback-test.groovy","logback-test.xml","logback.groovy","logback.xml"),则logback-spring.xml将为已接.


小智 8

我通过在 application.yml 中添加 logging.config 解决了这个问题

logging:
  config: classpath:logback-spring.xml
Run Code Online (Sandbox Code Playgroud)


kim*_*y82 6

我会在 application.properties 中指定这样的配置文件的位置。

logging.config=path
Run Code Online (Sandbox Code Playgroud)

Spring 可能不会查找此文件名。春季文档

他们建议使用这个名称logback-spring.xml而不仅仅是 logback.xml

如果可能,我会将配置放在 application.properties 中。

  • 当然我可以这样做,但它不会回答为什么应用程序选择忽略其他应用程序没有忽略的相同配置的问题。 (4认同)