What is the correct way of logging in an xml from two different classes ? For now I get an error in the xml produced

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-28 10:37:16

问题


By default the code :

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();
  }
}

produces a xml of the type :

<?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>

But if I try to append to the xml produced above, by the following code :

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();
    }
}

}

it appends the following to the previous xml produced :

<?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>

and when I try opening this xml in the browser,I get the following error :

This page contains the following errors:

error on line 27 at column 6: XML declaration allowed only at the start of the document

What do I do to avoid the statements :

<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">

two times ? I want xml with just the log tag appended to the end of the xml.


回答1:


The easies way to fix it is to configure FileHandler to generate a new file name each time you start your app, e.g.

new FileHandler("log%g.xml", 100000000, 10);

see API for details




回答2:


If you want to avoid xml headers completely in your log file, you can try this in your code

    FileHandler fHandler = new FileHandler("LOGGED.xml", true);
    fHandler.setFormatter(new XMLFormatter() {
        @Override
        public String getHead(Handler h) {
            return "";
        }
    });
    logger.addHandler(fHandler);



回答3:


The proposed patch is listed in RFE JDK-4629315 : Appending of XML Logfiles doesn't merge new records. You might be able to extend XMLFormatter and override the getHead method to condtionally write the default head value or the empty string based off of the size (empty or full) of the target file. Assuming you can compute the current file being used for logging.



来源:https://stackoverflow.com/questions/14669103/what-is-the-correct-way-of-logging-in-an-xml-from-two-different-classes-for-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!