我正在尝试向 Log4j2 添加两个自定义插件:
ConsoleLogConverter
和 `SocketLogResolverFactory'。但是,Log4j2 在应用程序启动时不会加载这些插件,尽管它们已正确定位和注释。
我这样做原则上是为了向日志添加屏蔽,但到目前为止我什至无法测试它是否有效
这是我已经检查过的:
ConsoleLogConverter
和 SocketLogResolverFactory
是否位于正确的包中并使用“@Plugin”进行注释。log4j2.xml
在启动时加载。@Plugin
和 @PluginFactory
是否从正确的 Log4j 包中导入。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);
}
}
我的转换器未注册
你如何确定这一点?
Log4j2 应该找到您的插件,如果插件本身和
Log4j2Plugins.dat
文件都在 启动应用程序的类路径上。
Log4j2 在应用程序启动时不会加载这些插件
您如何启动您的应用程序?
起初Log4j2Plugins.dat仅在目标中,然后我添加它以从目标复制到资源
...那不是事,不要这样做。