I am trying to add two custom plugins to Log4j2: ConsoleLogConverter
and `SocketLogResolverFactory’. However, Log4j2 does not load these plugins when the application is launched, despite the fact that they are correctly positioned and annotated.
I did this in principle in order to add masking to the logs, but so far I can’t even test whether it works or not
- Log4j2 version: 2.24
- Java versions: 17
- Framework: Spring Boot 3.3.2
- Build system: Maven
I have tried both the log4j2.plugins file to create and the log4j2.component.properties file to create.
And at first Log4j2Plugins.dat was only in target, then I added it to be copied from target to resources, but my converter does not register
Here’s what I’ve already checked:
- Checked that the classes
ConsoleLogConverter
andSocketLogResolverFactory
are in the correct package and annotated using `@Plugin’. - Made sure that the configuration file is
log4j2.xml
is loaded at startup. - Checked that the annotations
@Plugin
and@PluginFactory
were imported from the correct Log4j packages. - Checked for conflicting versions of Log4j2 and other libraries in
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>
Pom for log4j2
<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>
ConsoleLogConverter
@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);
}
}