1

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.

saplingPro
  • 20,769
  • 53
  • 137
  • 195

3 Answers3

1

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);
Shiva Kumar
  • 3,111
  • 1
  • 26
  • 34
0

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

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

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.

jmehrens
  • 10,580
  • 1
  • 38
  • 47