2 Replies Latest reply: Oct 20, 2010 2:29 PM by DrClap RSS

    java.util.logging.XMLFormatter - need advise

    806702
      Hi all,

      I have this class which suppose to write some message into the custom log file (in XML). Problem is, after the first time of writing it into the same file, it will append additional XML header :

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

      Worse, it will always append below.

      For example, the original XML file contains:
      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <!DOCTYPE log SYSTEM "logger.dtd">
      <log>
      <record>
        <date>2010-10-20T18:18:33</date>
        <millis>1287569913671</millis>
        <sequence>0</sequence>
        <logger>atm.controller.Log_Controller</logger>
        <level>INFO</level>
        <class>atm.controller.Log_Controller</class>
        <method>writeInfoLog</method>
        <thread>10</thread>
        <message>message inside thia standard data</message>
      </record>
      </log>
      when I write additional message into the same file using logger.info("some message"); on the second rounds, it will append additional XML header* info and become like this:
      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <!DOCTYPE log SYSTEM "logger.dtd">
      <log>
      <record>
        <date>2010-10-20T18:18:33</date>
        <millis>1287569913671</millis>
        <sequence>0</sequence>
        <logger>atm.controller.Log_Controller</logger>
        <level>INFO</level>
        <class>atm.controller.Log_Controller</class>
        <method>writeInfoLog</method>
        <thread>10</thread>
        <message>message inside thia standard data</message>
      </record>
      </log>
      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <!DOCTYPE log SYSTEM "logger.dtd">
      <log>
      <record>
        <date>2010-10-20T18:21:25</date>
        <millis>1287570085050</millis>
        <sequence>0</sequence>
        <logger>atm.controller.Log_Controller</logger>
        <level>INFO</level>
        <class>atm.controller.Log_Controller</class>
        <method>writeInfoLog</method>
        <thread>10</thread>
        <message>message inside thia standard data</message>
      </record>
      </log>
      --------------------------------------------------

      Below is the code.
       
      import java.io.IOException;
      import java.util.logging.FileHandler;
      import java.util.logging.Logger;
      import java.util.logging.XMLFormatter;
      
      /**
       *
       *
       */
      public class Log_Controller
      {
          private static Logger logger = Logger.getLogger( Log_Controller.class.getName() );
          private boolean allowAppend = true;
      
          public Log_Controller()
          {
              try
              {
                  XMLFormatter formatterTxt = new XMLFormatter();
                  FileHandler fileTxt = new FileHandler( "test.xml", allowAppend );
                  fileTxt.setFormatter( formatterTxt );
                  logger.addHandler( fileTxt );
      
              }
              catch ( IOException ioe )
              {
                  ioe.printStackTrace();
              }
          }
      
          public void writeInfoLog( String message )
          {
              logger.info("message inside thia standard data");  //THIS IS THE PART WHERE IT WRITE SOME MESSAGE INTO THE FILE
      //        logger.info( message );
          }
      
        
      }
      This is something new to me, appreciate if someone can point me what the mistake that I have done.
      Thanks thanks.

      Edited by: 803699 on 20-Oct-2010 04:15

      Edited by: 803699 on 20-Oct-2010 08:45