Spring Boot忽略logback-spring.xml

问题描述 投票:9回答:4

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

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

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

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

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

java maven spring-boot logback
4个回答
14
投票

我知道它有些陈旧,但我有同样的问题并想出来......所以原因很简单就是你的类路径上有一个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将为已接。


3
投票

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

logging.config=path

Spring可能没有找到这个文件名。 Spring doc

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

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


0
投票

要使用Logback,您需要在类路径中包含它和spring-jcl。最简单的方法是通过启动器,它们都依赖于spring-boot-starter-logging。对于Web应用程序,您只需要spring-boot-starter-web,因为它依赖于日志记录启动器。如果您使用Maven,以下依赖项会为您添加日志记录:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

因此,删除冗余的日志记录依赖项。


0
投票

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

logging:
  config: classpath:logback-spring.xml
© www.soinside.com 2019 - 2024. All rights reserved.