Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
For 2023 - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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.

Apache log4j 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
  wizard:jn10 graham$

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.
  export CLASSPATH=log4j-1.2.9.jar:.

example 1 (Hello logger's world!) - here's the code that you need :

  import org.apache.log4j.*;
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 articles
J606 - Java Roadmap - Beyond the Fundamentals
  [1049] Java 6, Apache Tomcat 6. - (2007-01-21)
  [1562] Java Compiler - needs all classes, compiles them all too - (2008-03-03)
  [1910] Java - Generics - (2008-11-27)
  [2036] Java Tag Libraries / how they work / Tomcat Deployment - (2009-02-11)
  [4305] Learning to program in Java - yes, we can help. - (2014-09-26)

A654 - Web Application Deployment - Configuring and Controlling Tomcat
  [837] Tomcat - Shutdown port - (2006-08-18)
  [907] Browser -> httpd -> Tomcat -> MySQL. Restarting. - (2006-10-28)
  [914] A practical example of roles - (2006-11-04)
  [1351] Compressing web pages sent out from server. Is it worth it? - (2007-09-14)
  [1370] Apache Tomcat Performance Tuning - (2007-09-29)
  [1503] Web page (http) error status 405 - (2008-01-12)
  [1553] Automatic startup and shutdown of Tomcat - (2008-02-24)
  [1762] WEB-INF (Tomcat) and .htaccess (httpd) - (2008-08-20)
  [1943] Port and Glasses - (2008-12-14)
  [1994] tomcat-users.xml; what a difference a space made - (2009-01-16)
  [2039] The Invoker - (2009-02-13)
  [2061] Tomcat 6 - Annotated Sample Configuration Files - (2009-03-01)
  [2163] CATALINA_OPTS v JAVA_OPTS - What is the difference? - (2009-05-09)
  [2652] Reading and writing cookies in Java Servlets and JSPs - (2010-02-26)

A506 - Web Application Deployment - Java - Installing and Deploying optional Technologies
  [1718] Increasing Java Virtual Machine memory for Tomcat - (2008-07-24)
  [2079] Java - Memory Allocation and garbage collection - (2009-03-14)
  [2081] Connecting jconsole remotely - the principles - (2009-03-14)
  [2082] Jmeter - a first test case - (2009-03-14)

Back to
Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl
Previous and next
Horse's mouth home
Forward to
Changing a Servlet - more that just editing and compiling
Some other Articles
What is a universal superclass? Java / Perl / Python / Other OO languages
Java 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 compiling
Gathering information - logging - with log4j. First steps.
Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl
Java - basic rules for arithmetic, variables and conversion
Doing several things at the same time - Java threads
Fresh Paint - Java Arrays
Setting up individual variables, and arrays, in Java - some commented examples
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page

This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2023: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3043_Gat ... teps-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb