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.
The following is adapted from the INSTALL file that comes with the log4j distribution (tailored to strauss and porsche).
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).
For strauss, use: /www/htdocs/CIS/software/dist/logging-log4j-1.2.13
On porsche, use: /porsche/cisc474/logging-log4j-1.2.13
Add $LOG4J_HOME/dist/lib/log4j-1.2.13.jar
to your CLASSPATH
For example, the following code will work for csh/tcsh startup files:
if ( -d /porsche/cisc474/logging-log4j-1.2.13 ) then
setenv LOG4J_HOME /porsche/cisc474/logging-log4j-1.2.13
if ( -r ${LOG4J_HOME}/dist/lib/log4j-1.2.13.jar ) then
setenv CLASSPATH ${CLASSPATH}:${LOG4J_HOME}/dist/lib/log4j-1.2.13.jar
endif
endif
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] >
See the file topics/java/log4j/usefulLinks.html for more information.
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: