Spring Boot Actuator and Log4j2

回眸只為那壹抹淺笑 提交于 2020-01-04 02:39:27

问题


In a Spring-Application I am trying to use Log4j2 instead of the default logging implementation Logback. So in my pom.xml I excluded spring-boot-starter-logging and included spring-boot-starter-log4j2. So I am able to use Log4j2.

But if I open now the Spring Boot Acuator endpoint localhost:8080/actuator/loggers I am not seeing all Loggers anymore. With Logback there were several hundred Loggers but with Log4j2 I am seeing only about 10.

My question is: How can I see the full list of all Loggers while using Log4j2 and not Logback in a Spring-Boot-Application?

Edit: here is my 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>

回答1:


Ok so after a little bit of research I found out, that the problem is the log4j2-spring.xml that is defined to configure the logging of Log4j2.

A simple log4j2 configuration can look like the following:

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

If you are using such a file only the Loggers defined in the <Loggers>-tag will be shown at the actuator endpoint. So if you want to see more Loggers, you have to add them in this file.

(Sadly I found no way how I can include all availabe Loggers whitout writing the name of every Logger one by one)



来源:https://stackoverflow.com/questions/52929707/spring-boot-actuator-and-log4j2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!