Gathering information - logging - with log4j. First steps.
Generating information about processes as they run - who has visited a server, where orders have been received from, noting which requests have been slow - are all vital elements of debugging and monitoring processes. And this information gathering all comes under the title of "logging".
Anyone who's been programming for a while will probably have opened / written to log files. Will probably have appended to such files. Will probably have had to clear out such files as they got rather large. May have had to synchronise several user's data to the same log file, and almost certainly will have wished to be able to turn the amount of logging up and down without rebuilding the application. And all of these things can
be handled by your own code.
But why re-invent the wheel when someone else has already invented a wheel that does everything you want - and much more besides. Not only are you saved the development and debug time, but you're also saved potential ongoing enhancement needs over the years as you can rely on the expert team who already know a lot about wheels.
is such a logging system. It provides an ability to log direct to the use, to file, to a "rolling file", and much more. It provides for multiple logging streams, multiple logging levels, audit information to be added by the logger, and so much more that ... you may say "goodness - I don't know where to start"
the first time you see it. Or you may get a message like this:
wizard:jn10 graham$ java Thumb
This is a bit trivial
log4j:WARN No appenders could be found for logger (DemoLogger.class).
log4j:WARN Please initialize the log4j system properly.
So is this
So here - to get you started - are two really easy examples
Before you compile / these - download log4j from [here]
and add the jar file to your CLASSPATH. That will give to access to the code in log4j.
example 1 (Hello logger's world!)
- here's the code that you need :
Gives access to the classes in log4j
static Logger logger = Logger.getLogger("Thumb.class");
Sets up the elements of a logger
Does the basic configuration stuff - steps past that earlier error message, for example
logger.debug("This is a debug message");
Produces a log line.
There's complete source code [here]
- including an output sample.
example 2 (logging controlled by a properties file)
- basically the same code (that's the beauty of logging - once you have said what you might want to log, what's actually done and produced is controlled without changing the Java!
... Add a log4j.properties file where it will be found.
There's complete source code [here]
- including a configuration file and an output sample.
You're now placed to make rather more of some of the documentation - see [here]
- or to continue experimenting if that's more your way of doing things.
Log4j is the chosen logging file of the Apache Tomcat server ... (written 2010-11-12)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesA506 - Web Application Deployment - Java - Installing and Deploying optional Technologies 
Jmeter - a first test case - (2009-03-14) 
Connecting jconsole remotely - the principles - (2009-03-14) 
Java - Memory Allocation and garbage collection - (2009-03-14) 
Increasing Java Virtual Machine memory for Tomcat - (2008-07-24)A654 - Web Application Deployment - Configuring and Controlling Tomcat 
Reading and writing cookies in Java Servlets and JSPs - (2010-02-26) 
CATALINA_OPTS v JAVA_OPTS - What is the difference? - (2009-05-09) 
Tomcat 6 - Annotated Sample Configuration Files - (2009-03-01) 
The Invoker - (2009-02-13) 
tomcat-users.xml; what a difference a space made - (2009-01-16) 
Port and Glasses - (2008-12-14) 
WEB-INF (Tomcat) and .htaccess (httpd) - (2008-08-20) 
Automatic startup and shutdown of Tomcat - (2008-02-24) 
Web page (http) error status 405 - (2008-01-12) 
Apache Tomcat Performance Tuning - (2007-09-29) 
Compressing web pages sent out from server. Is it worth it? - (2007-09-14) 
A practical example of roles - (2006-11-04) 
Browser -> httpd -> Tomcat -> MySQL. Restarting. - (2006-10-28) 
Tomcat - Shutdown port - (2006-08-18)J606 - Java Roadmap - Beyond the Fundamentals 
Java Tag Libraries / how they work / Tomcat Deployment - (2009-02-11) 
Java - Generics - (2008-11-27) 
Java Compiler - needs all classes, compiles them all too - (2008-03-03) 
Java 6, Apache Tomcat 6. - (2007-01-21)
Some other Articles
What is a universal superclass? Java / Perl / Python / Other OO languagesJava Beans, tag libraries and JSPs - what and why.After Course Resources - do we publish sample answers. Example from Java Exceptions module.Changing a Servlet - more that just editing and compilingGathering information - logging - with log4j. First steps.Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in PerlJava - basic rules for arithmetic, variables and conversionDoing several things at the same time - Java threadsFresh Paint - Java ArraysSetting up individual variables, and arrays, in Java - some commented examples