log4jInserting log statements into your code is a low-tech method for debugging it.
Very useful if you are working on developing distributed applications because there you don't always have debuggers available.
With log4j it is possible to enable logging at runtime without modifying the application binary.
Logging behavior can be controlled by editing a configuration file, without touching the application binary.
If you don't want to use external files to do the logging configuration, then you can always write all of the params in the code but that means you also would have to compile every time you change something to the logging parameters.
Logging is an effective tool to help you in testing your code.
More information on log4j, you can find on:
http://logging.apache.org/log4j/docs/http://en.wikipedia.org/wiki/Log4jI created this example last year so that some people in our company would get the basics of log4j and would finally stop using those pesky System.out.print statements.

Why would it be better to use logging instead of System.outs?
Using System.out is ok but it once the app is moved to production, it is no longer necessary (most of the time) to log all this information to the console. That would mean that you would have to go through the entire code and remove or comment the System.out lines.
If you use log4j and then you can enable/disable the logging in the configuration file or in the java code in one place instead of going through all the code to make the necessary changes.
A simple example where log4j is used in the code is the following:
CODE
/**
* <P>
* Description: Example of how to use Log4j with configuration in the Java file.
* </P>
* @author Kevin
* @version 02/08/2006
*/
package com.be.Actas.Samples.Log4J;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
/**
* @author Kevin
*/
public class Log4JExample {
private static Logger logger = Logger.getLogger(Log4JExample.class); //Identifying your logger for this class.
/**
* @param args
* @author Kevin
*/
public static void main(String[] args) {
logger.setLevel(Level.INFO); //Setting level of logging, there are 6 logging levels in total.
logger.addAppender(new ConsoleAppender(new SimpleLayout())); //Say how to log. this goes to console in simple layout.
logger.info("Log4j Logging is working..."); //INFO logging statement
logger.debug("This shouldn't be shown because logging level is too low in properties file."); //another logging statement; this one should be invisible because its logging level is lower (DEBUG) than the level we have set earlier in the code.
}
}
Output:
The appender used in the example is writing to the console. So there is no log file used here. The format of the output is also in simple mode. It is possible to format your output, you can find a more "complex" format in the configuration file (log4j.properties) in the attached zip file.
Logging:
6 Logging levels, from high to low they are:
* FATAL
* ERROR
* WARN
* INFO
* DEBUG
* TRACE
So if you would set the level on INFO. That means that everything above info level or info level itself will be logged, the rest will be ignored.
Attached to this tutorial you will also find an Eclipse project.
In this project you will find another Java example that uses an external configuration file.
Personally I find it a huge improvement over using System.out.println to do your testing and then in the end going through your code to turn those statements off, you just change a param in the config file of log4j and no more logging.

If you take a look at the other example and you need more information about the configuration file or the java code, let me know and I'll try to explain. Or you can always google it.
I know that example isn't as documented as it could be. If I find the time in the next coming days/weeks, I'll try to update it.
Hopefully this has clarified a bit about log4j.
NOTE: This is my first tutorial here and if I forgot to explain some things, let me know and I'll edit this post and improve it...
Cheers!
This post has been edited by AngeluS: 18 Apr, 2007 - 09:45 AM