Log4j2 configuration not found when running standalone application built by shade plugin
Asked Answered
A

4

32

I have application which when I run from maven log4j2 it is working:

mvn exec:java -Dexec.args=...

but when I run jar as standalone application then it shows error:

java -jar

log:

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 No log4j2 configuration file found. Using default configuration: logging only errors to the console.
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.

I dont understand this error. It shows that log4j2 configuration file is not found but also complain about some format which is probably in configuration file

My configuration is:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="off">
    <Appenders>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d [%t] %-5p - %-26.26c{1} - %m\n" />
        </Console>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="console" />
        </Root>

        <Logger name="my.package" level="DEBUG" /> 

    </Loggers>
</Configuration>

and it is located in root directory of jar file.

UPDATE

jar is created by maven shade plugin:

        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>

so it contains all necessary libraries (about 23 MB) and when I run this jar I just need to specify arguments

Atwood answered 22/1, 2016 at 11:16 Comment(4)
Can you given some info about how you build your jar file - is it a fat jar, or if not, how you supply the classpath when you execute it?Erythroblastosis
I am facing the same issue while building the fat jar using "maven-assembly-plugin" can you suggest something?Gypsum
@bestwishes Did you managed to find the solution? I have the same issue and the solutions below are not working for me. thanksWrapped
For those who are facing the same issue with maven-assembly-plugin" change version to 2.6 <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>2.6</version>Cherlynchernow
A
22

ok I found this issue about this problem.

In short, the problem arises when application classes are packaged in uber jar using maven shade plugin. While for log4j2 version 2.8.1 the fix is still pending, the suggested workaround is to update maven pom.xml with extra configuration settings for shade plugin as follow:

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

        . . . . .

        <build>
            . . . . . 
            <plugins>
                . . . . . 
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-shade-plugin</artifactId>
                    <version>2.4.1</version>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <transformers>
                            <transformer
                                    implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer"/>
                        </transformers>
                    </configuration>
                    <executions>
                        <execution>
                            <phase>package</phase>
                            <goals>
                                <goal>shade</goal>
                            </goals>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>com.github.edwgiz</groupId>
                            <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
                            <version>2.1</version>
                        </dependency>
                    </dependencies>
                </plugin>
                . . . . . 
            </plugins>
        . . . . . 
        </build>
        . . . . . 
        <pluginRepositories>
            <pluginRepository>
                <id>oss.sonatype.org</id>
                <name>OSS Sonatype Staging</name>
                <url>https://oss.sonatype.org/content/groups/staging</url>
            </pluginRepository>
        </pluginRepositories>      
        . . . . . 
    </project>
Atwood answered 22/1, 2016 at 12:29 Comment(4)
add this to what file?Yukoyukon
to pom.xml where u have shade pluginAtwood
@Atwood How about if I use ant?Contradiction
What does the plugin exactly do?Veer
T
6

Please refer this for the complete example of pom.file.

In addition to the answer given by @hudi, we need to add dependencies inside the plugin for the transformation.

<build>
    <sourceDirectory>src/main/java</sourceDirectory>
    <resources>
        <resource>
            <directory>src/main/resources</directory>               
        </resource>
    </resources>
    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-shade-plugin</artifactId>
    <version>2.4.1</version>
    <executions>
      <execution>
        <phase>package</phase>
        <goals>
          <goal>shade</goal>
        </goals>
        <configuration>
          <transformers>
            <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                <manifestEntries>
                    <Main-Class>com.auto.facade.RunMain</Main-Class>
                    <Build-Number>123</Build-Number>
                </manifestEntries>
            </transformer>
            <transformer implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer" />                
          </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>
    <dependencies>
                <dependency>
                    <groupId>com.github.edwgiz</groupId>
                    <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
                    <version>2.6.1</version>
                </dependency>
            </dependencies>
  </plugin>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.1</version>
        <configuration>
          <source>1.7</source>
          <target>1.7</target>
        </configuration>
      </plugin>
    </plugins>
</build>
Tightlipped answered 21/9, 2016 at 13:35 Comment(0)
A
1

I have resolved this issue by adding the following line in the run configuration.

-Dlog4j.configurationFile=C:\Users\aaa\Desktop\deploy\bin\log4j2.xml

My Log4j version is

2.12.1

Allnight answered 18/11, 2020 at 6:14 Comment(0)
S
0
<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.4.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <filters>
                            <filter>
                                <artifact>*:*</artifact>
                                <excludes>
                                    <exclude>META-INF/*.SF</exclude>
                                    <exclude>META-INF/*.DSA</exclude>
                                    <exclude>META-INF/*.RSA</exclude>
                                </excludes>
                            </filter>
                        </filters>
                        <finalName>project-name-product-1.0.0-SNAPSHOT</finalName>
                        <transformers>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
                                <mainClass>your.main.classname</mainClass>
                            </transformer>
                            <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>
                            <transformer implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
                                <resource>META-INF/spring.tooling</resource>
                            </transformer>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ComponentsXmlResourceTransformer"/>
                            <transformer
                                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
                            <transformer implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer" />

                        </transformers>
                    </configuration>
                </execution>
            </executions>
            <dependencies>
                <dependency>
                    <groupId>com.github.edwgiz</groupId>
                    <artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
                    <version>2.6.1</version>
                </dependency>
            </dependencies>
        </plugin>
Supertax answered 27/10, 2016 at 14:24 Comment(1)
Hi, Thanks for the answer. Though it might be more useful if you explain what it does hand how it solves the OP's problem.Nerva

© 2022 - 2024 — McMap. All rights reserved.