Logging to Files


Sun designed the logging facility to allow you to change logging characteristics at runtime. You can quickly route log messages to a file instead of to the console without having to change, recompile, and redeploy code.

The default behavior is to send logging messages to the console. This behavior is not hardcoded somewhere in the Logger class; it is located in an external properties file that you can freely edit.

Browse to the directory in which you installed Java. Within this directory, you should be able to navigate into the subdirectory jre/lib (or jre\lib if you are using Windows). Within this subdirectory you should see the file logging.properties.[8] Edit the file using any editor.

[8] If you don't have this file, you can create it. Use the bits of content that I demonstrate.

You can use properties files in many circumstances to store values that you may want to change with each execution of an application. Sun chose to allow you the ability to configure logging behavior using a properties file. The layout of a properties file should be self explanatory. Comment lines begin with the pound sign (#). Blank lines are ignored. The remainder of the lines are key-value pairs, like entries in a hash table. Each key-value pair, or property, appears in the form:

 key = value 

Several lines down into logging.properties, you should see:

 # "handlers" specifies a comma separated list of log Handler # classes. These handlers will be installed during VM startup. # Note that these classes must be on the system classpath. # By default we only configure a ConsoleHandler, which will only # show messages at the INFO and above levels. handlers= java.util.logging.ConsoleHandler # To also add the FileHandler, use the following line instead. #handlers=java.util.logging.FileHandler,java.util.logging.ConsoleHandler 

Swap the commenting around. Comment out the line that sets handlers to only the ConsoleHandler. Uncomment the line that sets the property handlers to both the FileHandler and the ConsoleHandler. By doing so, you tell the logger to route output to objects of each handler type.

Nearer to the bottom of logging.properties, you will find these lines:

 java.util.logging.FileHandler.pattern = %h/java%u.log java.util.logging.FileHandler.limit = 50000 java.util.logging.FileHandler.count = 1 java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter 

The logging facility uses these lines to determine how the FileHandler works.

The value for the java.util.logging.FileHandler.pattern is a pattern for naming the log file. The %h and %u that currently appear in the pattern are known as fields. The %h field tells the FileHandler to store log files in your home directory.

The file handler replaces the %u field in the pattern with a unique number. The goal is to avoid the conflict of two log files sharing the same filename.

Rerun your tests and then navigate to your home directory. Under most versions of Windows, you can do so using the command:[9]

[9] You must change to the drive on which the user profile is located. You can view the full path of your home directory, including the drive on which it is located, by executing set USERPROFILE.

 cd %USERPROFILE% 

Under most Unix shells you can navigate to your home directory using the command:

 cd $HOME 

Within your home directory, you should see a file named java0.log. The number 0 is the unique number replacement for the %u field.

View the contents of java0.log. They should look similar to this:

 <?xml version="1.0" encoding="windows-1252" standalone="no"?> <!DOCTYPE log SYSTEM "logger.dtd"> <log> <record>   <date>2004-04-15T03:27:05</date>   <millis>1082021225078</millis>   <sequence>0</sequence>   <logger>sis.studentinfo.Student</logger>   <level>INFO</level>   <class>sis.studentinfo.Student</class>   <method>log</method>   <thread>10</thread>   <message>Student name 'a b c d' contains more than 3 parts</message> </record> </log> 

It's not the two lines of logging message you expected. Instead, the message appears in XML format.[10] The logging facility allows you attach a different formatter to each handler. A formatter is a subclass of java.util.logging.Formatter; Sun supplies two formatter implementations. SimpleFormatter produces the two log lines you saw earlier. XMLFormatter produces the output you're currently seeing in java0.log. You can code your own formatter class to produce log output however you wish.

[10] eXtensible Markup Language. See http://www.w3.org/XML/.

Find the java.util.logging.FileHandler.formatter property in logging.properties. Its current value is java.util.logging.XMLFormatter. Change the value to java.util .logging.SimpleFormatter and rerun your tests.

The contents of java0.log should now look the same as the output produced by the ConsoleHandler.



Agile Java. Crafting Code with Test-Driven Development
Agile Javaв„ў: Crafting Code with Test-Driven Development
ISBN: 0131482394
EAN: 2147483647
Year: 2003
Pages: 391
Authors: Jeff Langr

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net