1 /*
   2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  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 java.util.logging;
  28 
  29 import java.lang.ref.WeakReference;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 import java.util.ArrayList;
  33 import java.util.Iterator;
  34 import java.util.Locale;
  35 import java.util.MissingResourceException;
  36 import java.util.ResourceBundle;
  37 import java.util.concurrent.CopyOnWriteArrayList;
  38 import java.util.function.Supplier;
  39 import sun.reflect.CallerSensitive;
  40 import sun.reflect.Reflection;
  41 
  42 /**
  43  * A Logger object is used to log messages for a specific
  44  * system or application component.  Loggers are normally named,
  45  * using a hierarchical dot-separated namespace.  Logger names
  46  * can be arbitrary strings, but they should normally be based on
  47  * the package name or class name of the logged component, such
  48  * as java.net or javax.swing.  In addition it is possible to create
  49  * "anonymous" Loggers that are not stored in the Logger namespace.
  50  * <p>
  51  * Logger objects may be obtained by calls on one of the getLogger
  52  * factory methods.  These will either create a new Logger or
  53  * return a suitable existing Logger. It is important to note that
  54  * the Logger returned by one of the {@code getLogger} factory methods
  55  * may be garbage collected at any time if a strong reference to the
  56  * Logger is not kept.
  57  * <p>
  58  * Logging messages will be forwarded to registered Handler
  59  * objects, which can forward the messages to a variety of
  60  * destinations, including consoles, files, OS logs, etc.
  61  * <p>
  62  * Each Logger keeps track of a "parent" Logger, which is its
  63  * nearest existing ancestor in the Logger namespace.
  64  * <p>
  65  * Each Logger has a "Level" associated with it.  This reflects
  66  * a minimum Level that this logger cares about.  If a Logger's
  67  * level is set to <tt>null</tt>, then its effective level is inherited
  68  * from its parent, which may in turn obtain it recursively from its
  69  * parent, and so on up the tree.
  70  * <p>
  71  * The log level can be configured based on the properties from the
  72  * logging configuration file, as described in the description
  73  * of the LogManager class.  However it may also be dynamically changed
  74  * by calls on the Logger.setLevel method.  If a logger's level is
  75  * changed the change may also affect child loggers, since any child
  76  * logger that has <tt>null</tt> as its level will inherit its
  77  * effective level from its parent.
  78  * <p>
  79  * On each logging call the Logger initially performs a cheap
  80  * check of the request level (e.g., SEVERE or FINE) against the
  81  * effective log level of the logger.  If the request level is
  82  * lower than the log level, the logging call returns immediately.
  83  * <p>
  84  * After passing this initial (cheap) test, the Logger will allocate
  85  * a LogRecord to describe the logging message.  It will then call a
  86  * Filter (if present) to do a more detailed check on whether the
  87  * record should be published.  If that passes it will then publish
  88  * the LogRecord to its output Handlers.  By default, loggers also
  89  * publish to their parent's Handlers, recursively up the tree.
  90  * <p>
  91  * Each Logger may have a {@code ResourceBundle} associated with it.
  92  * The {@code ResourceBundle} may be specified by name, using the
  93  * {@link #getLogger(java.lang.String, java.lang.String)} factory
  94  * method, or by value - using the {@link
  95  * #setResourceBundle(java.util.ResourceBundle) setResourceBundle} method.
  96  * This bundle will be used for localizing logging messages.
  97  * If a Logger does not have its own {@code ResourceBundle} or resource bundle
  98  * name, then it will inherit the {@code ResourceBundle} or resource bundle name
  99  * from its parent, recursively up the tree.
 100  * <p>
 101  * Most of the logger output methods take a "msg" argument.  This
 102  * msg argument may be either a raw value or a localization key.
 103  * During formatting, if the logger has (or inherits) a localization
 104  * {@code ResourceBundle} and if the {@code ResourceBundle} has a mapping for
 105  * the msg string, then the msg string is replaced by the localized value.
 106  * Otherwise the original msg string is used.  Typically, formatters use
 107  * java.text.MessageFormat style formatting to format parameters, so
 108  * for example a format string "{0} {1}" would format two parameters
 109  * as strings.
 110  * <p>
 111  * A set of methods alternatively take a "msgSupplier" instead of a "msg"
 112  * argument.  These methods take a {@link Supplier}{@code <String>} function
 113  * which is invoked to construct the desired log message only when the message
 114  * actually is to be logged based on the effective log level thus eliminating
 115  * unnecessary message construction. For example, if the developer wants to
 116  * log system health status for diagnosis, with the String-accepting version,
 117  * the code would look like:
 118  <pre><code>
 119 
 120    class DiagnosisMessages {
 121      static String systemHealthStatus() {
 122        // collect system health information
 123        ...
 124      }
 125    }
 126    ...
 127    logger.log(Level.FINER, DiagnosisMessages.systemHealthStatus());
 128 </code></pre>
 129  * With the above code, the health status is collected unnecessarily even when
 130  * the log level FINER is disabled. With the Supplier-accepting version as
 131  * below, the status will only be collected when the log level FINER is
 132  * enabled.
 133  <pre><code>
 134 
 135    logger.log(Level.FINER, DiagnosisMessages::systemHealthStatus);
 136 </code></pre>
 137  * <p>
 138  * When looking for a {@code ResourceBundle}, the logger will first look at
 139  * whether a bundle was specified using {@link
 140  * #setResourceBundle(java.util.ResourceBundle) setResourceBundle}, and then
 141  * only whether a resource bundle name was specified through the {@link
 142  * #getLogger(java.lang.String, java.lang.String) getLogger} factory method.
 143  * If no {@code ResourceBundle} or no resource bundle name is found,
 144  * then it will use the nearest {@code ResourceBundle} or resource bundle
 145  * name inherited from its parent tree.<br>
 146  * When a {@code ResourceBundle} was inherited or specified through the
 147  * {@link
 148  * #setResourceBundle(java.util.ResourceBundle) setResourceBundle} method, then
 149  * that {@code ResourceBundle} will be used. Otherwise if the logger only
 150  * has or inherited a resource bundle name, then that resource bundle name
 151  * will be mapped to a {@code ResourceBundle} object, using the default Locale
 152  * at the time of logging.
 153  * <br id="ResourceBundleMapping">When mapping resource bundle names to
 154  * {@code ResourceBundle} objects, the logger will first try to use the
 155  * Thread's {@linkplain java.lang.Thread#getContextClassLoader() context class
 156  * loader} to map the given resource bundle name to a {@code ResourceBundle}.
 157  * If the thread context class loader is {@code null}, it will try the
 158  * {@linkplain java.lang.ClassLoader#getSystemClassLoader() system class loader}
 159  * instead.  If the {@code ResourceBundle} is still not found, it will use the
 160  * class loader of the first caller of the {@link
 161  * #getLogger(java.lang.String, java.lang.String) getLogger} factory method.
 162  * <p>
 163  * Formatting (including localization) is the responsibility of
 164  * the output Handler, which will typically call a Formatter.
 165  * <p>
 166  * Note that formatting need not occur synchronously.  It may be delayed
 167  * until a LogRecord is actually written to an external sink.
 168  * <p>
 169  * The logging methods are grouped in five main categories:
 170  * <ul>
 171  * <li><p>
 172  *     There are a set of "log" methods that take a log level, a message
 173  *     string, and optionally some parameters to the message string.
 174  * <li><p>
 175  *     There are a set of "logp" methods (for "log precise") that are
 176  *     like the "log" methods, but also take an explicit source class name
 177  *     and method name.
 178  * <li><p>
 179  *     There are a set of "logrb" method (for "log with resource bundle")
 180  *     that are like the "logp" method, but also take an explicit resource
 181  *     bundle object for use in localizing the log message.
 182  * <li><p>
 183  *     There are convenience methods for tracing method entries (the
 184  *     "entering" methods), method returns (the "exiting" methods) and
 185  *     throwing exceptions (the "throwing" methods).
 186  * <li><p>
 187  *     Finally, there are a set of convenience methods for use in the
 188  *     very simplest cases, when a developer simply wants to log a
 189  *     simple string at a given log level.  These methods are named
 190  *     after the standard Level names ("severe", "warning", "info", etc.)
 191  *     and take a single argument, a message string.
 192  * </ul>
 193  * <p>
 194  * For the methods that do not take an explicit source name and
 195  * method name, the Logging framework will make a "best effort"
 196  * to determine which class and method called into the logging method.
 197  * However, it is important to realize that this automatically inferred
 198  * information may only be approximate (or may even be quite wrong!).
 199  * Virtual machines are allowed to do extensive optimizations when
 200  * JITing and may entirely remove stack frames, making it impossible
 201  * to reliably locate the calling class and method.
 202  * <P>
 203  * All methods on Logger are multi-thread safe.
 204  * <p>
 205  * <b>Subclassing Information:</b> Note that a LogManager class may
 206  * provide its own implementation of named Loggers for any point in
 207  * the namespace.  Therefore, any subclasses of Logger (unless they
 208  * are implemented in conjunction with a new LogManager class) should
 209  * take care to obtain a Logger instance from the LogManager class and
 210  * should delegate operations such as "isLoggable" and "log(LogRecord)"
 211  * to that instance.  Note that in order to intercept all logging
 212  * output, subclasses need only override the log(LogRecord) method.
 213  * All the other logging methods are implemented as calls on this
 214  * log(LogRecord) method.
 215  *
 216  * @since 1.4
 217  */
 218 public class Logger {
 219     private static final Handler emptyHandlers[] = new Handler[0];
 220     private static final int offValue = Level.OFF.intValue();
 221 
 222     static final String SYSTEM_LOGGER_RB_NAME = "sun.util.logging.resources.logging";
 223 
 224     // This class is immutable and it is important that it remains so.
 225     private static final class LoggerBundle {
 226         final String resourceBundleName; // Base name of the bundle.
 227         final ResourceBundle userBundle; // Bundle set through setResourceBundle.
 228         private LoggerBundle(String resourceBundleName, ResourceBundle bundle) {
 229             this.resourceBundleName = resourceBundleName;
 230             this.userBundle = bundle;
 231         }
 232         boolean isSystemBundle() {
 233             return SYSTEM_LOGGER_RB_NAME.equals(resourceBundleName);
 234         }
 235         static LoggerBundle get(String name, ResourceBundle bundle) {
 236             if (name == null && bundle == null) {
 237                 return NO_RESOURCE_BUNDLE;
 238             } else if (SYSTEM_LOGGER_RB_NAME.equals(name) && bundle == null) {
 239                 return SYSTEM_BUNDLE;
 240             } else {
 241                 return new LoggerBundle(name, bundle);
 242             }
 243         }
 244     }
 245 
 246     // This instance will be shared by all loggers created by the system
 247     // code
 248     private static final LoggerBundle SYSTEM_BUNDLE =
 249             new LoggerBundle(SYSTEM_LOGGER_RB_NAME, null);
 250 
 251     // This instance indicates that no resource bundle has been specified yet,
 252     // and it will be shared by all loggers which have no resource bundle.
 253     private static final LoggerBundle NO_RESOURCE_BUNDLE =
 254             new LoggerBundle(null, null);
 255 
 256     private volatile LogManager manager;
 257     private String name;
 258     private final CopyOnWriteArrayList<Handler> handlers =
 259         new CopyOnWriteArrayList<>();
 260     private volatile LoggerBundle loggerBundle = NO_RESOURCE_BUNDLE;
 261     private volatile boolean useParentHandlers = true;
 262     private volatile Filter filter;
 263     private boolean anonymous;
 264 
 265     // Cache to speed up behavior of findResourceBundle:
 266     private ResourceBundle catalog;     // Cached resource bundle
 267     private String catalogName;         // name associated with catalog
 268     private Locale catalogLocale;       // locale associated with catalog
 269 
 270     // The fields relating to parent-child relationships and levels
 271     // are managed under a separate lock, the treeLock.
 272     private static final Object treeLock = new Object();
 273     // We keep weak references from parents to children, but strong
 274     // references from children to parents.
 275     private volatile Logger parent;    // our nearest parent.
 276     private ArrayList<LogManager.LoggerWeakRef> kids;   // WeakReferences to loggers that have us as parent
 277     private volatile Level levelObject;
 278     private volatile int levelValue;  // current effective level value
 279     private WeakReference<ClassLoader> callersClassLoaderRef;
 280 
 281     /**
 282      * GLOBAL_LOGGER_NAME is a name for the global logger.
 283      *
 284      * @since 1.6
 285      */
 286     public static final String GLOBAL_LOGGER_NAME = "global";
 287 
 288     /**
 289      * Return global logger object with the name Logger.GLOBAL_LOGGER_NAME.
 290      *
 291      * @return global logger object
 292      * @since 1.7
 293      */
 294     public static final Logger getGlobal() {
 295         // In order to break a cyclic dependence between the LogManager
 296         // and Logger static initializers causing deadlocks, the global
 297         // logger is created with a special constructor that does not
 298         // initialize its log manager.
 299         //
 300         // If an application calls Logger.getGlobal() before any logger
 301         // has been initialized, it is therefore possible that the
 302         // LogManager class has not been initialized yet, and therefore
 303         // Logger.global.manager will be null.
 304         //
 305         // In order to finish the initialization of the global logger, we
 306         // will therefore call LogManager.getLogManager() here.
 307         //
 308         // To prevent race conditions we also need to call
 309         // LogManager.getLogManager() unconditionally here.
 310         // Indeed we cannot rely on the observed value of global.manager,
 311         // because global.manager will become not null somewhere during
 312         // the initialization of LogManager.
 313         // If two threads are calling getGlobal() concurrently, one thread
 314         // will see global.manager null and call LogManager.getLogManager(),
 315         // but the other thread could come in at a time when global.manager
 316         // is already set although ensureLogManagerInitialized is not finished
 317         // yet...
 318         // Calling LogManager.getLogManager() unconditionally will fix that.
 319 
 320         LogManager.getLogManager();
 321 
 322         // Now the global LogManager should be initialized,
 323         // and the global logger should have been added to
 324         // it, unless we were called within the constructor of a LogManager
 325         // subclass installed as LogManager, in which case global.manager
 326         // would still be null, and global will be lazily initialized later on.
 327 
 328         return global;
 329     }
 330 
 331     /**
 332      * The "global" Logger object is provided as a convenience to developers
 333      * who are making casual use of the Logging package.  Developers
 334      * who are making serious use of the logging package (for example
 335      * in products) should create and use their own Logger objects,
 336      * with appropriate names, so that logging can be controlled on a
 337      * suitable per-Logger granularity. Developers also need to keep a
 338      * strong reference to their Logger objects to prevent them from
 339      * being garbage collected.
 340      * <p>
 341      * @deprecated Initialization of this field is prone to deadlocks.
 342      * The field must be initialized by the Logger class initialization
 343      * which may cause deadlocks with the LogManager class initialization.
 344      * In such cases two class initialization wait for each other to complete.
 345      * The preferred way to get the global logger object is via the call
 346      * <code>Logger.getGlobal()</code>.
 347      * For compatibility with old JDK versions where the
 348      * <code>Logger.getGlobal()</code> is not available use the call
 349      * <code>Logger.getLogger(Logger.GLOBAL_LOGGER_NAME)</code>
 350      * or <code>Logger.getLogger("global")</code>.
 351      */
 352     @Deprecated
 353     public static final Logger global = new Logger(GLOBAL_LOGGER_NAME);
 354 
 355     /**
 356      * Protected method to construct a logger for a named subsystem.
 357      * <p>
 358      * The logger will be initially configured with a null Level
 359      * and with useParentHandlers set to true.
 360      *
 361      * @param   name    A name for the logger.  This should
 362      *                          be a dot-separated name and should normally
 363      *                          be based on the package name or class name
 364      *                          of the subsystem, such as java.net
 365      *                          or javax.swing.  It may be null for anonymous Loggers.
 366      * @param   resourceBundleName  name of ResourceBundle to be used for localizing
 367      *                          messages for this logger.  May be null if none
 368      *                          of the messages require localization.
 369      * @throws MissingResourceException if the resourceBundleName is non-null and
 370      *             no corresponding resource can be found.
 371      */
 372     protected Logger(String name, String resourceBundleName) {
 373         this(name, resourceBundleName, null, LogManager.getLogManager());
 374     }
 375 
 376     Logger(String name, String resourceBundleName, Class<?> caller, LogManager manager) {
 377         this.manager = manager;
 378         assert this.loggerBundle == NO_RESOURCE_BUNDLE;
 379         setupResourceInfo(resourceBundleName, caller);
 380         this.name = name;
 381         levelValue = Level.INFO.intValue();
 382     }
 383 
 384     private void setCallersClassLoaderRef(Class<?> caller) {
 385         ClassLoader callersClassLoader = ((caller != null)
 386                                          ? caller.getClassLoader()
 387                                          : null);
 388         if (callersClassLoader != null) {
 389             this.callersClassLoaderRef = new WeakReference<>(callersClassLoader);
 390         }
 391     }
 392 
 393     private ClassLoader getCallersClassLoader() {
 394         return (callersClassLoaderRef != null)
 395                 ? callersClassLoaderRef.get()
 396                 : null;
 397     }
 398 
 399     // This constructor is used only to create the global Logger.
 400     // It is needed to break a cyclic dependence between the LogManager
 401     // and Logger static initializers causing deadlocks.
 402     private Logger(String name) {
 403         // The manager field is not initialized here.
 404         assert this.loggerBundle == NO_RESOURCE_BUNDLE;
 405         this.name = name;
 406         levelValue = Level.INFO.intValue();
 407     }
 408 
 409     // It is called from LoggerContext.addLocalLogger() when the logger
 410     // is actually added to a LogManager.
 411     void setLogManager(LogManager manager) {
 412         this.manager = manager;
 413     }
 414 
 415     private void checkPermission() throws SecurityException {
 416         if (!anonymous) {
 417             if (manager == null) {
 418                 // Complete initialization of the global Logger.
 419                 manager = LogManager.getLogManager();
 420             }
 421             manager.checkPermission();
 422         }
 423     }
 424 
 425     // Until all JDK code converted to call sun.util.logging.PlatformLogger
 426     // (see 7054233), we need to determine if Logger.getLogger is to add
 427     // a system logger or user logger.
 428     //
 429     // As an interim solution, if the immediate caller whose caller loader is
 430     // null, we assume it's a system logger and add it to the system context.
 431     // These system loggers only set the resource bundle to the given
 432     // resource bundle name (rather than the default system resource bundle).
 433     private static class SystemLoggerHelper {
 434         static boolean disableCallerCheck = getBooleanProperty("sun.util.logging.disableCallerCheck");
 435         private static boolean getBooleanProperty(final String key) {
 436             String s = AccessController.doPrivileged(new PrivilegedAction<String>() {
 437                 @Override
 438                 public String run() {
 439                     return System.getProperty(key);
 440                 }
 441             });
 442             return Boolean.valueOf(s);
 443         }
 444     }
 445 
 446     private static Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
 447         LogManager manager = LogManager.getLogManager();
 448         SecurityManager sm = System.getSecurityManager();
 449         if (sm != null && !SystemLoggerHelper.disableCallerCheck) {
 450             if (caller.getClassLoader() == null) {
 451                 return manager.demandSystemLogger(name, resourceBundleName);
 452             }
 453         }
 454         return manager.demandLogger(name, resourceBundleName, caller);
 455         // ends up calling new Logger(name, resourceBundleName, caller)
 456         // iff the logger doesn't exist already
 457     }
 458 
 459     /**
 460      * Find or create a logger for a named subsystem.  If a logger has
 461      * already been created with the given name it is returned.  Otherwise
 462      * a new logger is created.
 463      * <p>
 464      * If a new logger is created its log level will be configured
 465      * based on the LogManager configuration and it will configured
 466      * to also send logging output to its parent's Handlers.  It will
 467      * be registered in the LogManager global namespace.
 468      * <p>
 469      * Note: The LogManager may only retain a weak reference to the newly
 470      * created Logger. It is important to understand that a previously
 471      * created Logger with the given name may be garbage collected at any
 472      * time if there is no strong reference to the Logger. In particular,
 473      * this means that two back-to-back calls like
 474      * {@code getLogger("MyLogger").log(...)} may use different Logger
 475      * objects named "MyLogger" if there is no strong reference to the
 476      * Logger named "MyLogger" elsewhere in the program.
 477      *
 478      * @param   name            A name for the logger.  This should
 479      *                          be a dot-separated name and should normally
 480      *                          be based on the package name or class name
 481      *                          of the subsystem, such as java.net
 482      *                          or javax.swing
 483      * @return a suitable Logger
 484      * @throws NullPointerException if the name is null.
 485      */
 486 
 487     // Synchronization is not required here. All synchronization for
 488     // adding a new Logger object is handled by LogManager.addLogger().
 489     @CallerSensitive
 490     public static Logger getLogger(String name) {
 491         // This method is intentionally not a wrapper around a call
 492         // to getLogger(name, resourceBundleName). If it were then
 493         // this sequence:
 494         //
 495         //     getLogger("Foo", "resourceBundleForFoo");
 496         //     getLogger("Foo");
 497         //
 498         // would throw an IllegalArgumentException in the second call
 499         // because the wrapper would result in an attempt to replace
 500         // the existing "resourceBundleForFoo" with null.
 501         return demandLogger(name, null, Reflection.getCallerClass());
 502     }
 503 
 504     /**
 505      * Find or create a logger for a named subsystem.  If a logger has
 506      * already been created with the given name it is returned.  Otherwise
 507      * a new logger is created.
 508      * <p>
 509      * If a new logger is created its log level will be configured
 510      * based on the LogManager and it will configured to also send logging
 511      * output to its parent's Handlers.  It will be registered in
 512      * the LogManager global namespace.
 513      * <p>
 514      * Note: The LogManager may only retain a weak reference to the newly
 515      * created Logger. It is important to understand that a previously
 516      * created Logger with the given name may be garbage collected at any
 517      * time if there is no strong reference to the Logger. In particular,
 518      * this means that two back-to-back calls like
 519      * {@code getLogger("MyLogger", ...).log(...)} may use different Logger
 520      * objects named "MyLogger" if there is no strong reference to the
 521      * Logger named "MyLogger" elsewhere in the program.
 522      * <p>
 523      * If the named Logger already exists and does not yet have a
 524      * localization resource bundle then the given resource bundle
 525      * name is used.  If the named Logger already exists and has
 526      * a different resource bundle name then an IllegalArgumentException
 527      * is thrown.
 528      * <p>
 529      * @param   name    A name for the logger.  This should
 530      *                          be a dot-separated name and should normally
 531      *                          be based on the package name or class name
 532      *                          of the subsystem, such as java.net
 533      *                          or javax.swing
 534      * @param   resourceBundleName  name of ResourceBundle to be used for localizing
 535      *                          messages for this logger. May be {@code null}
 536      *                          if none of the messages require localization.
 537      * @return a suitable Logger
 538      * @throws MissingResourceException if the resourceBundleName is non-null and
 539      *             no corresponding resource can be found.
 540      * @throws IllegalArgumentException if the Logger already exists and uses
 541      *             a different resource bundle name; or if
 542      *             {@code resourceBundleName} is {@code null} but the named
 543      *             logger has a resource bundle set.
 544      * @throws NullPointerException if the name is null.
 545      */
 546 
 547     // Synchronization is not required here. All synchronization for
 548     // adding a new Logger object is handled by LogManager.addLogger().
 549     @CallerSensitive
 550     public static Logger getLogger(String name, String resourceBundleName) {
 551         Class<?> callerClass = Reflection.getCallerClass();
 552         Logger result = demandLogger(name, resourceBundleName, callerClass);
 553 
 554         // MissingResourceException or IllegalArgumentException can be
 555         // thrown by setupResourceInfo().
 556         // We have to set the callers ClassLoader here in case demandLogger
 557         // above found a previously created Logger.  This can happen, for
 558         // example, if Logger.getLogger(name) is called and subsequently
 559         // Logger.getLogger(name, resourceBundleName) is called.  In this case
 560         // we won't necessarily have the correct classloader saved away, so
 561         // we need to set it here, too.
 562 
 563         result.setupResourceInfo(resourceBundleName, callerClass);
 564         return result;
 565     }
 566 
 567     // package-private
 568     // Add a platform logger to the system context.
 569     // i.e. caller of sun.util.logging.PlatformLogger.getLogger
 570     static Logger getPlatformLogger(String name) {
 571         LogManager manager = LogManager.getLogManager();
 572 
 573         // all loggers in the system context will default to
 574         // the system logger's resource bundle
 575         Logger result = manager.demandSystemLogger(name, SYSTEM_LOGGER_RB_NAME);
 576         return result;
 577     }
 578 
 579     /**
 580      * Create an anonymous Logger.  The newly created Logger is not
 581      * registered in the LogManager namespace.  There will be no
 582      * access checks on updates to the logger.
 583      * <p>
 584      * This factory method is primarily intended for use from applets.
 585      * Because the resulting Logger is anonymous it can be kept private
 586      * by the creating class.  This removes the need for normal security
 587      * checks, which in turn allows untrusted applet code to update
 588      * the control state of the Logger.  For example an applet can do
 589      * a setLevel or an addHandler on an anonymous Logger.
 590      * <p>
 591      * Even although the new logger is anonymous, it is configured
 592      * to have the root logger ("") as its parent.  This means that
 593      * by default it inherits its effective level and handlers
 594      * from the root logger. Changing its parent via the
 595      * {@link #setParent(java.util.logging.Logger) setParent} method
 596      * will still require the security permission specified by that method.
 597      * <p>
 598      *
 599      * @return a newly created private Logger
 600      */
 601     public static Logger getAnonymousLogger() {
 602         return getAnonymousLogger(null);
 603     }
 604 
 605     /**
 606      * Create an anonymous Logger.  The newly created Logger is not
 607      * registered in the LogManager namespace.  There will be no
 608      * access checks on updates to the logger.
 609      * <p>
 610      * This factory method is primarily intended for use from applets.
 611      * Because the resulting Logger is anonymous it can be kept private
 612      * by the creating class.  This removes the need for normal security
 613      * checks, which in turn allows untrusted applet code to update
 614      * the control state of the Logger.  For example an applet can do
 615      * a setLevel or an addHandler on an anonymous Logger.
 616      * <p>
 617      * Even although the new logger is anonymous, it is configured
 618      * to have the root logger ("") as its parent.  This means that
 619      * by default it inherits its effective level and handlers
 620      * from the root logger.  Changing its parent via the
 621      * {@link #setParent(java.util.logging.Logger) setParent} method
 622      * will still require the security permission specified by that method.
 623      * <p>
 624      * @param   resourceBundleName  name of ResourceBundle to be used for localizing
 625      *                          messages for this logger.
 626      *          May be null if none of the messages require localization.
 627      * @return a newly created private Logger
 628      * @throws MissingResourceException if the resourceBundleName is non-null and
 629      *             no corresponding resource can be found.
 630      */
 631 
 632     // Synchronization is not required here. All synchronization for
 633     // adding a new anonymous Logger object is handled by doSetParent().
 634     @CallerSensitive
 635     public static Logger getAnonymousLogger(String resourceBundleName) {
 636         LogManager manager = LogManager.getLogManager();
 637         // cleanup some Loggers that have been GC'ed
 638         manager.drainLoggerRefQueueBounded();
 639         Logger result = new Logger(null, resourceBundleName,
 640                                    Reflection.getCallerClass(), manager);
 641         result.anonymous = true;
 642         Logger root = manager.getLogger("");
 643         result.doSetParent(root);
 644         return result;
 645     }
 646 
 647     /**
 648      * Retrieve the localization resource bundle for this
 649      * logger.
 650      * This method will return a {@code ResourceBundle} that was either
 651      * set by the {@link
 652      * #setResourceBundle(java.util.ResourceBundle) setResourceBundle} method or
 653      * <a href="#ResourceBundleMapping">mapped from the
 654      * the resource bundle name</a> set via the {@link
 655      * Logger#getLogger(java.lang.String, java.lang.String) getLogger} factory
 656      * method for the current default locale.
 657      * <br>Note that if the result is {@code null}, then the Logger will use a resource
 658      * bundle or resource bundle name inherited from its parent.
 659      *
 660      * @return localization bundle (may be {@code null})
 661      */
 662     public ResourceBundle getResourceBundle() {
 663         return findResourceBundle(getResourceBundleName(), true);
 664     }
 665 
 666     /**
 667      * Retrieve the localization resource bundle name for this
 668      * logger.
 669      * This is either the name specified through the {@link
 670      * #getLogger(java.lang.String, java.lang.String) getLogger} factory method,
 671      * or the {@linkplain ResourceBundle#getBaseBundleName() base name} of the
 672      * ResourceBundle set through {@link
 673      * #setResourceBundle(java.util.ResourceBundle) setResourceBundle} method.
 674      * <br>Note that if the result is {@code null}, then the Logger will use a resource
 675      * bundle or resource bundle name inherited from its parent.
 676      *
 677      * @return localization bundle name (may be {@code null})
 678      */
 679     public String getResourceBundleName() {
 680         return loggerBundle.resourceBundleName;
 681     }
 682 
 683     /**
 684      * Set a filter to control output on this Logger.
 685      * <P>
 686      * After passing the initial "level" check, the Logger will
 687      * call this Filter to check if a log record should really
 688      * be published.
 689      *
 690      * @param   newFilter  a filter object (may be null)
 691      * @throws  SecurityException if a security manager exists,
 692      *          this logger is not anonymous, and the caller
 693      *          does not have LoggingPermission("control").
 694      */
 695     public void setFilter(Filter newFilter) throws SecurityException {
 696         checkPermission();
 697         filter = newFilter;
 698     }
 699 
 700     /**
 701      * Get the current filter for this Logger.
 702      *
 703      * @return  a filter object (may be null)
 704      */
 705     public Filter getFilter() {
 706         return filter;
 707     }
 708 
 709     /**
 710      * Log a LogRecord.
 711      * <p>
 712      * All the other logging methods in this class call through
 713      * this method to actually perform any logging.  Subclasses can
 714      * override this single method to capture all log activity.
 715      *
 716      * @param record the LogRecord to be published
 717      */
 718     public void log(LogRecord record) {
 719         if (!isLoggable(record.getLevel())) {
 720             return;
 721         }
 722         Filter theFilter = filter;
 723         if (theFilter != null && !theFilter.isLoggable(record)) {
 724             return;
 725         }
 726 
 727         // Post the LogRecord to all our Handlers, and then to
 728         // our parents' handlers, all the way up the tree.
 729 
 730         Logger logger = this;
 731         while (logger != null) {
 732             for (Handler handler : logger.getHandlers()) {
 733                 handler.publish(record);
 734             }
 735 
 736             if (!logger.getUseParentHandlers()) {
 737                 break;
 738             }
 739 
 740             logger = logger.getParent();
 741         }
 742     }
 743 
 744     // private support method for logging.
 745     // We fill in the logger name, resource bundle name, and
 746     // resource bundle and then call "void log(LogRecord)".
 747     private void doLog(LogRecord lr) {
 748         lr.setLoggerName(name);
 749         final LoggerBundle lb = getEffectiveLoggerBundle();
 750         final ResourceBundle  bundle = lb.userBundle;
 751         final String ebname = lb.resourceBundleName;
 752         if (ebname != null && bundle != null) {
 753             lr.setResourceBundleName(ebname);
 754             lr.setResourceBundle(bundle);
 755         }
 756         log(lr);
 757     }
 758 
 759 
 760     //================================================================
 761     // Start of convenience methods WITHOUT className and methodName
 762     //================================================================
 763 
 764     /**
 765      * Log a message, with no arguments.
 766      * <p>
 767      * If the logger is currently enabled for the given message
 768      * level then the given message is forwarded to all the
 769      * registered output Handler objects.
 770      * <p>
 771      * @param   level   One of the message level identifiers, e.g., SEVERE
 772      * @param   msg     The string message (or a key in the message catalog)
 773      */
 774     public void log(Level level, String msg) {
 775         if (!isLoggable(level)) {
 776             return;
 777         }
 778         LogRecord lr = new LogRecord(level, msg);
 779         doLog(lr);
 780     }
 781 
 782     /**
 783      * Log a message, which is only to be constructed if the logging level
 784      * is such that the message will actually be logged.
 785      * <p>
 786      * If the logger is currently enabled for the given message
 787      * level then the message is constructed by invoking the provided
 788      * supplier function and forwarded to all the registered output
 789      * Handler objects.
 790      * <p>
 791      * @param   level   One of the message level identifiers, e.g., SEVERE
 792      * @param   msgSupplier   A function, which when called, produces the
 793      *                        desired log message
 794      */
 795     public void log(Level level, Supplier<String> msgSupplier) {
 796         if (!isLoggable(level)) {
 797             return;
 798         }
 799         LogRecord lr = new LogRecord(level, msgSupplier.get());
 800         doLog(lr);
 801     }
 802 
 803     /**
 804      * Log a message, with one object parameter.
 805      * <p>
 806      * If the logger is currently enabled for the given message
 807      * level then a corresponding LogRecord is created and forwarded
 808      * to all the registered output Handler objects.
 809      * <p>
 810      * @param   level   One of the message level identifiers, e.g., SEVERE
 811      * @param   msg     The string message (or a key in the message catalog)
 812      * @param   param1  parameter to the message
 813      */
 814     public void log(Level level, String msg, Object param1) {
 815         if (!isLoggable(level)) {
 816             return;
 817         }
 818         LogRecord lr = new LogRecord(level, msg);
 819         Object params[] = { param1 };
 820         lr.setParameters(params);
 821         doLog(lr);
 822     }
 823 
 824     /**
 825      * Log a message, with an array of object arguments.
 826      * <p>
 827      * If the logger is currently enabled for the given message
 828      * level then a corresponding LogRecord is created and forwarded
 829      * to all the registered output Handler objects.
 830      * <p>
 831      * @param   level   One of the message level identifiers, e.g., SEVERE
 832      * @param   msg     The string message (or a key in the message catalog)
 833      * @param   params  array of parameters to the message
 834      */
 835     public void log(Level level, String msg, Object params[]) {
 836         if (!isLoggable(level)) {
 837             return;
 838         }
 839         LogRecord lr = new LogRecord(level, msg);
 840         lr.setParameters(params);
 841         doLog(lr);
 842     }
 843 
 844     /**
 845      * Log a message, with associated Throwable information.
 846      * <p>
 847      * If the logger is currently enabled for the given message
 848      * level then the given arguments are stored in a LogRecord
 849      * which is forwarded to all registered output handlers.
 850      * <p>
 851      * Note that the thrown argument is stored in the LogRecord thrown
 852      * property, rather than the LogRecord parameters property.  Thus it is
 853      * processed specially by output Formatters and is not treated
 854      * as a formatting parameter to the LogRecord message property.
 855      * <p>
 856      * @param   level   One of the message level identifiers, e.g., SEVERE
 857      * @param   msg     The string message (or a key in the message catalog)
 858      * @param   thrown  Throwable associated with log message.
 859      */
 860     public void log(Level level, String msg, Throwable thrown) {
 861         if (!isLoggable(level)) {
 862             return;
 863         }
 864         LogRecord lr = new LogRecord(level, msg);
 865         lr.setThrown(thrown);
 866         doLog(lr);
 867     }
 868 
 869     /**
 870      * Log a lazily constructed message, with associated Throwable information.
 871      * <p>
 872      * If the logger is currently enabled for the given message level then the
 873      * message is constructed by invoking the provided supplier function. The
 874      * message and the given {@link Throwable} are then stored in a {@link
 875      * LogRecord} which is forwarded to all registered output handlers.
 876      * <p>
 877      * Note that the thrown argument is stored in the LogRecord thrown
 878      * property, rather than the LogRecord parameters property.  Thus it is
 879      * processed specially by output Formatters and is not treated
 880      * as a formatting parameter to the LogRecord message property.
 881      * <p>
 882      * @param   level   One of the message level identifiers, e.g., SEVERE
 883      * @param   thrown  Throwable associated with log message.
 884      * @param   msgSupplier   A function, which when called, produces the
 885      *                        desired log message
 886      * @since   1.8
 887      */
 888     public void log(Level level, Throwable thrown, Supplier<String> msgSupplier) {
 889         if (!isLoggable(level)) {
 890             return;
 891         }
 892         LogRecord lr = new LogRecord(level, msgSupplier.get());
 893         lr.setThrown(thrown);
 894         doLog(lr);
 895     }
 896 
 897     //================================================================
 898     // Start of convenience methods WITH className and methodName
 899     //================================================================
 900 
 901     /**
 902      * Log a message, specifying source class and method,
 903      * with no arguments.
 904      * <p>
 905      * If the logger is currently enabled for the given message
 906      * level then the given message is forwarded to all the
 907      * registered output Handler objects.
 908      * <p>
 909      * @param   level   One of the message level identifiers, e.g., SEVERE
 910      * @param   sourceClass    name of class that issued the logging request
 911      * @param   sourceMethod   name of method that issued the logging request
 912      * @param   msg     The string message (or a key in the message catalog)
 913      */
 914     public void logp(Level level, String sourceClass, String sourceMethod, String msg) {
 915         if (!isLoggable(level)) {
 916             return;
 917         }
 918         LogRecord lr = new LogRecord(level, msg);
 919         lr.setSourceClassName(sourceClass);
 920         lr.setSourceMethodName(sourceMethod);
 921         doLog(lr);
 922     }
 923 
 924     /**
 925      * Log a lazily constructed message, specifying source class and method,
 926      * with no arguments.
 927      * <p>
 928      * If the logger is currently enabled for the given message
 929      * level then the message is constructed by invoking the provided
 930      * supplier function and forwarded to all the registered output
 931      * Handler objects.
 932      * <p>
 933      * @param   level   One of the message level identifiers, e.g., SEVERE
 934      * @param   sourceClass    name of class that issued the logging request
 935      * @param   sourceMethod   name of method that issued the logging request
 936      * @param   msgSupplier   A function, which when called, produces the
 937      *                        desired log message
 938      * @since   1.8
 939      */
 940     public void logp(Level level, String sourceClass, String sourceMethod,
 941                      Supplier<String> msgSupplier) {
 942         if (!isLoggable(level)) {
 943             return;
 944         }
 945         LogRecord lr = new LogRecord(level, msgSupplier.get());
 946         lr.setSourceClassName(sourceClass);
 947         lr.setSourceMethodName(sourceMethod);
 948         doLog(lr);
 949     }
 950 
 951     /**
 952      * Log a message, specifying source class and method,
 953      * with a single object parameter to the log message.
 954      * <p>
 955      * If the logger is currently enabled for the given message
 956      * level then a corresponding LogRecord is created and forwarded
 957      * to all the registered output Handler objects.
 958      * <p>
 959      * @param   level   One of the message level identifiers, e.g., SEVERE
 960      * @param   sourceClass    name of class that issued the logging request
 961      * @param   sourceMethod   name of method that issued the logging request
 962      * @param   msg      The string message (or a key in the message catalog)
 963      * @param   param1    Parameter to the log message.
 964      */
 965     public void logp(Level level, String sourceClass, String sourceMethod,
 966                                                 String msg, Object param1) {
 967         if (!isLoggable(level)) {
 968             return;
 969         }
 970         LogRecord lr = new LogRecord(level, msg);
 971         lr.setSourceClassName(sourceClass);
 972         lr.setSourceMethodName(sourceMethod);
 973         Object params[] = { param1 };
 974         lr.setParameters(params);
 975         doLog(lr);
 976     }
 977 
 978     /**
 979      * Log a message, specifying source class and method,
 980      * with an array of object arguments.
 981      * <p>
 982      * If the logger is currently enabled for the given message
 983      * level then a corresponding LogRecord is created and forwarded
 984      * to all the registered output Handler objects.
 985      * <p>
 986      * @param   level   One of the message level identifiers, e.g., SEVERE
 987      * @param   sourceClass    name of class that issued the logging request
 988      * @param   sourceMethod   name of method that issued the logging request
 989      * @param   msg     The string message (or a key in the message catalog)
 990      * @param   params  Array of parameters to the message
 991      */
 992     public void logp(Level level, String sourceClass, String sourceMethod,
 993                                                 String msg, Object params[]) {
 994         if (!isLoggable(level)) {
 995             return;
 996         }
 997         LogRecord lr = new LogRecord(level, msg);
 998         lr.setSourceClassName(sourceClass);
 999         lr.setSourceMethodName(sourceMethod);
1000         lr.setParameters(params);
1001         doLog(lr);
1002     }
1003 
1004     /**
1005      * Log a message, specifying source class and method,
1006      * with associated Throwable information.
1007      * <p>
1008      * If the logger is currently enabled for the given message
1009      * level then the given arguments are stored in a LogRecord
1010      * which is forwarded to all registered output handlers.
1011      * <p>
1012      * Note that the thrown argument is stored in the LogRecord thrown
1013      * property, rather than the LogRecord parameters property.  Thus it is
1014      * processed specially by output Formatters and is not treated
1015      * as a formatting parameter to the LogRecord message property.
1016      * <p>
1017      * @param   level   One of the message level identifiers, e.g., SEVERE
1018      * @param   sourceClass    name of class that issued the logging request
1019      * @param   sourceMethod   name of method that issued the logging request
1020      * @param   msg     The string message (or a key in the message catalog)
1021      * @param   thrown  Throwable associated with log message.
1022      */
1023     public void logp(Level level, String sourceClass, String sourceMethod,
1024                      String msg, Throwable thrown) {
1025         if (!isLoggable(level)) {
1026             return;
1027         }
1028         LogRecord lr = new LogRecord(level, msg);
1029         lr.setSourceClassName(sourceClass);
1030         lr.setSourceMethodName(sourceMethod);
1031         lr.setThrown(thrown);
1032         doLog(lr);
1033     }
1034 
1035     /**
1036      * Log a lazily constructed message, specifying source class and method,
1037      * with associated Throwable information.
1038      * <p>
1039      * If the logger is currently enabled for the given message level then the
1040      * message is constructed by invoking the provided supplier function. The
1041      * message and the given {@link Throwable} are then stored in a {@link
1042      * LogRecord} which is forwarded to all registered output handlers.
1043      * <p>
1044      * Note that the thrown argument is stored in the LogRecord thrown
1045      * property, rather than the LogRecord parameters property.  Thus it is
1046      * processed specially by output Formatters and is not treated
1047      * as a formatting parameter to the LogRecord message property.
1048      * <p>
1049      * @param   level   One of the message level identifiers, e.g., SEVERE
1050      * @param   sourceClass    name of class that issued the logging request
1051      * @param   sourceMethod   name of method that issued the logging request
1052      * @param   thrown  Throwable associated with log message.
1053      * @param   msgSupplier   A function, which when called, produces the
1054      *                        desired log message
1055      * @since   1.8
1056      */
1057     public void logp(Level level, String sourceClass, String sourceMethod,
1058                      Throwable thrown, Supplier<String> msgSupplier) {
1059         if (!isLoggable(level)) {
1060             return;
1061         }
1062         LogRecord lr = new LogRecord(level, msgSupplier.get());
1063         lr.setSourceClassName(sourceClass);
1064         lr.setSourceMethodName(sourceMethod);
1065         lr.setThrown(thrown);
1066         doLog(lr);
1067     }
1068 
1069 
1070     //=========================================================================
1071     // Start of convenience methods WITH className, methodName and bundle name.
1072     //=========================================================================
1073 
1074     // Private support method for logging for "logrb" methods.
1075     // We fill in the logger name, resource bundle name, and
1076     // resource bundle and then call "void log(LogRecord)".
1077     private void doLog(LogRecord lr, String rbname) {
1078         lr.setLoggerName(name);
1079         if (rbname != null) {
1080             lr.setResourceBundleName(rbname);
1081             lr.setResourceBundle(findResourceBundle(rbname, false));
1082         }
1083         log(lr);
1084     }
1085 
1086     // Private support method for logging for "logrb" methods.
1087     private void doLog(LogRecord lr, ResourceBundle rb) {
1088         lr.setLoggerName(name);
1089         if (rb != null) {
1090             lr.setResourceBundleName(rb.getBaseBundleName());
1091             lr.setResourceBundle(rb);
1092         }
1093         log(lr);
1094     }
1095 
1096     /**
1097      * Log a message, specifying source class, method, and resource bundle name
1098      * with no arguments.
1099      * <p>
1100      * If the logger is currently enabled for the given message
1101      * level then the given message is forwarded to all the
1102      * registered output Handler objects.
1103      * <p>
1104      * The msg string is localized using the named resource bundle.  If the
1105      * resource bundle name is null, or an empty String or invalid
1106      * then the msg string is not localized.
1107      * <p>
1108      * @param   level   One of the message level identifiers, e.g., SEVERE
1109      * @param   sourceClass    name of class that issued the logging request
1110      * @param   sourceMethod   name of method that issued the logging request
1111      * @param   bundleName     name of resource bundle to localize msg,
1112      *                         can be null
1113      * @param   msg     The string message (or a key in the message catalog)
1114      * @deprecated Use {@link #logrb(java.util.logging.Level, java.lang.String,
1115      * java.lang.String, java.util.ResourceBundle, java.lang.String,
1116      * java.lang.Object...)} instead.
1117      */
1118     @Deprecated
1119     public void logrb(Level level, String sourceClass, String sourceMethod,
1120                                 String bundleName, String msg) {
1121         if (!isLoggable(level)) {
1122             return;
1123         }
1124         LogRecord lr = new LogRecord(level, msg);
1125         lr.setSourceClassName(sourceClass);
1126         lr.setSourceMethodName(sourceMethod);
1127         doLog(lr, bundleName);
1128     }
1129 
1130     /**
1131      * Log a message, specifying source class, method, and resource bundle name,
1132      * with a single object parameter to the log message.
1133      * <p>
1134      * If the logger is currently enabled for the given message
1135      * level then a corresponding LogRecord is created and forwarded
1136      * to all the registered output Handler objects.
1137      * <p>
1138      * The msg string is localized using the named resource bundle.  If the
1139      * resource bundle name is null, or an empty String or invalid
1140      * then the msg string is not localized.
1141      * <p>
1142      * @param   level   One of the message level identifiers, e.g., SEVERE
1143      * @param   sourceClass    name of class that issued the logging request
1144      * @param   sourceMethod   name of method that issued the logging request
1145      * @param   bundleName     name of resource bundle to localize msg,
1146      *                         can be null
1147      * @param   msg      The string message (or a key in the message catalog)
1148      * @param   param1    Parameter to the log message.
1149      * @deprecated Use {@link #logrb(java.util.logging.Level, java.lang.String,
1150      *   java.lang.String, java.util.ResourceBundle, java.lang.String,
1151      *   java.lang.Object...)} instead
1152      */
1153     @Deprecated
1154     public void logrb(Level level, String sourceClass, String sourceMethod,
1155                                 String bundleName, String msg, Object param1) {
1156         if (!isLoggable(level)) {
1157             return;
1158         }
1159         LogRecord lr = new LogRecord(level, msg);
1160         lr.setSourceClassName(sourceClass);
1161         lr.setSourceMethodName(sourceMethod);
1162         Object params[] = { param1 };
1163         lr.setParameters(params);
1164         doLog(lr, bundleName);
1165     }
1166 
1167     /**
1168      * Log a message, specifying source class, method, and resource bundle name,
1169      * with an array of object arguments.
1170      * <p>
1171      * If the logger is currently enabled for the given message
1172      * level then a corresponding LogRecord is created and forwarded
1173      * to all the registered output Handler objects.
1174      * <p>
1175      * The msg string is localized using the named resource bundle.  If the
1176      * resource bundle name is null, or an empty String or invalid
1177      * then the msg string is not localized.
1178      * <p>
1179      * @param   level   One of the message level identifiers, e.g., SEVERE
1180      * @param   sourceClass    name of class that issued the logging request
1181      * @param   sourceMethod   name of method that issued the logging request
1182      * @param   bundleName     name of resource bundle to localize msg,
1183      *                         can be null.
1184      * @param   msg     The string message (or a key in the message catalog)
1185      * @param   params  Array of parameters to the message
1186      * @deprecated Use {@link #logrb(java.util.logging.Level, java.lang.String,
1187      *      java.lang.String, java.util.ResourceBundle, java.lang.String,
1188      *      java.lang.Object...)} instead.
1189      */
1190     @Deprecated
1191     public void logrb(Level level, String sourceClass, String sourceMethod,
1192                                 String bundleName, String msg, Object params[]) {
1193         if (!isLoggable(level)) {
1194             return;
1195         }
1196         LogRecord lr = new LogRecord(level, msg);
1197         lr.setSourceClassName(sourceClass);
1198         lr.setSourceMethodName(sourceMethod);
1199         lr.setParameters(params);
1200         doLog(lr, bundleName);
1201     }
1202 
1203     /**
1204      * Log a message, specifying source class, method, and resource bundle,
1205      * with an optional list of message parameters.
1206      * <p>
1207      * If the logger is currently enabled for the given message
1208      * level then a corresponding LogRecord is created and forwarded
1209      * to all the registered output Handler objects.
1210      * <p>
1211      * The {@code msg} string is localized using the given resource bundle.
1212      * If the resource bundle is {@code null}, then the {@code msg} string is not
1213      * localized.
1214      * <p>
1215      * @param   level   One of the message level identifiers, e.g., SEVERE
1216      * @param   sourceClass    Name of the class that issued the logging request
1217      * @param   sourceMethod   Name of the method that issued the logging request
1218      * @param   bundle         Resource bundle to localize {@code msg},
1219      *                         can be {@code null}.
1220      * @param   msg     The string message (or a key in the message catalog)
1221      * @param   params  Parameters to the message (optional, may be none).
1222      * @since 1.8
1223      */
1224     public void logrb(Level level, String sourceClass, String sourceMethod,
1225                       ResourceBundle bundle, String msg, Object... params) {
1226         if (!isLoggable(level)) {
1227             return;
1228         }
1229         LogRecord lr = new LogRecord(level, msg);
1230         lr.setSourceClassName(sourceClass);
1231         lr.setSourceMethodName(sourceMethod);
1232         if (params != null && params.length != 0) {
1233             lr.setParameters(params);
1234         }
1235         doLog(lr, bundle);
1236     }
1237 
1238     /**
1239      * Log a message, specifying source class, method, and resource bundle name,
1240      * with associated Throwable information.
1241      * <p>
1242      * If the logger is currently enabled for the given message
1243      * level then the given arguments are stored in a LogRecord
1244      * which is forwarded to all registered output handlers.
1245      * <p>
1246      * The msg string is localized using the named resource bundle.  If the
1247      * resource bundle name is null, or an empty String or invalid
1248      * then the msg string is not localized.
1249      * <p>
1250      * Note that the thrown argument is stored in the LogRecord thrown
1251      * property, rather than the LogRecord parameters property.  Thus it is
1252      * processed specially by output Formatters and is not treated
1253      * as a formatting parameter to the LogRecord message property.
1254      * <p>
1255      * @param   level   One of the message level identifiers, e.g., SEVERE
1256      * @param   sourceClass    name of class that issued the logging request
1257      * @param   sourceMethod   name of method that issued the logging request
1258      * @param   bundleName     name of resource bundle to localize msg,
1259      *                         can be null
1260      * @param   msg     The string message (or a key in the message catalog)
1261      * @param   thrown  Throwable associated with log message.
1262      * @deprecated Use {@link #logrb(java.util.logging.Level, java.lang.String,
1263      *     java.lang.String, java.util.ResourceBundle, java.lang.String,
1264      *     java.lang.Throwable)} instead.
1265      */
1266     @Deprecated
1267     public void logrb(Level level, String sourceClass, String sourceMethod,
1268                                         String bundleName, String msg, Throwable thrown) {
1269         if (!isLoggable(level)) {
1270             return;
1271         }
1272         LogRecord lr = new LogRecord(level, msg);
1273         lr.setSourceClassName(sourceClass);
1274         lr.setSourceMethodName(sourceMethod);
1275         lr.setThrown(thrown);
1276         doLog(lr, bundleName);
1277     }
1278 
1279     /**
1280      * Log a message, specifying source class, method, and resource bundle,
1281      * with associated Throwable information.
1282      * <p>
1283      * If the logger is currently enabled for the given message
1284      * level then the given arguments are stored in a LogRecord
1285      * which is forwarded to all registered output handlers.
1286      * <p>
1287      * The {@code msg} string is localized using the given resource bundle.
1288      * If the resource bundle is {@code null}, then the {@code msg} string is not
1289      * localized.
1290      * <p>
1291      * Note that the thrown argument is stored in the LogRecord thrown
1292      * property, rather than the LogRecord parameters property.  Thus it is
1293      * processed specially by output Formatters and is not treated
1294      * as a formatting parameter to the LogRecord message property.
1295      * <p>
1296      * @param   level   One of the message level identifiers, e.g., SEVERE
1297      * @param   sourceClass    Name of the class that issued the logging request
1298      * @param   sourceMethod   Name of the method that issued the logging request
1299      * @param   bundle         Resource bundle to localize {@code msg},
1300      *                         can be {@code null}
1301      * @param   msg     The string message (or a key in the message catalog)
1302      * @param   thrown  Throwable associated with the log message.
1303      * @since 1.8
1304      */
1305     public void logrb(Level level, String sourceClass, String sourceMethod,
1306                       ResourceBundle bundle, String msg, Throwable thrown) {
1307         if (!isLoggable(level)) {
1308             return;
1309         }
1310         LogRecord lr = new LogRecord(level, msg);
1311         lr.setSourceClassName(sourceClass);
1312         lr.setSourceMethodName(sourceMethod);
1313         lr.setThrown(thrown);
1314         doLog(lr, bundle);
1315     }
1316 
1317     //======================================================================
1318     // Start of convenience methods for logging method entries and returns.
1319     //======================================================================
1320 
1321     /**
1322      * Log a method entry.
1323      * <p>
1324      * This is a convenience method that can be used to log entry
1325      * to a method.  A LogRecord with message "ENTRY", log level
1326      * FINER, and the given sourceMethod and sourceClass is logged.
1327      * <p>
1328      * @param   sourceClass    name of class that issued the logging request
1329      * @param   sourceMethod   name of method that is being entered
1330      */
1331     public void entering(String sourceClass, String sourceMethod) {
1332         logp(Level.FINER, sourceClass, sourceMethod, "ENTRY");
1333     }
1334 
1335     /**
1336      * Log a method entry, with one parameter.
1337      * <p>
1338      * This is a convenience method that can be used to log entry
1339      * to a method.  A LogRecord with message "ENTRY {0}", log level
1340      * FINER, and the given sourceMethod, sourceClass, and parameter
1341      * is logged.
1342      * <p>
1343      * @param   sourceClass    name of class that issued the logging request
1344      * @param   sourceMethod   name of method that is being entered
1345      * @param   param1         parameter to the method being entered
1346      */
1347     public void entering(String sourceClass, String sourceMethod, Object param1) {
1348         logp(Level.FINER, sourceClass, sourceMethod, "ENTRY {0}", param1);
1349     }
1350 
1351     /**
1352      * Log a method entry, with an array of parameters.
1353      * <p>
1354      * This is a convenience method that can be used to log entry
1355      * to a method.  A LogRecord with message "ENTRY" (followed by a
1356      * format {N} indicator for each entry in the parameter array),
1357      * log level FINER, and the given sourceMethod, sourceClass, and
1358      * parameters is logged.
1359      * <p>
1360      * @param   sourceClass    name of class that issued the logging request
1361      * @param   sourceMethod   name of method that is being entered
1362      * @param   params         array of parameters to the method being entered
1363      */
1364     public void entering(String sourceClass, String sourceMethod, Object params[]) {
1365         String msg = "ENTRY";
1366         if (params == null ) {
1367            logp(Level.FINER, sourceClass, sourceMethod, msg);
1368            return;
1369         }
1370         if (!isLoggable(Level.FINER)) return;
1371         for (int i = 0; i < params.length; i++) {
1372             msg = msg + " {" + i + "}";
1373         }
1374         logp(Level.FINER, sourceClass, sourceMethod, msg, params);
1375     }
1376 
1377     /**
1378      * Log a method return.
1379      * <p>
1380      * This is a convenience method that can be used to log returning
1381      * from a method.  A LogRecord with message "RETURN", log level
1382      * FINER, and the given sourceMethod and sourceClass is logged.
1383      * <p>
1384      * @param   sourceClass    name of class that issued the logging request
1385      * @param   sourceMethod   name of the method
1386      */
1387     public void exiting(String sourceClass, String sourceMethod) {
1388         logp(Level.FINER, sourceClass, sourceMethod, "RETURN");
1389     }
1390 
1391 
1392     /**
1393      * Log a method return, with result object.
1394      * <p>
1395      * This is a convenience method that can be used to log returning
1396      * from a method.  A LogRecord with message "RETURN {0}", log level
1397      * FINER, and the gives sourceMethod, sourceClass, and result
1398      * object is logged.
1399      * <p>
1400      * @param   sourceClass    name of class that issued the logging request
1401      * @param   sourceMethod   name of the method
1402      * @param   result  Object that is being returned
1403      */
1404     public void exiting(String sourceClass, String sourceMethod, Object result) {
1405         logp(Level.FINER, sourceClass, sourceMethod, "RETURN {0}", result);
1406     }
1407 
1408     /**
1409      * Log throwing an exception.
1410      * <p>
1411      * This is a convenience method to log that a method is
1412      * terminating by throwing an exception.  The logging is done
1413      * using the FINER level.
1414      * <p>
1415      * If the logger is currently enabled for the given message
1416      * level then the given arguments are stored in a LogRecord
1417      * which is forwarded to all registered output handlers.  The
1418      * LogRecord's message is set to "THROW".
1419      * <p>
1420      * Note that the thrown argument is stored in the LogRecord thrown
1421      * property, rather than the LogRecord parameters property.  Thus it is
1422      * processed specially by output Formatters and is not treated
1423      * as a formatting parameter to the LogRecord message property.
1424      * <p>
1425      * @param   sourceClass    name of class that issued the logging request
1426      * @param   sourceMethod  name of the method.
1427      * @param   thrown  The Throwable that is being thrown.
1428      */
1429     public void throwing(String sourceClass, String sourceMethod, Throwable thrown) {
1430         if (!isLoggable(Level.FINER)) {
1431             return;
1432         }
1433         LogRecord lr = new LogRecord(Level.FINER, "THROW");
1434         lr.setSourceClassName(sourceClass);
1435         lr.setSourceMethodName(sourceMethod);
1436         lr.setThrown(thrown);
1437         doLog(lr);
1438     }
1439 
1440     //=======================================================================
1441     // Start of simple convenience methods using level names as method names
1442     //=======================================================================
1443 
1444     /**
1445      * Log a SEVERE message.
1446      * <p>
1447      * If the logger is currently enabled for the SEVERE message
1448      * level then the given message is forwarded to all the
1449      * registered output Handler objects.
1450      * <p>
1451      * @param   msg     The string message (or a key in the message catalog)
1452      */
1453     public void severe(String msg) {
1454         log(Level.SEVERE, msg);
1455     }
1456 
1457     /**
1458      * Log a WARNING message.
1459      * <p>
1460      * If the logger is currently enabled for the WARNING message
1461      * level then the given message is forwarded to all the
1462      * registered output Handler objects.
1463      * <p>
1464      * @param   msg     The string message (or a key in the message catalog)
1465      */
1466     public void warning(String msg) {
1467         log(Level.WARNING, msg);
1468     }
1469 
1470     /**
1471      * Log an INFO message.
1472      * <p>
1473      * If the logger is currently enabled for the INFO message
1474      * level then the given message is forwarded to all the
1475      * registered output Handler objects.
1476      * <p>
1477      * @param   msg     The string message (or a key in the message catalog)
1478      */
1479     public void info(String msg) {
1480         log(Level.INFO, msg);
1481     }
1482 
1483     /**
1484      * Log a CONFIG message.
1485      * <p>
1486      * If the logger is currently enabled for the CONFIG message
1487      * level then the given message is forwarded to all the
1488      * registered output Handler objects.
1489      * <p>
1490      * @param   msg     The string message (or a key in the message catalog)
1491      */
1492     public void config(String msg) {
1493         log(Level.CONFIG, msg);
1494     }
1495 
1496     /**
1497      * Log a FINE message.
1498      * <p>
1499      * If the logger is currently enabled for the FINE message
1500      * level then the given message is forwarded to all the
1501      * registered output Handler objects.
1502      * <p>
1503      * @param   msg     The string message (or a key in the message catalog)
1504      */
1505     public void fine(String msg) {
1506         log(Level.FINE, msg);
1507     }
1508 
1509     /**
1510      * Log a FINER message.
1511      * <p>
1512      * If the logger is currently enabled for the FINER message
1513      * level then the given message is forwarded to all the
1514      * registered output Handler objects.
1515      * <p>
1516      * @param   msg     The string message (or a key in the message catalog)
1517      */
1518     public void finer(String msg) {
1519         log(Level.FINER, msg);
1520     }
1521 
1522     /**
1523      * Log a FINEST message.
1524      * <p>
1525      * If the logger is currently enabled for the FINEST message
1526      * level then the given message is forwarded to all the
1527      * registered output Handler objects.
1528      * <p>
1529      * @param   msg     The string message (or a key in the message catalog)
1530      */
1531     public void finest(String msg) {
1532         log(Level.FINEST, msg);
1533     }
1534 
1535     //=======================================================================
1536     // Start of simple convenience methods using level names as method names
1537     // and use Supplier<String>
1538     //=======================================================================
1539 
1540     /**
1541      * Log a SEVERE message, which is only to be constructed if the logging
1542      * level is such that the message will actually be logged.
1543      * <p>
1544      * If the logger is currently enabled for the SEVERE message
1545      * level then the message is constructed by invoking the provided
1546      * supplier function and forwarded to all the registered output
1547      * Handler objects.
1548      * <p>
1549      * @param   msgSupplier   A function, which when called, produces the
1550      *                        desired log message
1551      * @since   1.8
1552      */
1553     public void severe(Supplier<String> msgSupplier) {
1554         log(Level.SEVERE, msgSupplier);
1555     }
1556 
1557     /**
1558      * Log a WARNING message, which is only to be constructed if the logging
1559      * level is such that the message will actually be logged.
1560      * <p>
1561      * If the logger is currently enabled for the WARNING message
1562      * level then the message is constructed by invoking the provided
1563      * supplier function and forwarded to all the registered output
1564      * Handler objects.
1565      * <p>
1566      * @param   msgSupplier   A function, which when called, produces the
1567      *                        desired log message
1568      * @since   1.8
1569      */
1570     public void warning(Supplier<String> msgSupplier) {
1571         log(Level.WARNING, msgSupplier);
1572     }
1573 
1574     /**
1575      * Log a INFO message, which is only to be constructed if the logging
1576      * level is such that the message will actually be logged.
1577      * <p>
1578      * If the logger is currently enabled for the INFO message
1579      * level then the message is constructed by invoking the provided
1580      * supplier function and forwarded to all the registered output
1581      * Handler objects.
1582      * <p>
1583      * @param   msgSupplier   A function, which when called, produces the
1584      *                        desired log message
1585      * @since   1.8
1586      */
1587     public void info(Supplier<String> msgSupplier) {
1588         log(Level.INFO, msgSupplier);
1589     }
1590 
1591     /**
1592      * Log a CONFIG message, which is only to be constructed if the logging
1593      * level is such that the message will actually be logged.
1594      * <p>
1595      * If the logger is currently enabled for the CONFIG message
1596      * level then the message is constructed by invoking the provided
1597      * supplier function and forwarded to all the registered output
1598      * Handler objects.
1599      * <p>
1600      * @param   msgSupplier   A function, which when called, produces the
1601      *                        desired log message
1602      * @since   1.8
1603      */
1604     public void config(Supplier<String> msgSupplier) {
1605         log(Level.CONFIG, msgSupplier);
1606     }
1607 
1608     /**
1609      * Log a FINE message, which is only to be constructed if the logging
1610      * level is such that the message will actually be logged.
1611      * <p>
1612      * If the logger is currently enabled for the FINE message
1613      * level then the message is constructed by invoking the provided
1614      * supplier function and forwarded to all the registered output
1615      * Handler objects.
1616      * <p>
1617      * @param   msgSupplier   A function, which when called, produces the
1618      *                        desired log message
1619      * @since   1.8
1620      */
1621     public void fine(Supplier<String> msgSupplier) {
1622         log(Level.FINE, msgSupplier);
1623     }
1624 
1625     /**
1626      * Log a FINER message, which is only to be constructed if the logging
1627      * level is such that the message will actually be logged.
1628      * <p>
1629      * If the logger is currently enabled for the FINER message
1630      * level then the message is constructed by invoking the provided
1631      * supplier function and forwarded to all the registered output
1632      * Handler objects.
1633      * <p>
1634      * @param   msgSupplier   A function, which when called, produces the
1635      *                        desired log message
1636      * @since   1.8
1637      */
1638     public void finer(Supplier<String> msgSupplier) {
1639         log(Level.FINER, msgSupplier);
1640     }
1641 
1642     /**
1643      * Log a FINEST message, which is only to be constructed if the logging
1644      * level is such that the message will actually be logged.
1645      * <p>
1646      * If the logger is currently enabled for the FINEST message
1647      * level then the message is constructed by invoking the provided
1648      * supplier function and forwarded to all the registered output
1649      * Handler objects.
1650      * <p>
1651      * @param   msgSupplier   A function, which when called, produces the
1652      *                        desired log message
1653      * @since   1.8
1654      */
1655     public void finest(Supplier<String> msgSupplier) {
1656         log(Level.FINEST, msgSupplier);
1657     }
1658 
1659     //================================================================
1660     // End of convenience methods
1661     //================================================================
1662 
1663     /**
1664      * Set the log level specifying which message levels will be
1665      * logged by this logger.  Message levels lower than this
1666      * value will be discarded.  The level value Level.OFF
1667      * can be used to turn off logging.
1668      * <p>
1669      * If the new level is null, it means that this node should
1670      * inherit its level from its nearest ancestor with a specific
1671      * (non-null) level value.
1672      *
1673      * @param newLevel   the new value for the log level (may be null)
1674      * @throws  SecurityException if a security manager exists,
1675      *          this logger is not anonymous, and the caller
1676      *          does not have LoggingPermission("control").
1677      */
1678     public void setLevel(Level newLevel) throws SecurityException {
1679         checkPermission();
1680         synchronized (treeLock) {
1681             levelObject = newLevel;
1682             updateEffectiveLevel();
1683         }
1684     }
1685 
1686     final boolean isLevelInitialized() {
1687         return levelObject != null;
1688     }
1689 
1690     /**
1691      * Get the log Level that has been specified for this Logger.
1692      * The result may be null, which means that this logger's
1693      * effective level will be inherited from its parent.
1694      *
1695      * @return  this Logger's level
1696      */
1697     public Level getLevel() {
1698         return levelObject;
1699     }
1700 
1701     /**
1702      * Check if a message of the given level would actually be logged
1703      * by this logger.  This check is based on the Loggers effective level,
1704      * which may be inherited from its parent.
1705      *
1706      * @param   level   a message logging level
1707      * @return  true if the given message level is currently being logged.
1708      */
1709     public boolean isLoggable(Level level) {
1710         if (level.intValue() < levelValue || levelValue == offValue) {
1711             return false;
1712         }
1713         return true;
1714     }
1715 
1716     /**
1717      * Get the name for this logger.
1718      * @return logger name.  Will be null for anonymous Loggers.
1719      */
1720     public String getName() {
1721         return name;
1722     }
1723 
1724     /**
1725      * Add a log Handler to receive logging messages.
1726      * <p>
1727      * By default, Loggers also send their output to their parent logger.
1728      * Typically the root Logger is configured with a set of Handlers
1729      * that essentially act as default handlers for all loggers.
1730      *
1731      * @param   handler a logging Handler
1732      * @throws  SecurityException if a security manager exists,
1733      *          this logger is not anonymous, and the caller
1734      *          does not have LoggingPermission("control").
1735      */
1736     public void addHandler(Handler handler) throws SecurityException {
1737         // Check for null handler
1738         handler.getClass();
1739         checkPermission();
1740         handlers.add(handler);
1741     }
1742 
1743     /**
1744      * Remove a log Handler.
1745      * <P>
1746      * Returns silently if the given Handler is not found or is null
1747      *
1748      * @param   handler a logging Handler
1749      * @throws  SecurityException if a security manager exists,
1750      *          this logger is not anonymous, and the caller
1751      *          does not have LoggingPermission("control").
1752      */
1753     public void removeHandler(Handler handler) throws SecurityException {
1754         checkPermission();
1755         if (handler == null) {
1756             return;
1757         }
1758         handlers.remove(handler);
1759     }
1760 
1761     /**
1762      * Get the Handlers associated with this logger.
1763      * <p>
1764      * @return  an array of all registered Handlers
1765      */
1766     public Handler[] getHandlers() {
1767         return handlers.toArray(emptyHandlers);
1768     }
1769 
1770     /**
1771      * Specify whether or not this logger should send its output
1772      * to its parent Logger.  This means that any LogRecords will
1773      * also be written to the parent's Handlers, and potentially
1774      * to its parent, recursively up the namespace.
1775      *
1776      * @param useParentHandlers   true if output is to be sent to the
1777      *          logger's parent.
1778      * @throws  SecurityException if a security manager exists,
1779      *          this logger is not anonymous, and the caller
1780      *          does not have LoggingPermission("control").
1781      */
1782     public void setUseParentHandlers(boolean useParentHandlers) {
1783         checkPermission();
1784         this.useParentHandlers = useParentHandlers;
1785     }
1786 
1787     /**
1788      * Discover whether or not this logger is sending its output
1789      * to its parent logger.
1790      *
1791      * @return  true if output is to be sent to the logger's parent
1792      */
1793     public boolean getUseParentHandlers() {
1794         return useParentHandlers;
1795     }
1796 
1797     private static ResourceBundle findSystemResourceBundle(final Locale locale) {
1798         // the resource bundle is in a restricted package
1799         return AccessController.doPrivileged(new PrivilegedAction<ResourceBundle>() {
1800             @Override
1801             public ResourceBundle run() {
1802                 try {
1803                     return ResourceBundle.getBundle(SYSTEM_LOGGER_RB_NAME,
1804                                                     locale,
1805                                                     ClassLoader.getSystemClassLoader());
1806                 } catch (MissingResourceException e) {
1807                     throw new InternalError(e.toString());
1808                 }
1809             }
1810         });
1811     }
1812 
1813     /**
1814      * Private utility method to map a resource bundle name to an
1815      * actual resource bundle, using a simple one-entry cache.
1816      * Returns null for a null name.
1817      * May also return null if we can't find the resource bundle and
1818      * there is no suitable previous cached value.
1819      *
1820      * @param name the ResourceBundle to locate
1821      * @param userCallersClassLoader if true search using the caller's ClassLoader
1822      * @return ResourceBundle specified by name or null if not found
1823      */
1824     private synchronized ResourceBundle findResourceBundle(String name,
1825                                                            boolean useCallersClassLoader) {
1826         // For all lookups, we first check the thread context class loader
1827         // if it is set.  If not, we use the system classloader.  If we
1828         // still haven't found it we use the callersClassLoaderRef if it
1829         // is set and useCallersClassLoader is true.  We set
1830         // callersClassLoaderRef initially upon creating the logger with a
1831         // non-null resource bundle name.
1832 
1833         // Return a null bundle for a null name.
1834         if (name == null) {
1835             return null;
1836         }
1837 
1838         Locale currentLocale = Locale.getDefault();
1839         final LoggerBundle lb = loggerBundle;
1840 
1841         // Normally we should hit on our simple one entry cache.
1842         if (lb.userBundle != null &&
1843                 name.equals(lb.resourceBundleName)) {
1844             return lb.userBundle;
1845         } else if (catalog != null && currentLocale.equals(catalogLocale)
1846                 && name.equals(catalogName)) {
1847             return catalog;
1848         }
1849 
1850         if (name.equals(SYSTEM_LOGGER_RB_NAME)) {
1851             catalog = findSystemResourceBundle(currentLocale);
1852             catalogName = name;
1853             catalogLocale = currentLocale;
1854             return catalog;
1855         }
1856 
1857         // Use the thread's context ClassLoader.  If there isn't one, use the
1858         // {@linkplain java.lang.ClassLoader#getSystemClassLoader() system ClassLoader}.
1859         ClassLoader cl = Thread.currentThread().getContextClassLoader();
1860         if (cl == null) {
1861             cl = ClassLoader.getSystemClassLoader();
1862         }
1863         try {
1864             catalog = ResourceBundle.getBundle(name, currentLocale, cl);
1865             catalogName = name;
1866             catalogLocale = currentLocale;
1867             return catalog;
1868         } catch (MissingResourceException ex) {
1869             // We can't find the ResourceBundle in the default
1870             // ClassLoader.  Drop through.
1871         }
1872 
1873         if (useCallersClassLoader) {
1874             // Try with the caller's ClassLoader
1875             ClassLoader callersClassLoader = getCallersClassLoader();
1876 
1877             if (callersClassLoader == null || callersClassLoader == cl) {
1878                 return null;
1879             }
1880 
1881             try {
1882                 catalog = ResourceBundle.getBundle(name, currentLocale,
1883                                                    callersClassLoader);
1884                 catalogName = name;
1885                 catalogLocale = currentLocale;
1886                 return catalog;
1887             } catch (MissingResourceException ex) {
1888                 return null; // no luck
1889             }
1890         } else {
1891             return null;
1892         }
1893     }
1894 
1895     // Private utility method to initialize our one entry
1896     // resource bundle name cache and the callers ClassLoader
1897     // Note: for consistency reasons, we are careful to check
1898     // that a suitable ResourceBundle exists before setting the
1899     // resourceBundleName field.
1900     // Synchronized to prevent races in setting the fields.
1901     private synchronized void setupResourceInfo(String name,
1902                                                 Class<?> callersClass) {
1903         final LoggerBundle lb = loggerBundle;
1904         if (lb.resourceBundleName != null) {
1905             // this Logger already has a ResourceBundle
1906 
1907             if (lb.resourceBundleName.equals(name)) {
1908                 // the names match so there is nothing more to do
1909                 return;
1910             }
1911 
1912             // cannot change ResourceBundles once they are set
1913             throw new IllegalArgumentException(
1914                 lb.resourceBundleName + " != " + name);
1915         }
1916 
1917         if (name == null) {
1918             return;
1919         }
1920 
1921         setCallersClassLoaderRef(callersClass);
1922         if (findResourceBundle(name, true) == null) {
1923             // We've failed to find an expected ResourceBundle.
1924             // unset the caller's ClassLoader since we were unable to find the
1925             // the bundle using it
1926             this.callersClassLoaderRef = null;
1927             throw new MissingResourceException("Can't find " + name + " bundle",
1928                                                 name, "");
1929         }
1930         loggerBundle = LoggerBundle.get(name, lb.userBundle);
1931     }
1932 
1933     /**
1934      * Sets a resource bundle on this logger.
1935      * All messages will be logged using the given resource bundle for its
1936      * specific {@linkplain ResourceBundle#getLocale locale}.
1937      * @param bundle The resource bundle that this logger shall use.
1938      * @throws NullPointerException if the given bundle is {@code null}.
1939      * @throws IllegalArgumentException if the given bundle doesn't have a
1940      *         {@linkplain ResourceBundle#getBaseBundleName base name},
1941      *         or if this logger already has a resource bundle set but
1942      *         the given bundle has a different base name.
1943      * @throws SecurityException if a security manager exists,
1944      *         this logger is not anonymous, and the caller
1945      *         does not have LoggingPermission("control").
1946      * @since 1.8
1947      */
1948     public void setResourceBundle(ResourceBundle bundle) {
1949         checkPermission();
1950 
1951         // Will throw NPE if bundle is null.
1952         final String baseName = bundle.getBaseBundleName();
1953 
1954         // bundle must have a name
1955         if (baseName == null || baseName.isEmpty()) {
1956             throw new IllegalArgumentException("resource bundle must have a name");
1957         }
1958 
1959         synchronized (this) {
1960             LoggerBundle lb = loggerBundle;
1961             final boolean canReplaceResourceBundle = lb.resourceBundleName == null
1962                     || lb.resourceBundleName.equals(baseName);
1963 
1964             if (!canReplaceResourceBundle) {
1965                 throw new IllegalArgumentException("can't replace resource bundle");
1966             }
1967 
1968 
1969             loggerBundle = LoggerBundle.get(baseName, bundle);
1970         }
1971     }
1972 
1973     /**
1974      * Return the parent for this Logger.
1975      * <p>
1976      * This method returns the nearest extant parent in the namespace.
1977      * Thus if a Logger is called "a.b.c.d", and a Logger called "a.b"
1978      * has been created but no logger "a.b.c" exists, then a call of
1979      * getParent on the Logger "a.b.c.d" will return the Logger "a.b".
1980      * <p>
1981      * The result will be null if it is called on the root Logger
1982      * in the namespace.
1983      *
1984      * @return nearest existing parent Logger
1985      */
1986     public Logger getParent() {
1987         // Note: this used to be synchronized on treeLock.  However, this only
1988         // provided memory semantics, as there was no guarantee that the caller
1989         // would synchronize on treeLock (in fact, there is no way for external
1990         // callers to so synchronize).  Therefore, we have made parent volatile
1991         // instead.
1992         return parent;
1993     }
1994 
1995     /**
1996      * Set the parent for this Logger.  This method is used by
1997      * the LogManager to update a Logger when the namespace changes.
1998      * <p>
1999      * It should not be called from application code.
2000      * <p>
2001      * @param  parent   the new parent logger
2002      * @throws  SecurityException  if a security manager exists and if
2003      *          the caller does not have LoggingPermission("control").
2004      */
2005     public void setParent(Logger parent) {
2006         if (parent == null) {
2007             throw new NullPointerException();
2008         }
2009 
2010         // check permission for all loggers, including anonymous loggers
2011         if (manager == null) {
2012             manager = LogManager.getLogManager();
2013         }
2014         manager.checkPermission();
2015 
2016         doSetParent(parent);
2017     }
2018 
2019     // Private method to do the work for parenting a child
2020     // Logger onto a parent logger.
2021     private void doSetParent(Logger newParent) {
2022 
2023         // System.err.println("doSetParent \"" + getName() + "\" \""
2024         //                              + newParent.getName() + "\"");
2025 
2026         synchronized (treeLock) {
2027 
2028             // Remove ourself from any previous parent.
2029             LogManager.LoggerWeakRef ref = null;
2030             if (parent != null) {
2031                 // assert parent.kids != null;
2032                 for (Iterator<LogManager.LoggerWeakRef> iter = parent.kids.iterator(); iter.hasNext(); ) {
2033                     ref = iter.next();
2034                     Logger kid =  ref.get();
2035                     if (kid == this) {
2036                         // ref is used down below to complete the reparenting
2037                         iter.remove();
2038                         break;
2039                     } else {
2040                         ref = null;
2041                     }
2042                 }
2043                 // We have now removed ourself from our parents' kids.
2044             }
2045 
2046             // Set our new parent.
2047             parent = newParent;
2048             if (parent.kids == null) {
2049                 parent.kids = new ArrayList<>(2);
2050             }
2051             if (ref == null) {
2052                 // we didn't have a previous parent
2053                 ref = manager.new LoggerWeakRef(this);
2054             }
2055             ref.setParentRef(new WeakReference<>(parent));
2056             parent.kids.add(ref);
2057 
2058             // As a result of the reparenting, the effective level
2059             // may have changed for us and our children.
2060             updateEffectiveLevel();
2061 
2062         }
2063     }
2064 
2065     // Package-level method.
2066     // Remove the weak reference for the specified child Logger from the
2067     // kid list. We should only be called from LoggerWeakRef.dispose().
2068     final void removeChildLogger(LogManager.LoggerWeakRef child) {
2069         synchronized (treeLock) {
2070             for (Iterator<LogManager.LoggerWeakRef> iter = kids.iterator(); iter.hasNext(); ) {
2071                 LogManager.LoggerWeakRef ref = iter.next();
2072                 if (ref == child) {
2073                     iter.remove();
2074                     return;
2075                 }
2076             }
2077         }
2078     }
2079 
2080     // Recalculate the effective level for this node and
2081     // recursively for our children.
2082 
2083     private void updateEffectiveLevel() {
2084         // assert Thread.holdsLock(treeLock);
2085 
2086         // Figure out our current effective level.
2087         int newLevelValue;
2088         if (levelObject != null) {
2089             newLevelValue = levelObject.intValue();
2090         } else {
2091             if (parent != null) {
2092                 newLevelValue = parent.levelValue;
2093             } else {
2094                 // This may happen during initialization.
2095                 newLevelValue = Level.INFO.intValue();
2096             }
2097         }
2098 
2099         // If our effective value hasn't changed, we're done.
2100         if (levelValue == newLevelValue) {
2101             return;
2102         }
2103 
2104         levelValue = newLevelValue;
2105 
2106         // System.err.println("effective level: \"" + getName() + "\" := " + level);
2107 
2108         // Recursively update the level on each of our kids.
2109         if (kids != null) {
2110             for (int i = 0; i < kids.size(); i++) {
2111                 LogManager.LoggerWeakRef ref = kids.get(i);
2112                 Logger kid =  ref.get();
2113                 if (kid != null) {
2114                     kid.updateEffectiveLevel();
2115                 }
2116             }
2117         }
2118     }
2119 
2120 
2121     // Private method to get the potentially inherited
2122     // resource bundle and resource bundle name for this Logger.
2123     // This method never returns null.
2124     private LoggerBundle getEffectiveLoggerBundle() {
2125         final LoggerBundle lb = loggerBundle;
2126         if (lb.isSystemBundle()) {
2127             return SYSTEM_BUNDLE;
2128         }
2129 
2130         // first take care of this logger
2131         final ResourceBundle b = getResourceBundle();
2132         if (b != null && b == lb.userBundle) {
2133             return lb;
2134         } else if (b != null) {
2135             // either lb.userBundle is null or getResourceBundle() is
2136             // overriden
2137             final String rbName = getResourceBundleName();
2138             if (!SYSTEM_LOGGER_RB_NAME.equals(rbName)
2139                     && !SYSTEM_LOGGER_RB_NAME.equals(b.getBaseBundleName())) {
2140                 return LoggerBundle.get(rbName, b);
2141             } else {
2142                 return SYSTEM_BUNDLE;
2143             }
2144         }
2145 
2146         // no resource bundle was specified on this logger, look up the
2147         // parent stack.
2148         Logger target = this.parent;
2149         while (target != null) {
2150             final LoggerBundle trb = target.loggerBundle;
2151             if (trb.isSystemBundle()) {
2152                 return SYSTEM_BUNDLE;
2153             }
2154             if (trb.userBundle != null) {
2155                 return trb;
2156             }
2157             final String rbName = target.getResourceBundleName();
2158             if (rbName != null) {
2159                 if (!SYSTEM_LOGGER_RB_NAME.equals(rbName)) {
2160                     return LoggerBundle.get(rbName,
2161                             findResourceBundle(rbName, true));
2162                 } else {
2163                     return SYSTEM_BUNDLE;
2164                 }
2165             }
2166             target = target.getParent();
2167         }
2168         return NO_RESOURCE_BUNDLE;
2169     }
2170 
2171 }