|
Running the code as shown in Listing 9-1 will produce two different output streams. First, System.out (normally, the console) will produce the output as shown in Listing 9-2. Listing 9-2. Logging ExampleAdded JDK 1.4 file handler Test fatal log Mar 22, 2004 9:04:06 PM com.cascadetg.ch09.LogGenerator main SEVERE: null java.lang.NumberFormatException: null at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.<init>(Unknown Source) at com.cascadetg.ch09.LogGenerator.main(LogGenerator.java:44) Test error log Mar 22, 2004 9:04:06 PM com.cascadetg.ch09.LogGenerator main SEVERE: null java.lang.NullPointerException at com.cascadetg.ch09.LogGenerator.main(LogGenerator.java:56) Test warn log Mar 22, 2004 9:04:06 PM com.cascadetg.ch09.LogGenerator main WARNING: Can't find a non-existant class! Test info log Mar 22, 2004 9:04:06 PM com.cascadetg.ch09.LogGenerator main INFO: Starting app! Mar 22, 2004 9:04:06 PM com.cascadetg.ch09.LogGenerator main INFO: Quitting app! Test debug log Test trace log Log test complete. You'll notice that the output written to System.out does not contain all of the messagesthe debug and trace level information is not sent to the console. Instead, the remaining data is written to disk. By default, JDK 1.4 will write to a file called java0.log in your home directory (~ on UNIX systems, or C:\Documents and Settings\User Name on Windows 2000/XP). The default file output for JDK 1.4 is an XML file, as shown in Listing 9-3 (only one record is shown for brevity). Listing 9-3. Detailed XML Logging Output<?xml version="1.0" encoding="windows-1252" standalone="no"?> <!DOCTYPE log SYSTEM "logger.dtd"> <log> <record> <date>2004-03-22T21:04:06</date> <millis>1080018246773</millis> <sequence>0</sequence> <logger>com.cascadetg.ch09.LogGenerator</logger> <level>SEVERE</level> <class>com.cascadetg.ch09.LogGenerator</class> <method>main</method> <thread>10</thread> <message>null</message> <exception> <message>java.lang.NumberFormatException: null</message> <frame> <class>java.lang.Integer</class> <method>parseInt</method> </frame> <frame> <class>java.lang.Integer</class> <method><init></method> </frame> <frame> <class>com.cascadetg.ch09.LogGenerator</class> <method>main</method> <line>44</line> </frame> </exception> </record> <record> ... </record> </log> To view the XML file in a validating browser or parser, you'll need a DTD file (or else just delete the <!DOCTYPE log SYSTEM "logger.dtd"> declaration at the top of the file). You can find the contents of this file at http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html#3.0simply copy the text of the DTD into a file called logger.dtd and place it in the same directory as the log file. Figure 9-1 shows the resulting log file. Figure 9-1. XML logging output in a browser.
As you can see, the generated XML file contains all of the data from the application's run, including debug and trace data. These log files can quickly become large, but JDK 1.4 includes provisions for automatically closing and rotating files as needed. It's easy to imagine combining these resulting log files with more sophisticated XML parsing and retrieval tools, such as JXPath and XPath, as described in Chapter 8, "JXPath." As a final note, significant effort is made to minimize the impact of logging statements on the performance of your application code. Assuming you follow proper rules for Java execution flow (in particular, avoiding the use of exceptions as a form of program flow control), your logging statements should have minimal impact on your application's performance. |
|