Categories
Deposit-friendly Development | Home | Using ColdFusion and LCDS in AIR-powered Flex Applications
Filed under Quick Tips on June 11, 2008 by Doug BurnsLayers of Java Logging
The Java Logging API is great for tracking the internal workings of your application, both during development and after it goes to production. The API is based on a highly flexible configuration model that gives you complete control over what gets logged and where it gets logged.
I recently started using the API after some time away, and it took a bit to get reacquainted with some of its concepts. In my first few attempts, I ended up with either all logging statements output or none, when what I really wanted was output from only a few specific classes.
I think my problem stemmed from confusion between the various ways to filter output. In the end, I found it easiest to think of the API as two separate layers - a filtering layer that answers the question "What should be output?" and a handler layer that answers the question "How should it be output?"
Below is an example configuration divided into two sections. Although they both end up in a single logging.properties file, I've broken them up to demonstrate the concept of separate layers. In the first, I define a default filter level for the entire application, as well as custom filter levels for select packages and classes.
Filter Layer
# Set the global filtering level to INFO. By default all statements below the # INFO level will not be logged. .level = INFO # Set the filtering level for a few packages to ALL. This overrides the # global level, so all statements in these packages will be output. # This is good for limiting output to a section of code you're currently # working with. com.arc90.package1.level = ALL com.arc90.package2.level = ALL # Set the filtering level for a few individual classes. Again, this overrides # less specific settings. In this case, all statements of level FINE and # higher in ClassA will be output, and all statements of level FINER and # higher in ClassB will be ouput. com.arc90.package3.ClassA.level = FINE com.arc90.package3.ClassB.level = FINER
Next, I'll define the handlers that will be used. In addition, I'll specify filters for the handlers themselves. This performs high level filtering on the final output from the filtering layer. Here, I say that I want only INFO level and higher statements sent to the console, and all statements sent to the log file. The important thing to remember is that these levels apply to the output from the filter layer only - if statements were already filtered out in that layer, they will never make it this far.
Handler Layer
# Specify the handlers to be used. Multiple handlers can be specified # in a comma separated list. handlers = java.util.logging.ConsoleHandler,java.util.logging.FileHandler # Specify the formatters that will be used for each handler java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter # Specify the log file location for the FileHandler. In this case the log will # be written to my home directory, in a file named example.log. java.util.logging.FileHandler.pattern = "%h/example.log" # Finally, specify the filtering level for each handler java.util.logging.ConsoleHandler.level = INFO java.util.logging.FileHandler.level = ALL
Thinking of the Java Logging API this way has helped to clear things up for me, and I hope it will for others as well. If you haven't used the API, you can try it out for yourself by dropping the above text into a logging.properties file (make sure there aren't spaces at the end of the lines or it won't work), which must be palced into your JVM's lib directory or a path specified as a -Djava.util.logging.config.file VM argument. Finally, just add some logging statements to your Java code and you should start seeing output to the console and the log file.
Comments
Deposit-friendly Development | Main | Using ColdFusion and LCDS in AIR-powered Flex Applications

Very useful, thanks!
Posted on June 13, 2008 3:16 PM by Avi Flax