src/share/classes/sun/util/logging/PlatformLogger.java

Print this page




  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 package sun.util.logging;
  28 
  29 import java.lang.ref.WeakReference;
  30 import java.io.PrintStream;
  31 import java.io.PrintWriter;
  32 import java.io.StringWriter;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;

  35 import java.util.Date;
  36 import java.util.HashMap;
  37 import java.util.Map;
  38 import sun.misc.JavaLangAccess;
  39 import sun.misc.SharedSecrets;
  40 
  41 /**
  42  * Platform logger provides an API for the JRE components to log
  43  * messages.  This enables the runtime components to eliminate the
  44  * static dependency of the logging facility and also defers the
  45  * java.util.logging initialization until it is enabled.
  46  * In addition, the PlatformLogger API can be used if the logging
  47  * module does not exist.
  48  *
  49  * If the logging facility is not enabled, the platform loggers
  50  * will output log messages per the default logging configuration
  51  * (see below). In this implementation, it does not log the
  52  * the stack frame information issuing the log message.
  53  *
  54  * When the logging facility is enabled (at startup or runtime),


  67  *   global logging level = INFO
  68  *   handlers = java.util.logging.ConsoleHandler
  69  *   java.util.logging.ConsoleHandler.level = INFO
  70  *   java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
  71  *
  72  * Limitation:
  73  * <JAVA_HOME>/lib/logging.properties is the system-wide logging
  74  * configuration defined in the specification and read in the
  75  * default case to configure any java.util.logging.Logger instances.
  76  * Platform loggers will not detect if <JAVA_HOME>/lib/logging.properties
  77  * is modified. In other words, unless the java.util.logging API
  78  * is used at runtime or the logging system properties is set,
  79  * the platform loggers will use the default setting described above.
  80  * The platform loggers are designed for JDK developers use and
  81  * this limitation can be workaround with setting
  82  * -Djava.util.logging.config.file system property.
  83  *
  84  * @since 1.7
  85  */
  86 public class PlatformLogger {
  87     // shortcut to PlatformLogger.Level enums
  88     public static final Level OFF     = Level.OFF;
  89     public static final Level SEVERE  = Level.SEVERE;
  90     public static final Level WARNING = Level.WARNING;
  91     public static final Level INFO    = Level.INFO;
  92     public static final Level CONFIG  = Level.CONFIG;
  93     public static final Level FINE    = Level.FINE;
  94     public static final Level FINER   = Level.FINER;
  95     public static final Level FINEST  = Level.FINEST;
  96     public static final Level ALL     = Level.ALL;






  97 
  98     /**
  99      * PlatformLogger logging levels.
 100      */
 101     public static enum Level {
 102         // The name and value must match that of {@code java.util.logging.Level} objects.
 103         ALL(Integer.MIN_VALUE),
 104         FINEST(300),
 105         FINER(400),
 106         FINE(500),
 107         CONFIG(700),
 108         INFO(800),
 109         WARNING(900),
 110         SEVERE(1000),
 111         OFF(Integer.MAX_VALUE);

 112 
 113         /**
 114          * Associated java.util.logging.Level lazily initialized in
 115          * JavaLoggerProxy's static initializer only once
 116          * when java.util.logging is available and enabled.
 117          * Only accessed by JavaLoggerProxy.
 118          */
 119         /* java.util.logging.Level */ Object javaLevel;
 120 
 121         private final int value;






 122         public int intValue() {
 123             return value;
 124         }
 125 
 126         Level(int value) {
 127             this.value = value;
















 128         }
 129     }
 130 
 131     private static final Level DEFAULT_LEVEL = INFO;
 132     private static boolean loggingEnabled;
 133     static {
 134         loggingEnabled = AccessController.doPrivileged(
 135             new PrivilegedAction<Boolean>() {
 136                 public Boolean run() {
 137                     String cname = System.getProperty("java.util.logging.config.class");
 138                     String fname = System.getProperty("java.util.logging.config.file");
 139                     return (cname != null || fname != null);
 140                 }
 141             });
 142 
 143         // force loading of all JavaLoggerProxy (sub)classes to make JIT de-optimizations
 144         // less probable.  Don't initialize JavaLoggerProxy class since
 145         // java.util.logging may not be enabled.
 146         try {
 147             Class.forName("sun.util.logging.PlatformLogger$DefaultLoggerProxy",
 148                           false,
 149                           PlatformLogger.class.getClassLoader());
 150             Class.forName("sun.util.logging.PlatformLogger$JavaLoggerProxy",
 151                           false,   // do not invoke class initializer


 217     }
 218 
 219     /**
 220      * A convenience method to test if the logger is turned off.
 221      * (i.e. its level is OFF).
 222      */
 223     public boolean isEnabled() {
 224         return loggerProxy.isEnabled();
 225     }
 226 
 227     /**
 228      * Gets the name for this platform logger.
 229      */
 230     public String getName() {
 231         return loggerProxy.name;
 232     }
 233 
 234     /**
 235      * Returns true if a message of the given level would actually
 236      * be logged by this logger.

































 237      */
 238     public boolean isLoggable(Level level) {



 239         // performance-sensitive method: use two monomorphic call-sites
 240         JavaLoggerProxy jlp = javaLoggerProxy;
 241         return jlp != null ? jlp.isLoggable(level) : loggerProxy.isLoggable(level);
 242     }
 243 
 244     /**
 245      * Get the log level that has been specified for this PlatformLogger.
 246      * The result may be null, which means that this logger's
 247      * effective level will be inherited from its parent.
 248      *
 249      * This method is primarily for testing purpose and not recommended
 250      * to be used at runtime since it does not support custom j.u.l.Level.
 251      *
 252      * @return  this PlatformLogger's level
 253      *
 254      * @throw IllegalArgumentException if j.u.l.Logger is set to
 255      *        a custom j.u.l.Level when java.util.logging facility is enabled
 256      */
 257     public Level getLevel() {
 258         return loggerProxy.getLevel();
 259     }
 260 
 261     /**
 262      * Set the log level specifying which message levels will be
 263      * logged by this logger.  Message levels lower than this
 264      * value will be discarded.  The level value {@link #OFF}
 265      * can be used to turn off logging.
 266      * <p>
 267      * If the new level is null, it means that this node should
 268      * inherit its level from its nearest ancestor with a specific
 269      * (non-null) level value.
 270      *
 271      * @param newLevel the new value for the log level (may be null)
 272      */
 273     public void setLevel(Level newLevel) {
 274         loggerProxy.setLevel(newLevel);
 275     }
 276 
 277     /**
 278      * Logs a SEVERE message.
 279      */
 280     public void severe(String msg) {
 281         loggerProxy.doLog(SEVERE, msg);
 282     }
 283 
 284     public void severe(String msg, Throwable t) {
 285         loggerProxy.doLog(SEVERE, msg, t);
 286     }
 287 
 288     public void severe(String msg, Object... params) {
 289         loggerProxy.doLog(SEVERE, msg, params);
 290     }
 291 
 292     /**
 293      * Logs a WARNING message.
 294      */
 295     public void warning(String msg) {
 296         loggerProxy.doLog(WARNING, msg);
 297     }
 298 
 299     public void warning(String msg, Throwable t) {
 300         loggerProxy.doLog(WARNING, msg, t);
 301     }
 302 
 303     public void warning(String msg, Object... params) {
 304         loggerProxy.doLog(WARNING, msg, params);
 305     }
 306 
 307     /**
 308      * Logs an INFO message.
 309      */
 310     public void info(String msg) {
 311         loggerProxy.doLog(INFO, msg);
 312     }
 313 
 314     public void info(String msg, Throwable t) {
 315         loggerProxy.doLog(INFO, msg, t);
 316     }
 317 
 318     public void info(String msg, Object... params) {
 319         loggerProxy.doLog(INFO, msg, params);
 320     }
 321 
 322     /**
 323      * Logs a CONFIG message.
 324      */
 325     public void config(String msg) {
 326         loggerProxy.doLog(CONFIG, msg);
 327     }
 328 
 329     public void config(String msg, Throwable t) {
 330         loggerProxy.doLog(CONFIG, msg, t);
 331     }
 332 
 333     public void config(String msg, Object... params) {
 334         loggerProxy.doLog(CONFIG, msg, params);
 335     }
 336 
 337     /**
 338      * Logs a FINE message.
 339      */
 340     public void fine(String msg) {
 341         loggerProxy.doLog(FINE, msg);
 342     }
 343 
 344     public void fine(String msg, Throwable t) {
 345         loggerProxy.doLog(FINE, msg, t);
 346     }
 347 
 348     public void fine(String msg, Object... params) {
 349         loggerProxy.doLog(FINE, msg, params);
 350     }
 351 
 352     /**
 353      * Logs a FINER message.
 354      */
 355     public void finer(String msg) {
 356         loggerProxy.doLog(FINER, msg);
 357     }
 358 
 359     public void finer(String msg, Throwable t) {
 360         loggerProxy.doLog(FINER, msg, t);
 361     }
 362 
 363     public void finer(String msg, Object... params) {
 364         loggerProxy.doLog(FINER, msg, params);
 365     }
 366 
 367     /**
 368      * Logs a FINEST message.
 369      */
 370     public void finest(String msg) {
 371         loggerProxy.doLog(FINEST, msg);
 372     }
 373 
 374     public void finest(String msg, Throwable t) {
 375         loggerProxy.doLog(FINEST, msg, t);
 376     }
 377 
 378     public void finest(String msg, Object... params) {
 379         loggerProxy.doLog(FINEST, msg, params);
 380     }
 381 
 382     /**
 383      * Abstract base class for logging support, defining the API and common field.
 384      */
 385     private static abstract class LoggerProxy {
 386         final String name;
 387 
 388         protected LoggerProxy(String name) {
 389             this.name = name;
 390         }
 391 
 392         abstract boolean isEnabled();
 393 
 394         abstract Level getLevel();
 395         abstract void setLevel(Level newLevel);
 396 
 397         abstract void doLog(Level level, String msg);
 398         abstract void doLog(Level level, String msg, Throwable thrown);
 399         abstract void doLog(Level level, String msg, Object... params);


 404 
 405     private static final class DefaultLoggerProxy extends LoggerProxy {
 406         /**
 407          * Default platform logging support - output messages to System.err -
 408          * equivalent to ConsoleHandler with SimpleFormatter.
 409          */
 410         private static PrintStream outputStream() {
 411             return System.err;
 412         }
 413 
 414         volatile Level effectiveLevel; // effective level (never null)
 415         volatile Level level;          // current level set for this node (may be null)
 416 
 417         DefaultLoggerProxy(String name) {
 418             super(name);
 419             this.effectiveLevel = deriveEffectiveLevel(null);
 420             this.level = null;
 421         }
 422 
 423         boolean isEnabled() {
 424             return effectiveLevel != OFF;
 425         }
 426 
 427         Level getLevel() {
 428             return level;
 429         }
 430 
 431         void setLevel(Level newLevel) {
 432             Level oldLevel = level;
 433             if (oldLevel != newLevel) {
 434                 level = newLevel;
 435                 effectiveLevel = deriveEffectiveLevel(newLevel);
 436             }
 437         }
 438 
 439         void doLog(Level level, String msg) {
 440             if (isLoggable(level)) {
 441                 outputStream().print(format(level, msg, null));
 442             }
 443         }
 444 
 445         void doLog(Level level, String msg, Throwable thrown) {
 446             if (isLoggable(level)) {
 447                 outputStream().print(format(level, msg, thrown));
 448             }
 449         }
 450 
 451         void doLog(Level level, String msg, Object... params) {
 452             if (isLoggable(level)) {
 453                 String newMsg = formatMessage(msg, params);
 454                 outputStream().print(format(level, newMsg, null));
 455             }
 456         }
 457 
 458         boolean isLoggable(Level level) {
 459             Level effectiveLevel = this.effectiveLevel;
 460             return level.intValue() >= effectiveLevel.intValue() && effectiveLevel != OFF;
 461         }
 462 
 463         // derive effective level (could do inheritance search like j.u.l.Logger)
 464         private Level deriveEffectiveLevel(Level level) {
 465             return level == null ? DEFAULT_LEVEL : level;
 466         }
 467 
 468         // Copied from java.util.logging.Formatter.formatMessage
 469         private String formatMessage(String format, Object... parameters) {
 470             // Do the formatting.
 471             try {
 472                 if (parameters == null || parameters.length == 0) {
 473                     // No parameters.  Just return format string.
 474                     return format;
 475                 }
 476                 // Is it a java.text style format?
 477                 // Ideally we could match with
 478                 // Pattern.compile("\\{\\d").matcher(format).find())
 479                 // However the cost is 14% higher, so we cheaply check for
 480                 // 1 of the first 4 parameters


 594         void doLog(Level level, String msg, Object... params) {
 595             if (!isLoggable(level)) {
 596                 return;
 597             }
 598             // only pass String objects to the j.u.l.Logger which may
 599             // be created by untrusted code
 600             int len = (params != null) ? params.length : 0;
 601             Object[] sparams = new String[len];
 602             for (int i = 0; i < len; i++) {
 603                 sparams [i] = String.valueOf(params[i]);
 604             }
 605             LoggingSupport.log(javaLogger, level.javaLevel, msg, sparams);
 606         }
 607 
 608         boolean isEnabled() {
 609             return LoggingSupport.isLoggable(javaLogger, Level.OFF.javaLevel);
 610         }
 611 
 612         /**
 613          * Returns the PlatformLogger.Level mapped from j.u.l.Level
 614          * set in the logger.
 615          * @throw IllegalArgumentException if j.u.l.Logger is set to
 616          *        a custom j.u.l.Level
 617          */
 618         Level getLevel() {
 619             Object javaLevel = LoggingSupport.getLevel(javaLogger);
 620             return javaLevel == null
 621                     ? null
 622                     : Level.valueOf(LoggingSupport.getLevelName(javaLevel));




 623         }
 624 
 625         void setLevel(Level level) {
 626             LoggingSupport.setLevel(javaLogger, level == null ? null : level.javaLevel);
 627         }
 628 
 629         boolean isLoggable(Level level) {
 630             return LoggingSupport.isLoggable(javaLogger, level.javaLevel);
 631         }
 632     }
 633 }


  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 
  27 package sun.util.logging;
  28 
  29 import java.lang.ref.WeakReference;
  30 import java.io.PrintStream;
  31 import java.io.PrintWriter;
  32 import java.io.StringWriter;
  33 import java.security.AccessController;
  34 import java.security.PrivilegedAction;
  35 import java.util.Arrays;
  36 import java.util.Date;
  37 import java.util.HashMap;
  38 import java.util.Map;
  39 import sun.misc.JavaLangAccess;
  40 import sun.misc.SharedSecrets;
  41 
  42 /**
  43  * Platform logger provides an API for the JRE components to log
  44  * messages.  This enables the runtime components to eliminate the
  45  * static dependency of the logging facility and also defers the
  46  * java.util.logging initialization until it is enabled.
  47  * In addition, the PlatformLogger API can be used if the logging
  48  * module does not exist.
  49  *
  50  * If the logging facility is not enabled, the platform loggers
  51  * will output log messages per the default logging configuration
  52  * (see below). In this implementation, it does not log the
  53  * the stack frame information issuing the log message.
  54  *
  55  * When the logging facility is enabled (at startup or runtime),


  68  *   global logging level = INFO
  69  *   handlers = java.util.logging.ConsoleHandler
  70  *   java.util.logging.ConsoleHandler.level = INFO
  71  *   java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
  72  *
  73  * Limitation:
  74  * <JAVA_HOME>/lib/logging.properties is the system-wide logging
  75  * configuration defined in the specification and read in the
  76  * default case to configure any java.util.logging.Logger instances.
  77  * Platform loggers will not detect if <JAVA_HOME>/lib/logging.properties
  78  * is modified. In other words, unless the java.util.logging API
  79  * is used at runtime or the logging system properties is set,
  80  * the platform loggers will use the default setting described above.
  81  * The platform loggers are designed for JDK developers use and
  82  * this limitation can be workaround with setting
  83  * -Djava.util.logging.config.file system property.
  84  *
  85  * @since 1.7
  86  */
  87 public class PlatformLogger {
  88     /*
  89      * These constants should be shortcuts to Level enum constants that
  90      * the clients of sun.util.logging.PlatformLogger require no source
  91      * modification and avoid the conversion from int to Level enum.
  92      *
  93      * This can be done when JavaFX is converted to use the new PlatformLogger.Level API.
  94      */
  95     public static final int OFF     = Integer.MAX_VALUE;
  96     public static final int SEVERE  = 1000;
  97     public static final int WARNING = 900;
  98     public static final int INFO    = 800;
  99     public static final int CONFIG  = 700;
 100     public static final int FINE    = 500;
 101     public static final int FINER   = 400;
 102     public static final int FINEST  = 300;
 103     public static final int ALL     = Integer.MIN_VALUE;
 104 
 105     /**
 106      * PlatformLogger logging levels.
 107      */
 108     public static enum Level {
 109         // The name and value must match that of {@code java.util.logging.Level}s.
 110         // Declare in ascending order of the given value for binary search.
 111         ALL,
 112         FINEST,
 113         FINER,
 114         FINE,
 115         CONFIG,
 116         INFO,
 117         WARNING,
 118         SEVERE,
 119         OFF;
 120 
 121         /**
 122          * Associated java.util.logging.Level lazily initialized in
 123          * JavaLoggerProxy's static initializer only once
 124          * when java.util.logging is available and enabled.
 125          * Only accessed by JavaLoggerProxy.
 126          */
 127         /* java.util.logging.Level */ Object javaLevel;
 128 
 129         // ascending order for binary search matching the list of enum constants
 130         private static final int[] levelValues = new int[] {
 131             PlatformLogger.ALL, PlatformLogger.FINEST, PlatformLogger.FINER,
 132             PlatformLogger.FINE, PlatformLogger.CONFIG, PlatformLogger.INFO,
 133             PlatformLogger.WARNING, PlatformLogger.SEVERE, PlatformLogger.OFF
 134         };
 135 
 136         public int intValue() {
 137             return levelValues[this.ordinal()];
 138         }
 139 
 140         static Level valueOf(int level) {
 141             switch (level) {
 142                 // ordering per the highest occurences in the jdk source
 143                 // finest, fine, finer, info first
 144                 case PlatformLogger.FINEST  : return Level.FINEST;
 145                 case PlatformLogger.FINE    : return Level.FINE;
 146                 case PlatformLogger.FINER   : return Level.FINER;
 147                 case PlatformLogger.INFO    : return Level.INFO;
 148                 case PlatformLogger.WARNING : return Level.WARNING;
 149                 case PlatformLogger.CONFIG  : return Level.CONFIG;
 150                 case PlatformLogger.SEVERE  : return Level.SEVERE;
 151                 case PlatformLogger.OFF     : return Level.OFF;
 152                 case PlatformLogger.ALL     : return Level.ALL;
 153             }
 154             // return the nearest Level value >= the given level,
 155             // for level > SEVERE, return SEVERE and exclude OFF
 156             int i = Arrays.binarySearch(levelValues, 0, levelValues.length-2, level);
 157             return values()[i >= 0 ? i : (-i-1)];
 158         }
 159     }
 160 
 161     private static final Level DEFAULT_LEVEL = Level.INFO;
 162     private static boolean loggingEnabled;
 163     static {
 164         loggingEnabled = AccessController.doPrivileged(
 165             new PrivilegedAction<Boolean>() {
 166                 public Boolean run() {
 167                     String cname = System.getProperty("java.util.logging.config.class");
 168                     String fname = System.getProperty("java.util.logging.config.file");
 169                     return (cname != null || fname != null);
 170                 }
 171             });
 172 
 173         // force loading of all JavaLoggerProxy (sub)classes to make JIT de-optimizations
 174         // less probable.  Don't initialize JavaLoggerProxy class since
 175         // java.util.logging may not be enabled.
 176         try {
 177             Class.forName("sun.util.logging.PlatformLogger$DefaultLoggerProxy",
 178                           false,
 179                           PlatformLogger.class.getClassLoader());
 180             Class.forName("sun.util.logging.PlatformLogger$JavaLoggerProxy",
 181                           false,   // do not invoke class initializer


 247     }
 248 
 249     /**
 250      * A convenience method to test if the logger is turned off.
 251      * (i.e. its level is OFF).
 252      */
 253     public boolean isEnabled() {
 254         return loggerProxy.isEnabled();
 255     }
 256 
 257     /**
 258      * Gets the name for this platform logger.
 259      */
 260     public String getName() {
 261         return loggerProxy.name;
 262     }
 263 
 264     /**
 265      * Returns true if a message of the given level would actually
 266      * be logged by this logger.
 267      *
 268      * @deprecated Use isLoggable(Level) instead.
 269      */
 270     @Deprecated
 271     public boolean isLoggable(int levelValue) {
 272         return isLoggable(Level.valueOf(levelValue));
 273     }
 274 
 275     /**
 276      * Gets the current log level. Returns 0 if the current effective level is
 277      * not set (equivalent to Logger.getLevel() returns null).
 278      *
 279      * @deprecated Use level() instead
 280      */
 281     @Deprecated
 282     public int getLevel() {
 283         Level level = loggerProxy.getLevel();
 284         return level != null ? level.intValue() : 0;
 285     }
 286 
 287     /**
 288      * Sets the log level.
 289      *
 290      * @deprecated Use setLevel(Level) instead
 291      */
 292     @Deprecated
 293     public void setLevel(int newLevel) {
 294         loggerProxy.setLevel(newLevel == 0 ? null : Level.valueOf(newLevel));
 295     }
 296 
 297     /**
 298      * Returns true if a message of the given level would actually
 299      * be logged by this logger.
 300      */
 301     public boolean isLoggable(Level level) {
 302         if (level == null) {
 303             throw new NullPointerException();
 304         }
 305         // performance-sensitive method: use two monomorphic call-sites
 306         JavaLoggerProxy jlp = javaLoggerProxy;
 307         return jlp != null ? jlp.isLoggable(level) : loggerProxy.isLoggable(level);
 308     }
 309 
 310     /**
 311      * Get the log level that has been specified for this PlatformLogger.
 312      * The result may be null, which means that this logger's
 313      * effective level will be inherited from its parent.
 314      *



 315      * @return  this PlatformLogger's level



 316      */
 317     public Level level() {
 318         return loggerProxy.getLevel();
 319     }
 320 
 321     /**
 322      * Set the log level specifying which message levels will be
 323      * logged by this logger.  Message levels lower than this
 324      * value will be discarded.  The level value {@link #OFF}
 325      * can be used to turn off logging.
 326      * <p>
 327      * If the new level is null, it means that this node should
 328      * inherit its level from its nearest ancestor with a specific
 329      * (non-null) level value.
 330      *
 331      * @param newLevel the new value for the log level (may be null)
 332      */
 333     public void setLevel(Level newLevel) {
 334         loggerProxy.setLevel(newLevel);
 335     }
 336 
 337     /**
 338      * Logs a SEVERE message.
 339      */
 340     public void severe(String msg) {
 341         loggerProxy.doLog(Level.SEVERE, msg);
 342     }
 343 
 344     public void severe(String msg, Throwable t) {
 345         loggerProxy.doLog(Level.SEVERE, msg, t);
 346     }
 347 
 348     public void severe(String msg, Object... params) {
 349         loggerProxy.doLog(Level.SEVERE, msg, params);
 350     }
 351 
 352     /**
 353      * Logs a WARNING message.
 354      */
 355     public void warning(String msg) {
 356         loggerProxy.doLog(Level.WARNING, msg);
 357     }
 358 
 359     public void warning(String msg, Throwable t) {
 360         loggerProxy.doLog(Level.WARNING, msg, t);
 361     }
 362 
 363     public void warning(String msg, Object... params) {
 364         loggerProxy.doLog(Level.WARNING, msg, params);
 365     }
 366 
 367     /**
 368      * Logs an INFO message.
 369      */
 370     public void info(String msg) {
 371         loggerProxy.doLog(Level.INFO, msg);
 372     }
 373 
 374     public void info(String msg, Throwable t) {
 375         loggerProxy.doLog(Level.INFO, msg, t);
 376     }
 377 
 378     public void info(String msg, Object... params) {
 379         loggerProxy.doLog(Level.INFO, msg, params);
 380     }
 381 
 382     /**
 383      * Logs a CONFIG message.
 384      */
 385     public void config(String msg) {
 386         loggerProxy.doLog(Level.CONFIG, msg);
 387     }
 388 
 389     public void config(String msg, Throwable t) {
 390         loggerProxy.doLog(Level.CONFIG, msg, t);
 391     }
 392 
 393     public void config(String msg, Object... params) {
 394         loggerProxy.doLog(Level.CONFIG, msg, params);
 395     }
 396 
 397     /**
 398      * Logs a FINE message.
 399      */
 400     public void fine(String msg) {
 401         loggerProxy.doLog(Level.FINE, msg);
 402     }
 403 
 404     public void fine(String msg, Throwable t) {
 405         loggerProxy.doLog(Level.FINE, msg, t);
 406     }
 407 
 408     public void fine(String msg, Object... params) {
 409         loggerProxy.doLog(Level.FINE, msg, params);
 410     }
 411 
 412     /**
 413      * Logs a FINER message.
 414      */
 415     public void finer(String msg) {
 416         loggerProxy.doLog(Level.FINER, msg);
 417     }
 418 
 419     public void finer(String msg, Throwable t) {
 420         loggerProxy.doLog(Level.FINER, msg, t);
 421     }
 422 
 423     public void finer(String msg, Object... params) {
 424         loggerProxy.doLog(Level.FINER, msg, params);
 425     }
 426 
 427     /**
 428      * Logs a FINEST message.
 429      */
 430     public void finest(String msg) {
 431         loggerProxy.doLog(Level.FINEST, msg);
 432     }
 433 
 434     public void finest(String msg, Throwable t) {
 435         loggerProxy.doLog(Level.FINEST, msg, t);
 436     }
 437 
 438     public void finest(String msg, Object... params) {
 439         loggerProxy.doLog(Level.FINEST, msg, params);
 440     }
 441 
 442     /**
 443      * Abstract base class for logging support, defining the API and common field.
 444      */
 445     private static abstract class LoggerProxy {
 446         final String name;
 447 
 448         protected LoggerProxy(String name) {
 449             this.name = name;
 450         }
 451 
 452         abstract boolean isEnabled();
 453 
 454         abstract Level getLevel();
 455         abstract void setLevel(Level newLevel);
 456 
 457         abstract void doLog(Level level, String msg);
 458         abstract void doLog(Level level, String msg, Throwable thrown);
 459         abstract void doLog(Level level, String msg, Object... params);


 464 
 465     private static final class DefaultLoggerProxy extends LoggerProxy {
 466         /**
 467          * Default platform logging support - output messages to System.err -
 468          * equivalent to ConsoleHandler with SimpleFormatter.
 469          */
 470         private static PrintStream outputStream() {
 471             return System.err;
 472         }
 473 
 474         volatile Level effectiveLevel; // effective level (never null)
 475         volatile Level level;          // current level set for this node (may be null)
 476 
 477         DefaultLoggerProxy(String name) {
 478             super(name);
 479             this.effectiveLevel = deriveEffectiveLevel(null);
 480             this.level = null;
 481         }
 482 
 483         boolean isEnabled() {
 484             return effectiveLevel != Level.OFF;
 485         }
 486 
 487         Level getLevel() {
 488             return level;
 489         }
 490 
 491         void setLevel(Level newLevel) {
 492             Level oldLevel = level;
 493             if (oldLevel != newLevel) {
 494                 level = newLevel;
 495                 effectiveLevel = deriveEffectiveLevel(newLevel);
 496             }
 497         }
 498 
 499         void doLog(Level level, String msg) {
 500             if (isLoggable(level)) {
 501                 outputStream().print(format(level, msg, null));
 502             }
 503         }
 504 
 505         void doLog(Level level, String msg, Throwable thrown) {
 506             if (isLoggable(level)) {
 507                 outputStream().print(format(level, msg, thrown));
 508             }
 509         }
 510 
 511         void doLog(Level level, String msg, Object... params) {
 512             if (isLoggable(level)) {
 513                 String newMsg = formatMessage(msg, params);
 514                 outputStream().print(format(level, newMsg, null));
 515             }
 516         }
 517 
 518         boolean isLoggable(Level level) {
 519             Level effectiveLevel = this.effectiveLevel;
 520             return level.intValue() >= effectiveLevel.intValue() && effectiveLevel != Level.OFF;
 521         }
 522 
 523         // derive effective level (could do inheritance search like j.u.l.Logger)
 524         private Level deriveEffectiveLevel(Level level) {
 525             return level == null ? DEFAULT_LEVEL : level;
 526         }
 527 
 528         // Copied from java.util.logging.Formatter.formatMessage
 529         private String formatMessage(String format, Object... parameters) {
 530             // Do the formatting.
 531             try {
 532                 if (parameters == null || parameters.length == 0) {
 533                     // No parameters.  Just return format string.
 534                     return format;
 535                 }
 536                 // Is it a java.text style format?
 537                 // Ideally we could match with
 538                 // Pattern.compile("\\{\\d").matcher(format).find())
 539                 // However the cost is 14% higher, so we cheaply check for
 540                 // 1 of the first 4 parameters


 654         void doLog(Level level, String msg, Object... params) {
 655             if (!isLoggable(level)) {
 656                 return;
 657             }
 658             // only pass String objects to the j.u.l.Logger which may
 659             // be created by untrusted code
 660             int len = (params != null) ? params.length : 0;
 661             Object[] sparams = new String[len];
 662             for (int i = 0; i < len; i++) {
 663                 sparams [i] = String.valueOf(params[i]);
 664             }
 665             LoggingSupport.log(javaLogger, level.javaLevel, msg, sparams);
 666         }
 667 
 668         boolean isEnabled() {
 669             return LoggingSupport.isLoggable(javaLogger, Level.OFF.javaLevel);
 670         }
 671 
 672         /**
 673          * Returns the PlatformLogger.Level mapped from j.u.l.Level
 674          * set in the logger.  If the j.u.l.Logger is set to a custom Level,
 675          * this method will return the nearest Level.

 676          */
 677         Level getLevel() {
 678             Object javaLevel = LoggingSupport.getLevel(javaLogger);
 679             if (javaLevel == null) return null;
 680 
 681             try {
 682                 return Level.valueOf(LoggingSupport.getLevelName(javaLevel));
 683             } catch (IllegalArgumentException e) {
 684                 return Level.valueOf(LoggingSupport.getLevelValue(javaLevel));
 685             }
 686         }
 687 
 688         void setLevel(Level level) {
 689             LoggingSupport.setLevel(javaLogger, level == null ? null : level.javaLevel);
 690         }
 691 
 692         boolean isLoggable(Level level) {
 693             return LoggingSupport.isLoggable(javaLogger, level.javaLevel);
 694         }
 695     }
 696 }