java.util.logging.ConsoleHandler does not close properly and fails to write tail

XMLWordPrintable

    • Type: Bug
    • Resolution: Unresolved
    • Priority: P4
    • tbd
    • Affects Version/s: 6u24
    • Component/s: core-libs

      FULL PRODUCT VERSION :
      java version "1.6.0_24"
      Java(TM) SE Runtime Environment (build 1.6.0_24-b07)
      Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux mathilda 2.6.35-28-generic #49-Ubuntu SMP Tue Mar 1 14:39:03 UTC 2011 x86_64 GNU/Linux


      A DESCRIPTION OF THE PROBLEM :
      This is how java.util.logging.ConsoleHandler is implemented:

      public void close() {
      flush();
      }

      In particular, this does not call super.close(), which in turn calls flushAndClose(). This is, however important to ensure that formatter.getTail() is written properly.

      The fix is simple and obvious: call

      super.close();


      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Use the sample code below or setup any Java program with config file

      handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler
      java.util.logging.ConsoleHandler.formatter=java.util.logging.XMLFormatter
      java.util.logging.FileHandler.formatter=java.util.logging.XMLFormatter
      java.util.logging.FileHandler.pattern=log.xml

      Then just log something. The file will have the trailing </log>, the console will not.


      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      On both console and file:

      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <!DOCTYPE log SYSTEM "logger.dtd">
      <log>
      <record>
        <date>2011-03-30T12:03:33</date>
        <millis>1301479413194</millis>
        <sequence>0</sequence>
        <logger></logger>
        <level>INFO</level>
        <class>java.util.logging.LogManager$RootLogger</class>
        <method>log</method>
        <thread>10</thread>
        <message>Test</message>
      </record>
      </log>
      ACTUAL -
      In file (correct)

      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <!DOCTYPE log SYSTEM "logger.dtd">
      <log>
      <record>
        <date>2011-03-30T12:03:33</date>
        <millis>1301479413194</millis>
        <sequence>0</sequence>
        <logger></logger>
        <level>INFO</level>
        <class>java.util.logging.LogManager$RootLogger</class>
        <method>log</method>
        <thread>10</thread>
        <message>Test</message>
      </record>
      </log>

      In console (</log> missing):

      <?xml version="1.0" encoding="UTF-8" standalone="no"?>
      <!DOCTYPE log SYSTEM "logger.dtd">
      <log>
      <record>
        <date>2011-03-30T12:03:33</date>
        <millis>1301479413194</millis>
        <sequence>0</sequence>
        <logger></logger>
        <level>INFO</level>
        <class>java.util.logging.LogManager$RootLogger</class>
        <method>log</method>
        <thread>10</thread>
        <message>Test</message>
      </record>


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      public class LogTest {
      }import java.io.IOException;
      import java.util.logging.ConsoleHandler;
      import java.util.logging.FileHandler;
      import java.util.logging.Logger;
      import java.util.logging.XMLFormatter;

      public class LogTest {
      public static void main(String[] args) throws SecurityException, IOException {
      Logger rootLogger = Logger.getLogger("");

      ConsoleHandler consoleHandler = new ConsoleHandler();
      consoleHandler.setFormatter(new XMLFormatter());
      rootLogger.addHandler(consoleHandler);

      FileHandler fileHandler = new FileHandler("log.xml");
      fileHandler.setFormatter(new XMLFormatter());
      rootLogger.addHandler(fileHandler);

      rootLogger.info("Test");
      }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Make your own handler implementation

            Assignee:
            Unassigned
            Reporter:
            Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: