我的 Spring Boot 应用程序中未使用 log4j2.xml 文件

问题描述 投票:0回答:1

我有一个小型 Spring Boot 应用程序并尝试使用 log4j2。

这就是我所做的:

这是我的build.gradle

configurations.all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'       
    }

dependencies {
...
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'
...}

这是我的 log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="LOG_PATTERN">
            %d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${hostName} --- [%15.15t] %-40.40c{1.} : %m%n%ex
        </Property>
    </Properties>
    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="de.myapp" level="debug" additivity="false">
            <AppenderRef ref="ConsoleAppender" />
        </Logger>

        <Root level="debug">
            <AppenderRef ref="ConsoleAppender" />
        </Root>
    </Loggers>
</Configuration>

我将 log4j2.xml 放在与 application.properties 相同的文件夹中。

日志级别为INFO。这就是我猜测的默认级别。看来log4j.xml没有被使用。日志级别应为 DEBUG。我还尝试使用显式设置 log4j2.xml 的路径 -Dlogging.config='/path/to/log4j2.xml'。我还尝试将 log4j.xml 放在不同的文件夹中。没有任何作用,日志级别仍为 INFO。这里缺少什么?


大家好,我现在更进了一步。我将 log4j2.xml 重命名为 log4j-test.xml,现在基本日志记录和日志级别可以工作。除了我自己包下的loggimg。无论如何,它都停留在 INFO 上。 这是我的 bild.gradle 中的片段:

   configurations.all {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-log4j'

}


dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-log4j2'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.camunda.bpm.springboot:camunda-bpm-spring-boot-starter-webapp:7.15.0'
implementation 'org.springframework.boot:spring-boot-starter-web-services'
implementation 'com.oracle.ojdbc:ojdbc8:19.3.0.0'
implementation 'org.awaitility:awaitility:4.2.0'
implementation 'org.apache.activemq:activemq-broker:5.17.0'
implementation  'org.springframework:spring-jms'
implementation 'org.springframework.boot:spring-boot-starter-activemq'
implementation 'axis:axis:1.4'
implementation files("${libs}/wf-externio.jar")
implementation files("${libs}/sad-wf-mapper.jar")
implementation files("${libs}/sad-util-lib-1.5.jar")


runtimeOnly 'com.h2database:h2'
runtimeOnly 'com.sun.xml.bind:jaxb-impl:2.2.3'
implementation 'javax.xml.bind:jaxb-api:2.1'

testImplementation 'junit:junit:4.13.1'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

似乎还有另一个我必须排除的 log4j.xml 。但我不知道在哪里。我怎样才能找到并排除它。

java spring-boot logging log4j
1个回答
0
投票

这是因为 Spring Boot 默认使用 Logback 作为日志记录方法。如果想替换为Log4J,首先需要将其从

的依赖中排除

spring-boot-starter-web

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>
© www.soinside.com 2019 - 2024. All rights reserved.