I have a maven java project using log4j2 2.5 (NOT log4j version 1.x).
I have read a lot of posts here regarding my issue but nothing works for me, so please, could someone help me?
I am using log4j2 in the following way:
1) Import package in each of my classes:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2) For each class, immediately after class line I create a logger:
public class A {
// log4j2 logger
private static final Logger logger = LogManager.getLogger(Main.class.getName());
}
3) Once I have created the logger as step 2, I use it as normal:
logger.trace("some message");
logger.info("another message");
I have put log4j2.xml file in src\main\resources (this folder is in the class-path):
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
<Properties>
<Property name="LOG_DIR">.</Property>
<Property name="ARCHIVE">output.log</Property>
<Property name="CONSOLE_PATTERN">[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</Property>
<Property name="FILE_PATTERN">%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Property>
</Properties>
<Appenders>
<Console name="console" target="SYSTEM_OUT">
<PatternLayout pattern="${CONSOLE_PATTERN}"/>
</Console>
<File name="file" fileName="${LOG_DIR}/${ARCHIVE}" immediateFlush="false" append="false">
<PatternLayout pattern="${FILE_PATTERN}"/>
</File>
</Appenders>
<Loggers>
<Root level="INFO" additivity="false">
<AppenderRef ref="console"/>
<AppenderRef ref="file"/>
</Root>
<Logger name="com.distributed.analyzer" level="TRACE" additivity="false">
<AppenderRef ref="console"/>
<AppenderRef ref="file"/>
</Logger>
<Logger name="com.distributed.analyzer.errors" level="ERROR" additivity="false">
<AppenderRef ref="console"/>
<AppenderRef ref="file"/>
</Logger>
</Loggers>
</Configuration>
Regarding maven, I have put below log4j2 dependencies for maven:
log4j-api-2.5.jar
log4j-core-2.5.jar
in pom.xml file I specify as below:
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.5</version>
</dependency>
From eclipse, I can build the project without problems (without errors) and I can execute it as well. All correct.
So right now what I am trying to do is to obtain a runnable jar file in order to I can execute using below command line:
java -jar myapp.jar
So in order to build it in a simple way I use maven-shade-plugin and I specify it in my pom.xml as follows:
<plugins>
<!-- Maven Shade Plugin -->
<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>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.distributed.analyzer.Main</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
So I build it using below goals: compile package
JAR file is correctly created but when I launch it from command line using:
java -jar myapp.jar
below error message is shown (seems like log4j2 is not found... I do not understand):
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/logging/log4j/LogManager
at com.distributed.analyzer.Main.<clinit>(Main.java:22)
Caused by: java.lang.ClassNotFoundException: org.apache.logging.log4j.LogManager
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
When running the program in eclipse everything work fine, but when I package it with maven and try to run the jar I get this kind of error....
UPDATED: If I create the runnable jar using export option from eclipse and the checking: "Package required libraries into the generated JAR" then the JAR package is created correctly. It contains all jar files in the root and a folder resources with all the resources but If I execute again using:
java -jar myapp.jar
I continue getting similar error:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.