I am trying to configure the logger in java to log my messages. I have downloaded and added the log4j.jar to my classpath. I am receving two error messages while I am running the program to check it.The code I am using is :

import org.apache.log4j.*;

public class LoggerClass{
public static Logger myLogger =
           Logger.getLogger(LoggerClass.class.getName( ));
    Appender myAppender;
    SimpleLayout myLayout;

public LoggerClass(){ 

    myLayout = new SimpleLayout();
    myAppender = new ConsoleAppender(myLayout);

public static void main(String args[])
 myLogger.info("This is info");

I am receiving the following error messages while trying to run the program :

log4j:WARN No appenders could be found for logger (LoggerClass).
log4j:WARN Please initialize the log4j system properly.

What could be the cause...help would be greatly appreciated?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Try adding the following to your constructor:

public LoggerClass(){ 

  myLayout = new SimpleLayout();
  myAppender = new ConsoleAppender(myLayout);

  myLogger.addAppender( myAppender );

If you want a file appender, you can add one of those like this

    RollingFileAppender fileAppender = new RollingFileAppender(new PatternLayout("%d: %m:  %l " + System.getProperty("line.separator") ), System.getProperty("user.dir") + java.io.File.separator +"application.log");  // can change the pattern and file location to whatever you need
    fileAppender.setThreshold(Priority.WARN); // if you want to set a threshold.
    myLogger.info("Started Rolling File Appender");

} catch (Exception ex){
  /* whatever you need here */

Or do the smart thing, and supply a configuration file for log4j as explained in the manual.
Far more flexible, and you can tune logging options without needing to recompile everything.

This article has been dead for over six months. Start a new discussion instead.