If you are reading this, you must be using log4j framework and got below error message.

Log4j warning for no appenders will come to console for standalone applications and in server logs for web applications running into some servlet container such as Tomcat or JBoss.

There could be multiple reasons for log4j warning no appenders found, let’s look at common reasons and how to fix them.

  1. log4j.xml or log4j.properties file are not in classpath

This is the most common reason, log4j framework look for log4j.xml or log4j.properties file in the classpath of your application. If you have a maven based application, you can create a source folder and put the configuration files there, as shown in below image.

log4j-warn-no-appender-could-be-found

If you are using log4j in web applications then check for this configuration file at WEB-INF/classes directory. If it’s not there then check that your source folders are properly configured.

  1. log4j configuration file name is something different

Log4j looks for standard file names, so if your log4j configuration file name is myapp-log4j.xml or myapp-log4j.properties then log4j won’t be able to load it automatically and you will get standard warning message.

For standalone java application, you can fix it easily through configuring it inside main method before using it. For example;

But you can’t use this simple approach with web application, because the configuration file is inside WEB-INF/classes directory. You can do this configuration through ServletContextListener as shown in below code.

Another approach is to set the log4j.configuration system property through java options like below.

Log4j framework is smart enough to use DOMConfigurator or PropertyConfigurator based on the file extension.

That’s all, log4j is simple to use once you get hold of these initial issues.

References:

By admin

Leave a Reply