Spring Boot 执行器和 Log4j2

在 Spring 应用程序中,我尝试使用 Log4j2 而不是默认的日志记录实现 Logback。所以在我的 pom.xml 中,我排除spring-boot-starter-logging并包含了spring-boot-starter-log4j2. 所以我可以使用Log4j2。


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


我的问题是:如何在 Spring-Boot-Application 中使用 Log4j2 而不是 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>

红糖糍粑
浏览 144回答 1
1回答

慕尼黑的夜晚无繁华

好的,经过一些研究后我发现,问题是定义为配置 Log4j2 日志记录的 log4j2-spring.xml。一个简单的 log4j2 配置如下所示:<?xml version="1.0" encoding="UTF-8"?><Configuration><Appenders>&nbsp; &nbsp; <Console name="Console" target="SYSTEM_OUT">&nbsp; &nbsp; &nbsp; &nbsp; <PatternLayout&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />&nbsp; &nbsp; </Console>&nbsp; &nbsp; <RollingFile name="RollingFile"&nbsp; &nbsp; &nbsp; &nbsp; fileName="./logs/spring-boot-logger-log4j2.log"&nbsp; &nbsp; &nbsp; &nbsp; filePattern="./logs/$${date:yyyy-MM}/spring-boot-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log.gz">&nbsp; &nbsp; &nbsp; &nbsp; <PatternLayout>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <pattern>%d %p %C{1.} [%t] %m%n</pattern>&nbsp; &nbsp; &nbsp; &nbsp; </PatternLayout>&nbsp; &nbsp; &nbsp; &nbsp; <Policies>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <!-- rollover on startup, daily and when the file reaches 10 MegaBytes-->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <OnStartupTriggeringPolicy />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <SizeBasedTriggeringPolicy size="10 MB" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <TimeBasedTriggeringPolicy />&nbsp; &nbsp; &nbsp; &nbsp; </Policies>&nbsp; &nbsp; </RollingFile></Appenders><Loggers>&nbsp; &nbsp; <!-- LOG everything at INFO level -->&nbsp; &nbsp; <Root level="info">&nbsp; &nbsp; &nbsp; &nbsp; <AppenderRef ref="Console" />&nbsp; &nbsp; &nbsp; &nbsp; <AppenderRef ref="RollingFile" />&nbsp; &nbsp; </Root>&nbsp; &nbsp; <!-- LOG "com.example*" at TRACE level -->&nbsp; &nbsp; <Logger name="com.example" level="trace"></Logger></Loggers></Configuration>如果您使用这样的文件,则只有在<Loggers>-tag 中定义的记录器才会显示在执行器端点。所以如果你想看到更多的记录器,你必须在这个文件中添加它们。(可悲的是,我没有办法将所有可用的 Logger 包括在内,而无需一一写下每个 Logger 的名称)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java