$ java -Dlog4j.configuration=file:///path/to/your/log4j2.xml -jar /path/to/your/jar_file.jar
Written to the console, you get
ERROR
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.
To work with version 2 of log4j aka "log4j2"
-Dlog4j.configuration=
should read
-Dlog4j.configurationFile=
log4j:WARN ....
In your project, uninclude the log4j-1.2 jar and instead, include the log4j-1.2-api-2.1.jar. I wasn't sure how exactly to exclude the log4j 1.2. I knew that what dependency of my project was requiring it. So, with some reading, I excluded a bunch of stuff.
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.10</artifactId>
<version>0.8.2.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
I am not sure which of the exclusions did the trick. Separately, I included a dependency to the 1.2 api which bridges to 2.x.
<!--
http://logging.apache.org/log4j/2.0/manual/migration.html
http://logging.apache.org/log4j/2.0/maven-artifacts.html
Log4j 1.x API Bridge
If existing components use Log4j 1.x and you want to have this logging
routed to Log4j 2, then remove any log4j 1.x dependencies and add the
following.
-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>2.2</version>
</dependency>
Now, the 1.2 logs which were only going to the console actually flow to our 2.x appenders.
In my case I am using the log4j2 Json file log4j2.json
in the classpath of my gradle project and I got the same error.
The solution here was to add dependency for JSON handling to my gradle dependencies.
compile group:"com.fasterxml.jackson.core", name:"jackson-core", version:'2.8.4'
compile group:"com.fasterxml.jackson.core", name:"jackson-databind", version:'2.8.4'
compile group:"com.fasterxml.jackson.core", name:"jackson-annotations", version:'2.8.4'
See also documentation of log4j2:
The JSON support uses the Jackson Data Processor to parse the JSON files. These dependencies must be added to a project that wants to use JSON for configuration:
I am getting this error because log4j2 file got deleted from my src folder
Simply add xml file under src folder
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
</Console>
<RollingFile name="RollingFile" filename="logs/B2CATA-hybrid.log"
filepattern="${logPath}/%d{yyyyMMddHHmmss}-fargo.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" />
<Policies>
<SizeBasedTriggeringPolicy size="100 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.pragiti." level="trace" />
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
</Loggers>