2

I am trying to use log4j2.xml instad of log4j but I keep getting

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.

File is located under src/main/resources

 <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="WIP">
  <Appenders>
    <Console name="console" target="SYSTEM_OUT">
        <PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %MDC{threadTrackId} %40c{1.} - %msg%n"/>
    </Console>
</Appenders>
<Loggers>
    <Root level="warn">
        <AppenderRef ref="console"/>
    </Root>
    <Logger name="com.test" level="debug"/>
    <Logger name="org.springframework" level="info"/>
</Loggers>
</Configuration>

I am not sure if the file is being located correctly but the appenders inside it are not read correctly or the file is not being located.

in My pom file I have the following dependencies

<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </dependency>
<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-1.2-api</artifactId>
    </dependency>

And this is my build section in pom

  <build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.4.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>java</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <mainClass>com.wirecard.wip.db.RecreateDatabase</mainClass>
                <cleanupDaemonThreads>false</cleanupDaemonThreads>
            </configuration>
        </plugin>
    </plugins>
</build>

In my class this is how I initialize my logger

private static final Logger LOG =   LoggerFactory.getLogger(MyClass.class);

and my imports are

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

I run my class through maven using this command

mvn -B -f pom.xml exec:java -Dlog4j.configurationFile=log4j2.xml

I also tried

mvn -B -f pom.xml exec:java -Dlog4j.configurationFile=classpath:log4j2.xml

Any help is appreciated

Amer Qarabsa
  • 6,412
  • 3
  • 20
  • 43
  • 2
    Are you trying to use just log4j2.xml with the actually old log4j libs? You might be setting up libs dependencies incorreclty. Check this topic https://stackoverflow.com/questions/25386651/slf4j-log4j2-maven-setup-query – Alexey R. Oct 06 '17 at 17:35
  • @AlexeyR. I already tried this – Amer Qarabsa Oct 09 '17 at 12:29

2 Answers2

12

The message

log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment).
log4j:WARN Please initialize the log4j system properly.

is coming from log4j1. You must still have the jar on your classpath. Run "mvn dependency:tree" and see what is bringing it in and then add an exclude for it.

rgoers
  • 8,696
  • 1
  • 22
  • 24
  • yes actually that was the problem , I figured it out several days ago abut forgot to post an answer , thanks for your help – Amer Qarabsa Oct 12 '17 at 10:40
  • i also faced same problem, i had a jar that pointed to log4j1 in my classpath. I traced that jar and found that it was indirectly coming from another jar. Excluded that inner jar and built once again. Now it is working. Thanks. – Bruce wayne - The Geek Killer Nov 11 '21 at 07:20
0

If you want to refer log4j.properties file from custom location, create log file also at custom location and if you are using:

  1. spring boot
  2. log4j version 1.2+

and you exclude the

"spring-boot-starter-logging"

from

"spring-boot-starter"

Then add this dependency after this above dependency in maven

<dependency>
        <groupId>log4j</groupId>
        <artifactId>apache-log4j-extras</artifactId>
        <version>1.2.17</version>
    </dependency>

Do the clean build.

It will refer the file from custom location and create log file as well. Hope this will help someone.

Atul
  • 3,043
  • 27
  • 39