默认情况下,代码:
class Tester {
public static void main(String args[]) throws IOException{
Logger logger = Logger.getLogger(Tester.class.getName());
FileHandler fHandler = new FileHandler("LOGGED.xml",true);
logger.addHandler(fHandler);
logger.log(Level.INFO,"This is an info log message");
fHandler.close();
}
}
产生 axml
的类型:
<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
<date>2013-02-03T08:16:37</date>
<millis>1359859597763</millis>
<sequence>0</sequence>
<logger>Tester</logger>
<level>INFO</level>
<class>Tester</class>
<method>main</method>
<thread>1</thread>
<message>This is an info log message</message>
</record>
</log>
但是,如果我尝试通过以下代码附加到上面生成的xml中:
class Tester_1{
public static void main(String args[]) {
try {
Logger logger = Logger.getLogger(Tester_1.class.getName());
FileHandler fHandler = new FileHandler("LOGGED.xml",true);
logger.addHandler(fHandler);
logger.log(Level.INFO,"This is a custom message from my own formatter !");
fHandler.close();
}catch(Exception exc) {
exc.printStackTrace();
}
}
}
它将以下内容附加到先前生成的xml中:
<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
<date>2013-02-03T08:16:51</date>
<millis>1359859611306</millis>
<sequence>0</sequence>
<logger>Tester_1</logger>
<level>INFO</level>
<class>Tester_1</class>
<method>main</method>
<thread>1</thread>
<message>This is a custom message from my own formatter !</message>
</record>
</log>
当我尝试在浏览器中打开此xml时,出现以下错误:
This page contains the following errors:
error on line 27 at column 6: XML declaration allowed only at the start of the document
我该怎么做才能避免这些陈述:
<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
两次?我想要只在xmllog
末尾附加标签的 xml。