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

Simple enhancements to java.util.Logger for code readability

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • 8
    • core-libs
    • None

      The JUL Logger API has methods such as:

      public void log(Level level, String msg, Object params[])

      This makes the code somewhat ugly, whereas converting that last argument to a varargs would make it lovely.

      // A bit clunky
      LOG.log(Level.SEVERE, "Failed to deploy bundle {0}\n{1}", new Object[] { bundleName, e });
      // A bit nicer
      LOG.log(Level.SEVERE, "Failed to deploy bundle {0}\n{1}", bundleName, e);

      Another little pet peeve, I prefer not to use the log methods, but instead the "warning", "severe" etc methods because it makes the code very readable and also means that I don't have to either statically import "Level" or prefix everything with Level. However these methods are missing variants for handling formatted text, which makes them less than useful.

      // Well, with varargs a bit nicer
      LOG.log(Level.SEVERE, "Failed to deploy bundle {0}\n{1}", bundleName, e);
      // Nirvana
      LOG.severe("Failed to deploy bundle {0}\n{1}", bundleName, e);

      And just to drive home the point:

      // What I have to do today:
      LOG.log(Level.SEVERE, "Failed to deploy bundle {0}\n{1}", new Object[] { bundleName, e });
      // What I'd like to see
      LOG.severe("Failed to deploy bundle {0}\n{1}", bundleName, e);

            dfuchs Daniel Fuchs
            rbair Richard Bair (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: