It looks like this question has been asked many times. But all the solutions I've tried (mostly be sure that the log4j.properties file is in the correct location and it correctly typed) do not work in my case.
I have a maven project. I would like to use log4j for my tests. The test class uses a helper method defined in src/main/java where the logger is used.
In my helper class (in src/main/java/) I've imported
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
and I have instantiated the logger
private static final String TAG = Helper.class.getSimpleName();
private static final Logger logger = LoggerFactory.getLogger(TAG);
I've included both in src/main/resources and src/test/resources the following log4j.properties file
### set log levels - for more verbose logging change 'info' to 'debug' ###
### Also add logfile to the root, if need stdout then add stdout appender here###
log4j.rootLogger=debug, stdout
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{dd-mm HH:mm:ss,SSS} %p/%c{1}:%L - %m%n
In my POM I've included the dependency on slf4j
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
In the code in my helper class I use the logger in this way
logger.debug("logger test...");
No messages are printed in the console and I get the following warning message
log4j:WARN No appenders could be found for logger (Helper).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
What am I missing?
Update
The issue was related to a project option that sets the log4j.configuration property to log4j-test.xml. I've added the following plugin to the project maven pom and this fixed the issue.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<systemPropertyVariables>
<log4j.configuration>log4j.properties</log4j.configuration>
</systemPropertyVariables>
</configuration>
</plugin>
From the parts you have posted it looks ok. So let's investigate the issue. I did a simple project (only 2 classes Helper: has a method to call your logger statement, AppTest: a JUnit test to call the method in Helper)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With