Log4j2 不加载自定义 ConsoleLogConverter 和 SocketLogResolverFactor 插件

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

我正在尝试向 Log4j2 添加两个自定义插件:

ConsoleLogConverter
和 `SocketLogResolverFactory'。但是,Log4j2 在应用程序启动时不会加载这些插件,尽管它们已正确定位和注释。 我这样做原则上是为了向日志添加屏蔽,但到目前为止我什至无法测试它是否有效

  • Log4j2版本:2.24
  • Java 版本:17
  • 框架:Spring Boot 3.3.2
  • 构建系统:Maven 我尝试过创建 log4j2.plugins 文件和创建 log4j2.component.properties 文件。 起初 Log4j2Plugins.dat 仅在目标中,然后我添加它以从目标复制到资源,但我的转换器没有注册

这是我已经检查过的:

  1. 检查类
    ConsoleLogConverter
    SocketLogResolverFactory
    是否位于正确的包中并使用“@Plugin”进行注释。
  2. 确保配置文件
    log4j2.xml
    在启动时加载。
  3. 检查注释
    @Plugin
    @PluginFactory
    是否从正确的 Log4j 包中导入。
  4. 检查了 Log4j2 和其他库的版本冲突
    pom.xml 

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="trace" monitorInterval="10">
    <Properties>
        <Property name="encProvider"/>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg_json%n"/>
            <Filters>
                <RegexFilter regex="(?i).*Authorization.*" onMatch="DENY" onMismatch="NEUTRAL"/>
                <RegexFilter regex="(?i).*getAccessToken.*" onMatch="DENY" onMismatch="NEUTRAL"/>
                <RegexFilter regex="(?i).*jwt.*" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
        </Console>
    </Appenders>

    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

log4j2 的 Pom

  <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j2-impl</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-layout-template-json</artifactId>
            <version>${log4j.version}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.mapstruct</groupId>
                            <artifactId>mapstruct-processor</artifactId>
                            <version>${mapstruct.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                            <version>${lombok.version}</version>
                        </path>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok-mapstruct-binding</artifactId>
                            <version>${lombok.mapstruct.binding.version}</version>
                        </path>
                        <path>
                            <groupId>org.apache.logging.log4j</groupId>
                            <artifactId>log4j-core</artifactId>
                            <version>${log4j2.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.liquibase</groupId>
                <artifactId>liquibase-maven-plugin</artifactId>
                <version>${liquibase.version}</version>
                <configuration>
                    <propertyFile>
                        src/main/resources/liquibase/liquibase.properties
                    </propertyFile>
                    <changeLogFile>
                        src/main/resources/liquibase/db_changelog_master.yaml
                    </changeLogFile>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <id>copy-log4j2-plugins</id>
                        <phase>process-resources</phase>
                        <goals>
                            <goal>copy-resources</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${basedir}/src/main/resources/META-INF/org/apache/logging/log4j/core/config/plugins</outputDirectory>
                            <resources>
                                <resource>
                                    <directory>${project.build.directory}/classes/META-INF/org/apache/logging/log4j/core/config/plugins</directory>
                                    <includes>
                                        <include>Log4j2Plugins.dat</include>
                                    </includes>
                                </resource>
                            </resources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

控制台日志转换器

@SuppressWarnings("unused")
@Plugin(name = "ConsoleLogConverter", category = CATEGORY)
@ConverterKeys({"msg_json"})
public class ConsoleLogConverter extends LogEventPatternConverter implements LogEventAppender {

    public ConsoleLogConverter() {
        super("Message", "message");
        System.out.println("ConsoleLogConverter loaded!");
    }

    public static ConsoleLogConverter newInstance(String[] options) {
        return new ConsoleLogConverter();
    }

    @Override
    public void format(LogEvent event, StringBuilder toAppendTo) {
        appendAsJson(event, toAppendTo);
    }
}
java log4j2
1个回答
0
投票

我的转换器未注册

你如何确定这一点?

Log4j2 应该找到您的插件,如果插件本身和

Log4j2Plugins.dat
文件都在 启动应用程序的类路径上。

Log4j2 在应用程序启动时不会加载这些插件

您如何启动您的应用程序?


起初Log4j2Plugins.dat仅在目标中,然后我添加它以从目标复制到资源

...那不是事,不要这样做。

© www.soinside.com 2019 - 2024. All rights reserved.