Getting Started with log4j (CISC474, Spring 2006)

log4j is a way to assist with debugging and logging in Java applications. Simply put, it is an alternative to putting system.out.print() statements in your code, and having to take them out later—only to have to put them back in when the darn thing breaks again.

Quick Start with log4j on strauss and porsche

The following is adapted from the INSTALL file that comes with the log4j distribution (tailored to strauss and porsche).

  1. Define the environment variable LOG4J_HOME in your .localenv file on strauss, or your .cshrc file on porsche (or if you use a shell other then csh/tcsh, the equivalent file for your shell).

  2. Add $LOG4J_HOME/dist/lib/log4j-1.2.13.jar to your CLASSPATH

  3. You can now test your installation by first compiling the simple program Hello.java below. After compilation, try it out by issuing the command
          java Hello
              

    You should see log statements appearing on the console.

    	// Hello.java   from the INSTALL file for log4j
    
        import org.apache.log4j.Logger;
        import org.apache.log4j.BasicConfigurator;
        
        public class Hello {
        
          static Logger logger = Logger.getLogger(Hello.class);
        
          public
          static
          void main(String argv[]) {
            BasicConfigurator.configure();
            logger.debug("Hello world.");
            logger.info("What a beatiful day.");
          }
        }
              

    Here is an example of output on porsche:

    porsche[88] > javac Hello.java 
    porsche[89] > java Hello
    1 [main] DEBUG Hello  - Hello world.
    4 [main] INFO Hello  - What a beatiful day.
    porsche[90] > 
    

Going further with log4j

See the file topics/java/log4j/usefulLinks.html for more information.

Using Jakarta Commons Logging

If you are using libraries from the Apache Jakarta project, you may need to use the Jakarta Commons Logging API.

This is, according to the web page for the Jakarta Commons Logging project, an "an ultra-thin bridge between different logging implementations."

It solves the problem that log4j is not the only logging package out there for Java, and so if you are writing a general purpose library that needs to do its own logging, you can't know which logger your users are going to pick.

According to the quick start section of the documentation, all you should need to do to use Jakarta Commons Logging with log4j is to have both the jar file for log4j and the jar file for Jakarta Commons Logging in your path.

The jar files for Jakarta Commons Logging is in the following locations:


Valid XHTML 1.1 Valid CSS!