在Spring Boot中禁用自动日志记录配置

3

我使用的是spring boot 1.2.1.RELEASE版本,并注意到在启动时,spring会自动更改我的log4j配置。

以下是我的(spring)依赖项:

<!-- parent includes slf4j and log4j -->
<dependencies>
    <dependency>
        <!-- Import dependency management from Spring Boot -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>1.1.2.RELEASE</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <version>1.1.2.RELEASE</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
        <version>1.1.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>de.komoot.wanderwalter</groupId>
        <artifactId>wanderwalter-api-models</artifactId>
        <version>1.26-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>de.komoot.wanderwalter</groupId>
        <artifactId>wanderwalter-routing</artifactId>
        <version>1.26-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.graphhopper</groupId>
        <artifactId>graphhopper</artifactId>
        <version>0.3-kmt</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
        <version>${spring.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
</dependencies>

<dependencyManagement>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.3</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.8</version>
        </dependency>
    </dependencies>
</dependencyManagement>

当我使用 -Dlog4j.configuration=log4j-live.xml -Dlog4j.debug 启动应用程序时,我可以看到首先使用了我的log4j配置,然后Spring清除它并安装自己的配置,然后(我猜测)从类路径中添加默认的 log4j.xml。

我该如何只使用默认的log4j行为,或者如何定义 Spring 使用哪个文件进行配置?

祝好,

Jan

2个回答

4

非常感谢,我只是在查看文档而不是Java文档。那个标志隐藏得很好。我会尝试一下并让你知道结果。 - Jan
2
该属性在参考文档中也有描述。点击此处查看。 - celkins
最终按预期工作了(即使使用 --logging.config,这对独立应用程序看起来更好)。我使用 classpath:log4j-custom.xml 作为值。 - Jan
--logging.config=../etc/log4j.xml 对我有效,但 -Dlogging.config=../etc/log4j.xml 在使用Spring Boot 1.3.0.RELEASE和log4j 1.2.17时无效。 - lukass77

0

我在以下设置中遇到了类似的问题:

  • spring boot 2.7.3
  • spring 5.3.22
  • log4j 2.18

通过 log4j-1.2-api 组件将 log4j-1 配置(和 API 使用)桥接到 log4j2,命令行中定义 "-Dlog4j1.compatibility=true"。

根据 celkins 的建议定义 logging.config(指向 V1 log4j.xml)没有帮助,因为由于实现不知道 log4j V1 配置语法(sax 解析错误),所以甚至连 spring 的重新配置都失败了。

我的解决方案是:

设置命令行属性

-Dorg.springframework.boot.logging.LoggingSystem=none

这使得Spring可以实例化一个不执行任何操作的NoOp LoggingSystem。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接