Tips Tricks Samples

Log4J logging to the console

I'm conscious that technical posts have been notable by their absence here recently, but it's just the nature of what I'm doing right now. Anyway I'm not sure this counts but it's caused me to waste half an hour searching for it.

Log4J is an excellent Java logging solution (if you're not using then I highly recommend it). The only problem I've had is that when writing test classes I get an error "log4j:WARN No appenders could be found for logger", so, how to get the logging output to be sent to the console in MyEclipse? It's a very simple problem but is something I always forget. Well, all you need to do is add one line at the start of the main method:


So your test class will look like this:
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

public class MyClass{
   static Logger logger = Logger.getLogger(MyClass.class);
   public static void main(String[] args) {
      logger.debug("This will appear in your console now");

Next week I'll be teaching you how to suck eggs!