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

MemoryHandler not using configured Formatter from properties

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      OS: Windows10, Fedora35
      Java ( jdk.java.net ): 1.8.0.322, 18.0.1
      div Hardware, Laptops, Desktops, Intel i5-3k... / i7-10k...

      A DESCRIPTION OF THE PROBLEM :
      When using a custom properties-file to set the configuration for java.util.logging (example, see below link) the configured custom MemoryHandler is set up with a custom Formatter (also see below link regarding the api-documentation). The target of the CustomMemoryHandler id directed to the std ConsoleHandler (using the default-formatter).
      The problem is, the CustomMemoryHandler does not format any log-message.
      Workaround: Enabling the CustomFormatter on the ConsoleHandler gets the output formatted.

      API-Documantation: https://docs.oracle.com/en/java/javase/18/docs/api/java.logging/java/util/logging/MemoryHandler.html
      Example on github: https://github.com/SvenJunge/memoryhandler-eval

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      See code from github repo.
      Run: foo.Main
      For Workaround change loaded resource/properties-file (logging.properties.nocustomformat vs. logging.properties.customformat ).



      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      The log-messages printed to console should be formatted by the CustomFormatter (one line, level: message).
      ACTUAL -
      The log-messages printed to console are formatted with standard-formatter (two lines, localized level, class-name, method-name).

      ---------- BEGIN SOURCE ----------
      https://github.com/SvenJunge/memoryhandler-eval
      ------------------------
      ## CustomFormatter.java
      package foo;


      import java.util.logging.Formatter;
      import java.util.logging.LogRecord;


      public final
      class CustomFormatter
          extends Formatter
      {

        @Override
        public String
        format( final LogRecord record )
        {
          return record.getLevel() + ":\t\t" + record.getMessage() + System.lineSeparator();
        }

      }
      -----------------------
      ## CustomMemoryHandler.java
      package foo;


      import java.util.logging.MemoryHandler;


      public final
      class CustomMemoryHandler
          extends MemoryHandler
      {

        @Override
        public void
        close()
            throws SecurityException
        {
          super.push();
          super.flush();
          super.close();
        }

      }
      ------------------------
      ## Main.java
      package foo;


      import java.io.IOException;
      import java.io.InputStream;
      import java.util.logging.LogManager;
      import java.util.logging.Logger;


      public final
      class Main
      {

        public static void
        main( final String[] args )
            throws InterruptedException
                 , IOException
        {
          System.out.println( "Starting ..." );

          try ( final InputStream is = Main.class.getClassLoader()
                                                 .getResourceAsStream( "logging.properties.nocustomformat" ) )
      // .getResourceAsStream( "logging.properties.customformat" ) )
          {
            LogManager.getLogManager()
                      .readConfiguration( is );
          }

          final Logger log = Logger.getLogger( Main.class.getName() );

          for( int i = 1; i <= 3; i++ )
          {
            System.out.println( "run: " + i );
            Thread.sleep( 200L );

            log.config( "config-msg" );
            log.info( "info-msg" );
            if( i % 2 == 0)
            { log.warning( "warning-msg" ); }

            System.out.println( "sleep" );
            Thread.sleep( 650L );
          }

          System.out.println( "-fin. (CustomMemoryHandler.close() should invoke publish+flush)" );
        }

      }
      --------------------------------
      ## logging.properties.nocustomformat
      #.level = ALL
      #.handler = java.util.logging.ConsoleHandler

      java.util.logging.ConsoleHandler.level = CONFIG
      #java.util.logging.ConsoleHandler.formatter = foo.CustomFormatter

      foo.level = ALL
      foo.handlers = foo.CustomMemoryHandler

      foo.CustomMemoryHandler.formatter = foo.CustomFormatter
      foo.CustomMemoryHandler.target = java.util.logging.ConsoleHandler
      foo.CustomMemoryHandler.push = WARNING

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Adding the CustomFormatter to the ConsoleHandler.

      FREQUENCY : always


            dfuchs Daniel Fuchs
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: