Use of BasicConfigurator in Log4j logging

If you are getting foregoing log4j warn debug info, that means you have not initialized log4j properly. So In this part of tutorial we will tell you how to configure log4j using BasicConfigurator.

Use of BasicConfigurator in Log4j logging

Use of BasicConfigurator in Log4j logging

     

log4j warning

log4j: WARN No appenders could be found for logger (org.activemq.transport.tcp.TcpTransportChannel).
log4j: WARN Please initialize the log4j system properly.

If  you are getting foregoing log4j warn debug info, that means you have not initialized log4j properly. So
In this part of tutorial we will tell you how to configure log4j using BasicConfigurator.

Now lets see when this log4j warning occurs. We are creating only SimpleLog file without configuring it as:

 

 

 

 

 

import org.apache.log4j.*;
public class SimpleLog {
  static Logger logger = Logger.getLogger("SimpleLog.class");
  public static void main(String[] args) {
  logger.debug("Hello world.");  
  }
}

After running the example we have got following log4j warning flashing on command prompt:

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

It is because we don't have configured Log4j. There are many different ways of configuring Log4j. One way is through BasicConfigurator. This can be done as follows:

import org.apache.log4j.*;
import org.apache.log4j.BasicConfigurator;
public class SimpleLog {
  static Logger logger = Logger.getLogger("SimpleLog.class");
  public static void main(String[] args) {
  BasicConfigurator.configure();
  logger.debug("Hello world.");  
  }
}

Now after running this code you will get the following output on your console:

Output:

0 [main] DEBUG SimpleLog.class - Hello world.

where "0" shows time taken in milliseconds from start of program to the logging request. "Hello world" is the message that we have given.
SimpleLog.class is the logger name and in bracket [main] is the thread who have invoked logging.

Download Source Code