log4j:WARN No appenders could be found for logger (org.springframework.core.env.StandardEnvironment)

Viewed 23838

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

2 Answers

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.

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.

Related