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

Use varargs in java.util.logging.Logger

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 5.0, 6
    • core-libs
    • Cause Known
    • generic, x86
    • generic, linux, windows_xp



      Name: rmT116609 Date: 02/24/2004


      A DESCRIPTION OF THE REQUEST :
      The "convenience" methods of java.util.logging.logger (finest, finer, fine, config, info, warning, and severe) would be more useful if they were extended to use varags. The new signatures would be:

      void finest(String, Object...);
      void finer(String, Object...);
      void fine(String, Object...);
      void config(String, Object...);
      void info(String, Object...);
      void warning(String, Object...);
      void servere(String, Object...);

      JUSTIFICATION :
      This change would enhance the value of the convenience methods by making it possible to use them with arguments. Currently logging a message with arguments requires using the "log" method, which more-or-less requires importing "java.util.logging.Level" and converting the logging call to use a much more clumsy syntax, making it very tempting just to use string concatenation instead of passing arguments.
      (Incident Review ID: 240173)
      ======================================================================

      Name: rmT116609 Date: 08/25/2004


      A DESCRIPTION OF THE REQUEST :
      The Logger methods that take multiple parameters for log messages should
      use the variable-arguments facility instead of requiring the caller to create an
      Object array and put all the parameters in that array.

      JUSTIFICATION :
      A. Ease of coding:

      logger.info( "xxx x = {0}, y= {1}", a, b )

      vs.

      logger.info( "xxx x = {0}, y = {1}}", new Object[] {a, b} )
      (Review ID: 300577)
      ======================================================================
      I would even suggest adding static overloads to Logger with a Class parameter:

      public static void info(Class clazz, String format, Object... args);
      // ...

      so that you could write even more easily e.g.:

      public class Frobnitz {
        public static void range(int lo, int hi) {
          if (lo > hi) {
            Logger.warning(Frobnitz.class, "Invalid range: {0} > {1}", lo, hi);
          }
        }
      }

      where the call would be equivalent to

      Logger.getLogger(Frobnitz.class.getName()).log(Level.WARNING, "Invalid range: {0} > {1}", new Object[] {lo, hi});

      This would make it particular convenient and pleasant to send log messages labelled with the originating class (so that you can easily turn on and off logging at the package or class level). Otherwise you need to use the clumsy and verbose getLogger syntax. (Using a static field for a per-class Logger is reasonable if you are using it a lot, but annoying for just one or two uses.)

            Unassigned Unassigned
            rmandalasunw Ranjith Mandala (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Imported:
              Indexed: