Presents your JAVA E-NEWSLETTER for April 21, 2003 <-------------------------------------------> DEBUG WITH JAVA'S LOGGING PACKAGE Java 1.4 introduced the logging package java.util.logging. If you've used the popular logging framework log4j, then Java's built-in logging will be familiar territory. You can begin using logging with no setup. The simplest way to do this is to create an instance of java.util.logging.Logger. Once you have a Logger instance, you can start logging. Below is a complete example of a logging class: package tips; import java.util.logging.Logger; public class LogTip { // create an instance of the Logger class private static Logger log = Logger.getLogger("tips.LogTip"); public static void main(String args[]) { log.finest("the finest message"); log.finer("finer message"); log.fine("a fine message"); log.config("some configuration message"); log.info("a little bit of information"); log.warning("a warning message"); log.severe("a severe message"); } } If you compile and run this class, you should get console output like this: Apr 1, 2003 11:09:05 PM tips.LogTip main INFO: a little bit of information Apr 1, 2003 11:09:05 PM tips.LogTip main WARNING: a warning message Apr 1, 2003 11:09:05 PM tips.LogTip main SEVERE: a severe message Not all of the log entries make it to the console because Java's logging package makes use of log levels. A log level allows you to control logging output. During development, you want to output plenty of debug information so you can see what's going on in the application. In production, you probably want to suppress debug messages but not errors. The logging package gives you this control through the use of logging levels. There are seven hierarchal levels of logging defined in the java.util.logging.Level class: SEVERE, WARNING, INFO, CONFIG, FINE, FINER, and FINEST. The SEVERE level is for errors and catastrophic application events. The FINE, FINER, and FINEST levels are for information that is less important and often used for debugging. In addition, you can set the level to its extreme value by setting the logging level to ALL or OFF. When you set the level to ALL, no log entries are suppressed. When you set the level to OFF, all entries are suppressed. Because logging levels are hierarchal, when you set logging to a certain level, only that level and higher are logged. SEVERE is the highest priority level and FINEST is the lowest. The default log level is INFO; that's why we see only the last three log messages in the code above. To see all of the messages, we'll need to make some configuration changes. There are a number of ways to modify or replace the default configuration including by properties file, programmatically at runtime, or by supplying your own configuration class. When you use a properties file to control logging, you can either use the default properties file (which is not recommended), %YOUR_JRE_PATH%/lib/logging.properties, or you can supply your own properties file by setting the system property java.util.logging.config.file to the name of the properties file you want to use, like this: java -Djava.util.logging.config.file=log.properties tips.LogTip Now logging properties will be read from the log.properties file, which in this case is a modified version of the default logging.properties file, as shown below: handlers= java.util.logging.ConsoleHandler .level=FINEST java.util.logging.ConsoleHandler.level = FINEST java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter Now when the class is run, the output looks like this: Apr 1, 2003 11:12:00 PM tips.LogTip main FINEST: the finest message Apr 1, 2003 11:12:00 PM tips.LogTip main FINER: finer message Apr 1, 2003 11:12:00 PM tips.LogTip main FINE: a fine message Apr 1, 2003 11:12:00 PM tips.LogTip main CONFIG: some configuration message Apr 1, 2003 11:12:00 PM tips.LogTip main INFO: a little bit of information Apr 1, 2003 11:12:00 PM tips.LogTip main WARNING: a warning message Apr 1, 2003 11:12:00 PM tips.LogTip main SEVERE: a severe message In the real world, you'll probably want to exert more control over logging. You'll want some classes logging at FINEST, others at SEVERE, and some not at all. You'll want to log some information to the console, some to file, and maybe some to a database. ----------------------------------------