I have put log4j to my buildpath, but I get the following message when I run my application:
log4j:WARN No appenders could be found for logger (dao.hsqlmanag
I ran into this issue when trying to build an executable jar with maven in intellij 12. It turned out that because the java manifest file didn't include a class path the log4j properties file couldn't be found at the root level (where the jar file was executed from.)
FYI I was getting the logger like this:
Logger log = LogManager.getLogger(MyClassIWantedToLogFrom.class);
And I was able to get it to work with a pom file that included this:
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.2-beta-5</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mycompany.mainPackage.mainClass</mainClass>
</manifest>
<manifestEntries>
<Class-Path>.</Class-Path> <!-- need to add current directory to classpath properties files can be found -->
</manifestEntries>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
I had this problem too. I just forgot to mark the resources directory in IntelliJ IDEA
First import:
import org.apache.log4j.PropertyConfigurator;
Then add below code to main method:
String log4jConfPath ="path to/log4j.properties";
PropertyConfigurator.configure(log4jConfPath);
Create a file at path to and add the below code to that file.
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
Consider the log4j JVM argument Dlog4j.configuration
In general:
Add the JVM argument that points out the log4j config file. The syntax is just like follows:
java [ options ] -jar file.jar [ arguments ]
A sample of a real command line is just like the following:
java -Dlog4j.configuration=conf/log4j.xml -jar myJarFile.jar myArg1 myArg2
For IntelliJ IDE users:
1.Run/Debug Configurations
2.Edit configurations...
3.VM options
4.Enter the same value also starting with "-D"
Tips:
1.Eclipse IDE users will find an equivalent approach
2.For the run/debug configuration editor, it's very likely that, at the beginning of the times, your particular executable file is not there. Depending on the size of the project you're currently working on, it can be unpleasant to navigate directories to find it. It's less of a hassle if you just run/execute the file (click play) once before proceeding to run/debug config no matter what the execution result is.
3.Pay attention to your working directory, relative paths, and classpath.
The reason can be lack of the word static
in some:
final static Logger logging = Logger.getLogger(ProcessorTest.class);
If I make logger the instance field, I am getting exactly this very warning:
No appenders could be found for logger (org.apache.kafka.producer.Sender)
What is worse, the warning points not to ProcessorTest
, where the mistake lives, but to an absolutely different class (Sender) as a source of problems. That class has correct set logger and need not any changes! We could look for the problem for ages!
Most of the answers here suggested that log4j.properties
file be placed in the right location(for maven project, it should be located in src/main/resources
)
But for me, the problem is that my log4j.properties
is not correctly configured. Here's a sample that works for me, you can try it out first.
# Root logger option
log4j.rootLogger=INFO, stdout
# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n