When you run as an uber jar you are most likely breaking things. 

Log4j uses a file named Log4j2Plugins.dat to define its plugins. Every jar that 
has Log4j plugins will have one. When you create an uber jar most likely only 
one is left and it probably doesn’t contain the core Log4j stuff.

To solve this you need to use 
https://github.com/edwgiz/maven-shaded-log4j-transformer.

Ralph

> On Jul 6, 2022, at 5:07 AM, Alain Désilets <alaindesile...@gmail.com> wrote:
> 
> Here is another interesting thing.
> 
> If I remove the log4j-1.2-api dependency from the pom, then the when I try
> to build the project from Terminal, I get the following failure:
> 
> [*ERROR*] 
> */Users/desilets/Documents/GitRepositories/SpikeLog4j2Compatibility/src/main/java/org/examples/HelloWorld.java:[3,24]
> package org.apache.log4j does not exist*
> 
> 
> Which is what I would expect.
> 
> But the project builds and runs without problem in intelliJ. This is true
> even if I do "Invalidate caches and Restart".
> 
> Very strange.
> 
> On Wed, Jul 6, 2022 at 7:27 AM Alain Désilets <alaindesile...@gmail.com>
> wrote:
> 
>> I am trying to use log4j2 through log4j-1.2-api in the context of an
>> uber-jar created with the maven shade plugin and am experiencing issues.
>> 
>> I wrote a simple HelloWorld class that illustrates the issue.
>> 
>> When I run this class through intelliJ, everything works fine. But when I
>> run it through a terminal, I get
>> 
>> ERROR StatusLogger Reconfiguration failed: No configuration found for
>> '55f96302' at 'null' in 'null'
>> 
>> 
>> In both cases, I invoke the command with
>> 
>> -Dlog4j.configuration=/path/to/my/log4j.properties
>> 
>> 
>> And the log4j.properties is in the old log4j style (i.e. NOT log4j2 style).
>> 
>> Not sure what I am doing wrong.
>> 
>> One thing I notice is that the class path is different in both situations.
>> When running through intelliJ, it contains a great number of jars,
>> including:
>> 
>> 
>>   -
>>   
>> .m2/repository/org/apache/logging/log4j/log4j-core/2.17.2/log4j-core-2.17.2.jar
>>   -
>>   
>> .m2/repository/org/apache/logging/log4j/log4j-api/2.17.2/log4j-api-2.17.2.jar
>>   -
>>   
>> .m2/repository/org/apache/logging/log4j/log4j-1.2-api/2.17.2/log4j-1.2-api-2.17.2.jar
>> 
>> whereas when I run it from terminal, it just contains my one uber-jar. But
>> based on the content of my pom, I expect that the uber jar will contain
>> classes from those same log4j jars.
>> 
>> Here are the details of this HelloWorld example.
>> 
>> 
>> This is the HelloWorld class
>> 
>> package org.examples;
>> 
>> import org.apache.log4j.Logger;
>> 
>> import java.net.URL;
>> import java.net.URLClassLoader;
>> 
>> public class HelloWorld {
>> 
>>  public static void main(String[] args) {
>>    printClassPath();
>>    Logger logger = Logger.getLogger("org.examples.HelloWorld");
>>    logger.trace("Hello from Logger");
>>  }
>> 
>>  private static void printClassPath() {
>>    ClassLoader cl = ClassLoader.getSystemClassLoader();
>>    URL[] urls = ((URLClassLoader)cl).getURLs();
>> 
>>    System.out.println("Class path is:");
>>    for(URL url: urls){
>>      System.out.println(url.getFile());
>>    }
>>  }
>> }
>> 
>> 
>> And here is the pom:
>> 
>> <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";>
>> 
>>  <modelVersion>4.0.0</modelVersion>
>>  <groupId>org.examples</groupId>
>>  <artifactId>log4j2-compatibility</artifactId>
>>  <version>1.0.0</version>
>> 
>>  <properties>
>>    <log4j.version>2.17.2</log4j.version>
>>  </properties>
>> 
>>  <build>
>>    <plugins>
>>      <!-- Use shade plugin to create an uber-jar with all dependencies -->
>>      <plugin>
>>        <groupId>org.apache.maven.plugins</groupId>
>>        <artifactId>maven-shade-plugin</artifactId>
>>        <version>3.1.0</version>
>>        <configuration>
>>          <shadedArtifactAttached>true</shadedArtifactAttached>
>>          <shadedClassifierName>jar-with-dependencies</shadedClassifierName>
>>          <filters>
>>            <filter>
>>              <artifact>*:*</artifact>
>>              <excludes>
>>                <!-- This file may contain some log4j configuration that
>>                    is not in sync with the actual version of log4j we
>>                    use. So we exclude all versions of that file that
>>                    might be inherited from a dependency-->
>>                <exclude>**/Log4j2Plugins.dat</exclude>
>>              </excludes>
>>            </filter>
>>          </filters>
>>        </configuration>
>>        <executions>
>>          <execution>
>>            <phase>package</phase>
>>            <goals>
>>              <goal>shade</goal>
>>            </goals>
>>          </execution>
>>        </executions>
>>      </plugin>
>>    </plugins>
>>  </build>
>> 
>>  <dependencies>
>> 
>>    <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-1.2-api</artifactId>
>>      <version>${log4j.version}</version>
>>    </dependency>
>> 
>>  </dependencies>
>> </project>
>> 
>> 
>> And here is the log4.properties file
>> 
>> # Log to stdout only
>> log4j.rootLogger=warn, stdout
>> 
>> # Configure the stdout logger
>> log4j.appender.stdout=org.apache.log4j.ConsoleAppender
>> log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
>> log4j.appender.stdout.layout.ConversionPattern =-- %c{2}: %m%n
>> 
>> # Activate some traces
>> log4j.logger.org.examples.HelloWorld=trace
>> 
>> 


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-user-h...@logging.apache.org

Reply via email to