一尘不染

Spring Boot执行器和Log4j2

spring-boot

在Spring-Application中,我尝试使用Log4j2代替默认的日志实现Logback。因此,在我的pom.xml中,我排除spring- boot-starter-logging并包含了spring-boot-starter-log4j2。这样我就可以使用Log4j2。

但是,如果我现在打开Spring Boot
Acuator端点,那么localhost:8080/actuator/loggers我将不再看到所有Logger。使用Logback,有数百个Logger,但是使用Log4j2,我只看到大约10个。

我的问题是:在使用Log4j2时,如何查看所有Logger的完整列表,而在Spring-Boot-Application中却看不到Logback?

编辑:这是我的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.example</groupId>
<artifactId>log4jdemoclient1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>log4jdemoclient1</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <!-- needed for spring boot admin (actuator) -->
    <spring-boot-admin.version>2.0.3</spring-boot-admin.version>
</properties>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-starter-client</artifactId>
        <exclusions>
            <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-log4j2</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>       
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-dependencies</artifactId>
            <version>${spring-boot-admin.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
</project>

阅读 389

收藏
2020-05-30

共1个答案

一尘不染

好了,经过一番研究,我发现问题出在log4j2-spring.xml,它被定义为配置Log4j2的日志记录。

一个简单的log4j2配置如下所示:

<?xml version="1.0" encoding="UTF-8"?>

<Configuration>
<Appenders>
    <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout
            pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
    </Console>

    <RollingFile name="RollingFile"
        fileName="./logs/spring-boot-logger-log4j2.log"
        filePattern="./logs/$${date:yyyy-MM}/spring-boot-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log.gz">
        <PatternLayout>
            <pattern>%d %p %C{1.} [%t] %m%n</pattern>
        </PatternLayout>
        <Policies>
            <!-- rollover on startup, daily and when the file reaches 10 MegaBytes-->
            <OnStartupTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="10 MB" />
            <TimeBasedTriggeringPolicy />
        </Policies>
    </RollingFile>
</Appenders>

<Loggers>
    <!-- LOG everything at INFO level -->
    <Root level="info">
        <AppenderRef ref="Console" />
        <AppenderRef ref="RollingFile" />
    </Root>

    <!-- LOG "com.example*" at TRACE level -->
    <Logger name="com.example" level="trace"></Logger>
</Loggers>

</Configuration>

如果使用这样的文件,则仅在<Loggers>-tag中定义的记录器将显示在执行器端点。因此,如果您想查看更多Logger,则必须将其添加到此文件中。

(可悲的是,我发现我无法将所有可用的Logger都一一写出,包括每个Logger的名称)

2020-05-30