Tutorial

Fix for log4j WARN No appenders could be found for logger, Please initialize the log4j system properly

Published on August 3, 2022
author

Pankaj

Fix for log4j WARN No appenders could be found for logger, Please initialize the log4j system properly

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

log4j:WARN No appenders could be found for logger
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See https://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

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 appenders 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;

/**
 * method to init log4j configurations, should be called first before using logging
 */
private static void init() {
	DOMConfigurator.configure("myapp-log4j.xml");
	// OR for property file, should use any one of these
	//PropertyConfigurator.configure("myapp-log4j.properties");
}

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.

public final class Log4jInitListener implements ServletContextListener {
 
    public void contextDestroyed(ServletContextEvent paramServletContextEvent)  { 
    }
 
    public void contextInitialized(ServletContextEvent servletContext)  { 
        String webAppPath = servletContext.getServletContext().getRealPath("/");
    String log4jFilePath = webAppPath + "WEB-INF/classes/myapp-log4j.xml";
        DOMConfigurator.configure(log4jFilePath);
        System.out.println("initialized log4j configuration from file:"+log4jFilePath);
    }
     
}

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

-Dlog4j.configuration=file:///path/to/your/myapp-log4j.xml

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:

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
October 10, 2018

We are using log4j2.xml file in our web application and those are present in WEB-INF/classes location. But we are still getting this issue in our Tomcat logs. Though it is not a showstopper still I’m curious to know why are we getting this and how can it be permanently fixed.

- TejaMiryala

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    June 14, 2018

    What if you use log4j 2? and not log4j 1.2

    - mkrawetko

      JournalDev
      DigitalOcean Employee
      DigitalOcean Employee badge
      December 15, 2017

      je ne comprends ///path/to/your/myapp-log4j.xml??Pourriez-vous m’expliquer??

      - salif

        Try DigitalOcean for free

        Click below to sign up and get $200 of credit to try our products over 60 days!

        Sign up

        Join the Tech Talk
        Success! Thank you! Please check your email for further details.

        Please complete your information!

        Become a contributor for community

        Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

        DigitalOcean Documentation

        Full documentation for every DigitalOcean product.

        Resources for startups and SMBs

        The Wave has everything you need to know about building a business, from raising funding to marketing your product.

        Get our newsletter

        Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

        New accounts only. By submitting your email you agree to our Privacy Policy

        The developer cloud

        Scale up as you grow — whether you're running one virtual machine or ten thousand.

        Get started for free

        Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

        *This promotional offer applies to new accounts only.