Java Reference
In-Depth Information
There should also be a temp.log file in the current working directory, which contains the
log message in XML format:
C:\Temp>dir temp.log
Volume in drive C has no label.
Volume Serial Number is E0F1-2766
Directory of C:\Temp
11/12/2004 07:22p 388 temp.log
1 File(s) 388 bytes
0 Dir(s) 2,066,956,288 bytes free
C:\Temp>type temp.log
<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
<date>2004-12-11T19:22:40</date>
<millis>1102753360550</millis>
<sequence>0</sequence>
<logger>Test</logger>
<level>SEVERE</level>
<class>TestLogging</class>
<method>main</method>
<thread>10</thread>
<message>My program did something bad</message>
</record>
</log>
When you have a program to analyze your log messages, you may find this useful. How-
ever, when you want to read the log messages yourself, you may find it more beneficial to have
the output in plain text. You can do this by adding a Formatter .
Changing the code to read:
import java.util.logging.*;
import java.io.IOException;
public class TestLogging {
public static void main(String[] args) throws IOException {
FileHandler myFileHandler = new FileHandler("temp.log");
myFileHandler.setFormatter(new SimpleFormatter());
Logger myLogger = Logger.getLogger("Test");
myLogger.addHandler(myFileHandler);
myLogger.severe("My program did something bad");
}
}
Search WWH ::




Custom Search