Log4J2 Error Statuslogger Unrecognized Conversion Specifier

Log4J2 Error Statuslogger Unrecognized Conversion Specifier

I have log4j2 in my project when I run main method in intellij Idea ,it correct to print log.

when i use maven-shade-plugin package project to jar file, and run jar as standalone application it shows error:

java -cp package.jar com.xxx.TestMain

the console output

ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [level]
ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [logger]
ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [msg]
ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [n]
ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [level]
ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [logger]
ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [msg]
ERROR StatusLogger Unrecognized conversion specifier [msg] starting at position 54 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [n]
ERROR StatusLogger Unrecognized conversion specifier [n] starting at position 56 in conversion pattern.

pom.xml config . log4j2.version = 2.10.0, spring boot version is 1.5.9.RELEASE

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${log4j2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j2.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j2.version}</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
                <createDependencyReducedPom>false</createDependencyReducedPom>
            </configuration>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <transformers>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.handlers</resource>
                            </transformer>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.schemas</resource>
                            </transformer>
                        </transformers>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                    </configuration>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
        </resource>
    </resources>
    <sourceDirectory>src/main/java</sourceDirectory>
</build>

log4j2.xml config

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" monitorInterval="30" shutdownHook="disable">
    <properties>
        <property name="LOG_HOME">/data1/logs</property>
        <property name="JOB_NAME">noname</property>
    </properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss} [%t] %level [%logger{36}][%file:%line] %msg%n%throwable"/>
        </Console>

        <RollingRandomAccessFile name="DetailFile"
                                 fileName="${sys:LOG_HOME}/${sys:JOB_NAME}/detail.log" bufferedIO="false"
                                 filePattern="${sys:LOG_HOME}/${sys:JOB_NAME}/detail.%d{yyyy-MM-dd}-%i.log">

            <PatternLayout
                    pattern="%date{yyyy-MM-dd HH:mm:ss} [%t] %level [%file:%line] %msg%n%throwable"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="500 MB"/>
            </Policies>
        </RollingRandomAccessFile>
        <RollingRandomAccessFile name="error"
                                 fileName="${sys:LOG_HOME}/${sys:JOB_NAME}/error.log" bufferedIO="true"
                                 filePattern="${sys:LOG_HOME}/${sys:JOB_NAME}/error.%d{yyyy-MM-dd}.log">

            <PatternLayout
                    pattern="%date{yyyy-MM-dd HH:mm:ss} [%t] %level [%logger{36}:%line] %msg%n%throwable"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingRandomAccessFile>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="DetailFile"/>
            <AppenderRef level="error" ref="error"/>
        </Root>
    </Loggers>
</Configuration>
3

9 Answers

If a dependency contains log4j2 plugin, a Log4j2Plugins.dat cache file is included in the jar. When the Maven shade plugin is used to merge multiple jars with a Log4j2Plugins.dat file, only one will survive. Without the plugin definitions, errors are shown on startup. Log4j2 issue

One solution for this is excluding Log4j2Plugins.dat cache file from shaded jar so that Log4j scans for plugins on startup. For this, add a filter in maven-shade-plugin configuration in your pom:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-shade-plugin</artifactId>
  <version>3.1.0</version>
  <configuration>
    <createDependencyReducedPom>false</createDependencyReducedPom>
    <filters>
      <filter>
        <artifact>*:*</artifact>
        <excludes>
          <exclude>**/Log4j2Plugins.dat</exclude>
        </excludes>
      </filter>
    </filters>
  </configuration>
  <!-- ... -->
</plugin>

Another solution is to merge the cache files using a transformation plugin which is log4j version specific.

5

If you are using Gradle and ShadowJar 4+:

shadowJar {
  transform(com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer)
}

Gradle Kotlin DSL:

shadowJar {
  transform(com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer::class.java)
}
1

Unfortunately, I can't reply to Shashank Kapoor answer, as he correctly pointed out excluding the Log4j2Plugins.dat file solves the issue. This did the trick for me while using gradle:

shadowJar {
    exclude "**/Log4j2Plugins.dat"
}
2

I’ve seen this error before when people had multiple versions of Log4j2 on the classpath.

2

For those of us who are removing the older Logj42 from our classpaths to remediate the Log4Shell (CVE-2021-44228) vulnerability, you may find you have this issue when using your uber jars after you upgrade.

If you are building your uber jars from Maven or Gradle, use one of the plugins respectively:

  • Maven: with
  • Gradle:

if you are building them by some other means, see this answer: . But basically you have to properly handle Log4j2 plugin cache files as this class does:

to properly handle the Log4j2Plugins.dat file when building the new uber jar.

On our project, such a problem arose after the creation of the TestAppender class.

@Plugin(name = "TestAppender", category = Core.CATEGORY_NAME, elementType = Appender.ELEMENT_TYPE)
public class TestAppender extends AbstractAppender {
...

Its settings overwrote the logger settings in the Log4j2Plugins.dat file.

Then we moved it to a separate module and began to attach it to the project in a similar way (please, pay attention to scope):

<dependency>
  <groupId>info.project</groupId>
  <artifactId>test</artifactId>
  <version>1.0-SNAPSHOT</version>
  <scope>test</scope>
</dependency>

This link works for me. There's a problem while shading log4j

<filter>
    <artifact>log4j:log4j</artifact>
    <includes>
        <include>org/apache/log4j/spi/LoggingEvent.class</include>
    </includes>
</filter>
<filter>
    <artifact>org.apache.logging.log4j:log4j-*</artifact>
    <includes>
        <include>**</include>
    </includes>
</filter>
@PowerMockIgnore( {"org.springframework.util.Log4jConfigurer","javax.script.*","javax.management.*", "org.w3c.dom.*", "org.apache.log4j.*","org.apache.logging.*","org.apache.commons.logging.*", "org.xml.sax.*",   "javax.xml.*"})

added this snippet to the test class. No error log anymore.

1

You can exclude all the Log4j2Plugins.dat files except the right one, the one contained in log4j-core:

<filter>
    <artifact>SOME-ARTIFACT</artifact>
    <excludes>
        <exclude>**/Log4j2Plugins.dat</exclude>
    </excludes>
</filter>
<filter>
    <artifact>SOME-ARTIFACT</artifact>
    <excludes>
        <exclude>**/Log4j2Plugins.dat</exclude>
    </excludes>
</filter>
...

See answer here, with a sample project:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi
Author

Maya Lin-Takahashi

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.