Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4874955

Clarification needed when overriding java.util.logging.Formatter.format

XMLWordPrintable

    • b03
    • x86
    • windows_xp
    • Not verified

        Name: rmT116609 Date: 06/05/2003


        FULL PRODUCT VERSION :
        java version "1.4.2-beta"
        Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2-beta-b19)
        Java HotSpot(TM) Client VM (build 1.4.2-beta-b19, mixed mode)

        FULL OS VERSION :
        Windows XP Professional Version 2002 Service Pack 1

        A DESCRIPTION OF THE PROBLEM :
        I am trying to customize the text actually written out to the
        log by subclassing SimpleFormatter and overriding format and
        formatMessage. Neither works correctly: if I override the latter
        I get two printouts for every message whereas if I override the
        former I get a printout that does not look at all like what it
        should be.

        The behaviour in the case of format overriding is especially
        puzzling: if I understand this correctly, it performs the
        final message processing and the returned value is what is
        "published". So in the case where format returns a constant
        string, as in my sample code, you should get just that.

        The actual results look like the handler has two formatters:
        mine + the default one, and prints every message twice, using
        each formatter in turn.


        STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
        1/ java Bug1 false
        2/ java Bug1 true

        EXPECTED VERSUS ACTUAL BEHAVIOR :
        EXPECTED -
        1/ 05-Jun-2003 14:17:09 Bug1 main
           WARNING: [First test]
           05-Jun-2003 14:17:09 Bug1 main
           WARNING: [Second test]

        2/ ===
           ===
        ACTUAL -
        1/ 05-Jun-2003 14:21:33 Bug1 main
           WARNING: [First test]
           05-Jun-2003 14:21:33 Bug1 main
           WARNING: First test
           05-Jun-2003 14:21:33 Bug1 main
           WARNING: [Second test]
           05-Jun-2003 14:21:33 Bug1 main
           WARNING: Second test

        2/ ===
           05-Jun-2003 14:21:56 Bug1 main
           WARNING: First test
           ===
           05-Jun-2003 14:21:56 Bug1 main
           WARNING: Second test

        REPRODUCIBILITY :
        This bug can be reproduced always.

        ---------- BEGIN SOURCE ----------
        import java.util.logging.*;

        public class Bug1 {

            public static final void main(String[] argv) {

        boolean override = Boolean.valueOf(argv[0]).booleanValue();

        Logger logger = Logger.getAnonymousLogger();
        Handler handler = new ConsoleHandler();
        Formatter format = override ?
        (Formatter)new SimpleFormatter() {
        public String formatMessage(LogRecord record) {
        return "[" + record.getMessage() + "]"; }
        public String format(LogRecord record) {
        return "===\n"; } } :
        (Formatter)new SimpleFormatter() {
        public String formatMessage(LogRecord record) {
        return "[" + record.getMessage() + "]"; } };
        handler.setFormatter(format);
        // this may or may not be necessary but better be safe
        removeAllHandlers(logger);
        logger.addHandler(handler);

        logger.warning("First test");
        logger.warning("Second test");
            }

            public static final void removeAllHandlers(Logger logger) {
        Handler[] handlers = logger.getHandlers();
        for (int i = 0; i < handlers.length; i++)
        logger.removeHandler(handlers[i]);
            }
        }
        ---------- END SOURCE ----------

        (Review ID: 187202)
        ======================================================================

              sspitsyn Serguei Spitsyn
              rmandalasunw Ranjith Mandala (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Created:
                Updated:
                Resolved:
                Imported:
                Indexed: