rt/emul/compact/src/main/java/java/util/logging/Logger.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 09 Oct 2013 17:17:30 +0200
changeset 1356 637f7fb50abd
parent 1355 ae20214a816c
child 1358 65dd5a650eab
permissions -rw-r--r--
Merging the conditional logging checks
     1 /*
     2  * Copyright (c) 2000, 2011, 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.util.HashMap;
    30 import java.util.Map;
    31 import org.apidesign.bck2brwsr.core.JavaScriptBody;
    32 
    33 /**
    34  * A Logger object is used to log messages for a specific
    35  * system or application component.  Loggers are normally named,
    36  * using a hierarchical dot-separated namespace.  Logger names
    37  * can be arbitrary strings, but they should normally be based on
    38  * the package name or class name of the logged component, such
    39  * as java.net or javax.swing.  In addition it is possible to create
    40  * "anonymous" Loggers that are not stored in the Logger namespace.
    41  * <p>
    42  * Logger objects may be obtained by calls on one of the getLogger
    43  * factory methods.  These will either create a new Logger or
    44  * return a suitable existing Logger. It is important to note that
    45  * the Logger returned by one of the {@code getLogger} factory methods
    46  * may be garbage collected at any time if a strong reference to the
    47  * Logger is not kept.
    48  * <p>
    49  * Logging messages will be forwarded to registered Handler
    50  * objects, which can forward the messages to a variety of
    51  * destinations, including consoles, files, OS logs, etc.
    52  * <p>
    53  * Each Logger keeps track of a "parent" Logger, which is its
    54  * nearest existing ancestor in the Logger namespace.
    55  * <p>
    56  * Each Logger has a "Level" associated with it.  This reflects
    57  * a minimum Level that this logger cares about.  If a Logger's
    58  * level is set to <tt>null</tt>, then its effective level is inherited
    59  * from its parent, which may in turn obtain it recursively from its
    60  * parent, and so on up the tree.
    61  * <p>
    62  * The log level can be configured based on the properties from the
    63  * logging configuration file, as described in the description
    64  * of the LogManager class.  However it may also be dynamically changed
    65  * by calls on the Logger.setLevel method.  If a logger's level is
    66  * changed the change may also affect child loggers, since any child
    67  * logger that has <tt>null</tt> as its level will inherit its
    68  * effective level from its parent.
    69  * <p>
    70  * On each logging call the Logger initially performs a cheap
    71  * check of the request level (e.g., SEVERE or FINE) against the
    72  * effective log level of the logger.  If the request level is
    73  * lower than the log level, the logging call returns immediately.
    74  * <p>
    75  * After passing this initial (cheap) test, the Logger will allocate
    76  * a LogRecord to describe the logging message.  It will then call a
    77  * Filter (if present) to do a more detailed check on whether the
    78  * record should be published.  If that passes it will then publish
    79  * the LogRecord to its output Handlers.  By default, loggers also
    80  * publish to their parent's Handlers, recursively up the tree.
    81  * <p>
    82  * Each Logger may have a ResourceBundle name associated with it.
    83  * The named bundle will be used for localizing logging messages.
    84  * If a Logger does not have its own ResourceBundle name, then
    85  * it will inherit the ResourceBundle name from its parent,
    86  * recursively up the tree.
    87  * <p>
    88  * Most of the logger output methods take a "msg" argument.  This
    89  * msg argument may be either a raw value or a localization key.
    90  * During formatting, if the logger has (or inherits) a localization
    91  * ResourceBundle and if the ResourceBundle has a mapping for the msg
    92  * string, then the msg string is replaced by the localized value.
    93  * Otherwise the original msg string is used.  Typically, formatters use
    94  * java.text.MessageFormat style formatting to format parameters, so
    95  * for example a format string "{0} {1}" would format two parameters
    96  * as strings.
    97  * <p>
    98  * When mapping ResourceBundle names to ResourceBundles, the Logger
    99  * will first try to use the Thread's ContextClassLoader.  If that
   100  * is null it will try the SystemClassLoader instead.  As a temporary
   101  * transition feature in the initial implementation, if the Logger is
   102  * unable to locate a ResourceBundle from the ContextClassLoader or
   103  * SystemClassLoader the Logger will also search up the class stack
   104  * and use successive calling ClassLoaders to try to locate a ResourceBundle.
   105  * (This call stack search is to allow containers to transition to
   106  * using ContextClassLoaders and is likely to be removed in future
   107  * versions.)
   108  * <p>
   109  * Formatting (including localization) is the responsibility of
   110  * the output Handler, which will typically call a Formatter.
   111  * <p>
   112  * Note that formatting need not occur synchronously.  It may be delayed
   113  * until a LogRecord is actually written to an external sink.
   114  * <p>
   115  * The logging methods are grouped in five main categories:
   116  * <ul>
   117  * <li><p>
   118  *     There are a set of "log" methods that take a log level, a message
   119  *     string, and optionally some parameters to the message string.
   120  * <li><p>
   121  *     There are a set of "logp" methods (for "log precise") that are
   122  *     like the "log" methods, but also take an explicit source class name
   123  *     and method name.
   124  * <li><p>
   125  *     There are a set of "logrb" method (for "log with resource bundle")
   126  *     that are like the "logp" method, but also take an explicit resource
   127  *     bundle name for use in localizing the log message.
   128  * <li><p>
   129  *     There are convenience methods for tracing method entries (the
   130  *     "entering" methods), method returns (the "exiting" methods) and
   131  *     throwing exceptions (the "throwing" methods).
   132  * <li><p>
   133  *     Finally, there are a set of convenience methods for use in the
   134  *     very simplest cases, when a developer simply wants to log a
   135  *     simple string at a given log level.  These methods are named
   136  *     after the standard Level names ("severe", "warning", "info", etc.)
   137  *     and take a single argument, a message string.
   138  * </ul>
   139  * <p>
   140  * For the methods that do not take an explicit source name and
   141  * method name, the Logging framework will make a "best effort"
   142  * to determine which class and method called into the logging method.
   143  * However, it is important to realize that this automatically inferred
   144  * information may only be approximate (or may even be quite wrong!).
   145  * Virtual machines are allowed to do extensive optimizations when
   146  * JITing and may entirely remove stack frames, making it impossible
   147  * to reliably locate the calling class and method.
   148  * <P>
   149  * All methods on Logger are multi-thread safe.
   150  * <p>
   151  * <b>Subclassing Information:</b> Note that a LogManager class may
   152  * provide its own implementation of named Loggers for any point in
   153  * the namespace.  Therefore, any subclasses of Logger (unless they
   154  * are implemented in conjunction with a new LogManager class) should
   155  * take care to obtain a Logger instance from the LogManager class and
   156  * should delegate operations such as "isLoggable" and "log(LogRecord)"
   157  * to that instance.  Note that in order to intercept all logging
   158  * output, subclasses need only override the log(LogRecord) method.
   159  * All the other logging methods are implemented as calls on this
   160  * log(LogRecord) method.
   161  *
   162  * @since 1.4
   163  */
   164 
   165 
   166 public class Logger {
   167     private static int offValue = Level.OFF.intValue();
   168     private static final Map<String,Logger> ALL = new HashMap<>();
   169     private String name;
   170 
   171     private volatile int levelValue;  // current effective level value
   172     private Level levelObject;
   173     
   174     /**
   175      * GLOBAL_LOGGER_NAME is a name for the global logger.
   176      *
   177      * @since 1.6
   178      */
   179     public static final String GLOBAL_LOGGER_NAME = "global";
   180 
   181     /**
   182      * Return global logger object with the name Logger.GLOBAL_LOGGER_NAME.
   183      *
   184      * @return global logger object
   185      * @since 1.7
   186      */
   187     public static final Logger getGlobal() {
   188         return global;
   189     }
   190 
   191     /**
   192      * The "global" Logger object is provided as a convenience to developers
   193      * who are making casual use of the Logging package.  Developers
   194      * who are making serious use of the logging package (for example
   195      * in products) should create and use their own Logger objects,
   196      * with appropriate names, so that logging can be controlled on a
   197      * suitable per-Logger granularity. Developers also need to keep a
   198      * strong reference to their Logger objects to prevent them from
   199      * being garbage collected.
   200      * <p>
   201      * @deprecated Initialization of this field is prone to deadlocks.
   202      * The field must be initialized by the Logger class initialization
   203      * which may cause deadlocks with the LogManager class initialization.
   204      * In such cases two class initialization wait for each other to complete.
   205      * The preferred way to get the global logger object is via the call
   206      * <code>Logger.getGlobal()</code>.
   207      * For compatibility with old JDK versions where the
   208      * <code>Logger.getGlobal()</code> is not available use the call
   209      * <code>Logger.getLogger(Logger.GLOBAL_LOGGER_NAME)</code>
   210      * or <code>Logger.getLogger("global")</code>.
   211      */
   212     @Deprecated
   213     public static final Logger global = new Logger(GLOBAL_LOGGER_NAME);
   214 
   215     /**
   216      * Protected method to construct a logger for a named subsystem.
   217      * <p>
   218      * The logger will be initially configured with a null Level
   219      * and with useParentHandlers set to true.
   220      *
   221      * @param   name    A name for the logger.  This should
   222      *                          be a dot-separated name and should normally
   223      *                          be based on the package name or class name
   224      *                          of the subsystem, such as java.net
   225      *                          or javax.swing.  It may be null for anonymous Loggers.
   226      * @param   resourceBundleName  name of ResourceBundle to be used for localizing
   227      *                          messages for this logger.  May be null if none
   228      *                          of the messages require localization.
   229      * @throws MissingResourceException if the resourceBundleName is non-null and
   230      *             no corresponding resource can be found.
   231      */
   232     protected Logger(String name, String resourceBundleName) {
   233         this.name = name;
   234         levelValue = Level.INFO.intValue();
   235     }
   236 
   237     // This constructor is used only to create the global Logger.
   238     // It is needed to break a cyclic dependence between the LogManager
   239     // and Logger static initializers causing deadlocks.
   240     private Logger(String name) {
   241         // The manager field is not initialized here.
   242         this.name = name;
   243         levelValue = Level.INFO.intValue();
   244     }
   245 
   246     private void checkAccess() throws SecurityException {
   247         throw new SecurityException();
   248     }
   249 
   250     /**
   251      * Find or create a logger for a named subsystem.  If a logger has
   252      * already been created with the given name it is returned.  Otherwise
   253      * a new logger is created.
   254      * <p>
   255      * If a new logger is created its log level will be configured
   256      * based on the LogManager configuration and it will configured
   257      * to also send logging output to its parent's Handlers.  It will
   258      * be registered in the LogManager global namespace.
   259      * <p>
   260      * Note: The LogManager may only retain a weak reference to the newly
   261      * created Logger. It is important to understand that a previously
   262      * created Logger with the given name may be garbage collected at any
   263      * time if there is no strong reference to the Logger. In particular,
   264      * this means that two back-to-back calls like
   265      * {@code getLogger("MyLogger").log(...)} may use different Logger
   266      * objects named "MyLogger" if there is no strong reference to the
   267      * Logger named "MyLogger" elsewhere in the program.
   268      *
   269      * @param   name            A name for the logger.  This should
   270      *                          be a dot-separated name and should normally
   271      *                          be based on the package name or class name
   272      *                          of the subsystem, such as java.net
   273      *                          or javax.swing
   274      * @return a suitable Logger
   275      * @throws NullPointerException if the name is null.
   276      */
   277 
   278     // Synchronization is not required here. All synchronization for
   279     // adding a new Logger object is handled by LogManager.addLogger().
   280     public static Logger getLogger(String name) {
   281         return getLogger(name, null);
   282     }
   283 
   284     /**
   285      * Find or create a logger for a named subsystem.  If a logger has
   286      * already been created with the given name it is returned.  Otherwise
   287      * a new logger is created.
   288      * <p>
   289      * If a new logger is created its log level will be configured
   290      * based on the LogManager and it will configured to also send logging
   291      * output to its parent's Handlers.  It will be registered in
   292      * the LogManager global namespace.
   293      * <p>
   294      * Note: The LogManager may only retain a weak reference to the newly
   295      * created Logger. It is important to understand that a previously
   296      * created Logger with the given name may be garbage collected at any
   297      * time if there is no strong reference to the Logger. In particular,
   298      * this means that two back-to-back calls like
   299      * {@code getLogger("MyLogger", ...).log(...)} may use different Logger
   300      * objects named "MyLogger" if there is no strong reference to the
   301      * Logger named "MyLogger" elsewhere in the program.
   302      * <p>
   303      * If the named Logger already exists and does not yet have a
   304      * localization resource bundle then the given resource bundle
   305      * name is used.  If the named Logger already exists and has
   306      * a different resource bundle name then an IllegalArgumentException
   307      * is thrown.
   308      * <p>
   309      * @param   name    A name for the logger.  This should
   310      *                          be a dot-separated name and should normally
   311      *                          be based on the package name or class name
   312      *                          of the subsystem, such as java.net
   313      *                          or javax.swing
   314      * @param   resourceBundleName  name of ResourceBundle to be used for localizing
   315      *                          messages for this logger. May be <CODE>null</CODE> if none of
   316      *                          the messages require localization.
   317      * @return a suitable Logger
   318      * @throws MissingResourceException if the resourceBundleName is non-null and
   319      *             no corresponding resource can be found.
   320      * @throws IllegalArgumentException if the Logger already exists and uses
   321      *             a different resource bundle name.
   322      * @throws NullPointerException if the name is null.
   323      */
   324 
   325     // Synchronization is not required here. All synchronization for
   326     // adding a new Logger object is handled by LogManager.addLogger().
   327     public static Logger getLogger(String name, String resourceBundleName) {
   328         Logger l = ALL.get(name);
   329         if (l == null) {
   330             l = new Logger(name, resourceBundleName);
   331             ALL.put(name, l);
   332         }
   333         return l;
   334     }
   335 
   336 
   337     /**
   338      * Create an anonymous Logger.  The newly created Logger is not
   339      * registered in the LogManager namespace.  There will be no
   340      * access checks on updates to the logger.
   341      * <p>
   342      * This factory method is primarily intended for use from applets.
   343      * Because the resulting Logger is anonymous it can be kept private
   344      * by the creating class.  This removes the need for normal security
   345      * checks, which in turn allows untrusted applet code to update
   346      * the control state of the Logger.  For example an applet can do
   347      * a setLevel or an addHandler on an anonymous Logger.
   348      * <p>
   349      * Even although the new logger is anonymous, it is configured
   350      * to have the root logger ("") as its parent.  This means that
   351      * by default it inherits its effective level and handlers
   352      * from the root logger.
   353      * <p>
   354      *
   355      * @return a newly created private Logger
   356      */
   357     public static Logger getAnonymousLogger() {
   358         return getAnonymousLogger(null);
   359     }
   360 
   361     /**
   362      * Create an anonymous Logger.  The newly created Logger is not
   363      * registered in the LogManager namespace.  There will be no
   364      * access checks on updates to the logger.
   365      * <p>
   366      * This factory method is primarily intended for use from applets.
   367      * Because the resulting Logger is anonymous it can be kept private
   368      * by the creating class.  This removes the need for normal security
   369      * checks, which in turn allows untrusted applet code to update
   370      * the control state of the Logger.  For example an applet can do
   371      * a setLevel or an addHandler on an anonymous Logger.
   372      * <p>
   373      * Even although the new logger is anonymous, it is configured
   374      * to have the root logger ("") as its parent.  This means that
   375      * by default it inherits its effective level and handlers
   376      * from the root logger.
   377      * <p>
   378      * @param   resourceBundleName  name of ResourceBundle to be used for localizing
   379      *                          messages for this logger.
   380      *          May be null if none of the messages require localization.
   381      * @return a newly created private Logger
   382      * @throws MissingResourceException if the resourceBundleName is non-null and
   383      *             no corresponding resource can be found.
   384      */
   385 
   386     // Synchronization is not required here. All synchronization for
   387     // adding a new anonymous Logger object is handled by doSetParent().
   388     public static Logger getAnonymousLogger(String resourceBundleName) {
   389         return new Logger(null, resourceBundleName);
   390     }
   391 
   392     /**
   393      * Retrieve the localization resource bundle for this
   394      * logger for the current default locale.  Note that if
   395      * the result is null, then the Logger will use a resource
   396      * bundle inherited from its parent.
   397      *
   398      * @return localization bundle (may be null)
   399      */
   400 //    public ResourceBundle getResourceBundle() {
   401 //        return findResourceBundle(getResourceBundleName());
   402 //    }
   403 
   404     /**
   405      * Retrieve the localization resource bundle name for this
   406      * logger.  Note that if the result is null, then the Logger
   407      * will use a resource bundle name inherited from its parent.
   408      *
   409      * @return localization bundle name (may be null)
   410      */
   411     public String getResourceBundleName() {
   412         return null;
   413     }
   414 
   415     /**
   416      * Set a filter to control output on this Logger.
   417      * <P>
   418      * After passing the initial "level" check, the Logger will
   419      * call this Filter to check if a log record should really
   420      * be published.
   421      *
   422      * @param   newFilter  a filter object (may be null)
   423      * @exception  SecurityException  if a security manager exists and if
   424      *             the caller does not have LoggingPermission("control").
   425      */
   426 //    public void setFilter(Filter newFilter) throws SecurityException {
   427 //        checkAccess();
   428 //    }
   429 
   430     /**
   431      * Get the current filter for this Logger.
   432      *
   433      * @return  a filter object (may be null)
   434      */
   435 //    public Filter getFilter() {
   436 //        return filter;
   437 //    }
   438 
   439     /**
   440      * Log a LogRecord.
   441      * <p>
   442      * All the other logging methods in this class call through
   443      * this method to actually perform any logging.  Subclasses can
   444      * override this single method to capture all log activity.
   445      *
   446      * @param record the LogRecord to be published
   447      */
   448     public void log(LogRecord record) {
   449         if (record.getLevel().intValue() < levelValue) {
   450             return;
   451         }
   452         
   453         String method;
   454         switch (record.getLevel().toString()) {
   455             case "INFO": method = "info";  break;
   456             case "SEVERE": method = "error"; break;
   457             case "WARNING": method = "warn"; break;
   458             default: method = "log"; break;
   459         }
   460         
   461         String msg = record.getMessage();
   462         final Object[] params = record.getParameters();
   463         if (params != null && params.length != 0) {
   464             for (int i = 0; i < params.length; i++) {
   465                 msg = msg.replace("{" + i + "}", params[i] == null ? "null" : params[i].toString());
   466             }
   467         }
   468 
   469         consoleLog(
   470             method, 
   471             record.getLoggerName(), msg);
   472     }
   473     
   474     @JavaScriptBody(args = { "method", "logger", "msg" }, body = 
   475         "if (console) console[method]('[' + logger + ']: ' + msg);"
   476     )
   477     private static native void consoleLog(
   478         String method, String logger, String msg
   479     );
   480 
   481     // private support method for logging.
   482     // We fill in the logger name, resource bundle name, and
   483     // resource bundle and then call "void log(LogRecord)".
   484     private void doLog(LogRecord lr) {
   485         doLog(lr, lr.getResourceBundleName());
   486     }
   487     private void doLog(LogRecord lr, String bundleName) {
   488         lr.setLoggerName(name);
   489         log(lr);
   490     }
   491 
   492 
   493     //================================================================
   494     // Start of convenience methods WITHOUT className and methodName
   495     //================================================================
   496 
   497     /**
   498      * Log a message, with no arguments.
   499      * <p>
   500      * If the logger is currently enabled for the given message
   501      * level then the given message is forwarded to all the
   502      * registered output Handler objects.
   503      * <p>
   504      * @param   level   One of the message level identifiers, e.g., SEVERE
   505      * @param   msg     The string message (or a key in the message catalog)
   506      */
   507     public void log(Level level, String msg) {
   508         if (level.intValue() < levelValue || levelValue == offValue) {
   509             return;
   510         }
   511         LogRecord lr = new LogRecord(level, msg);
   512         doLog(lr);
   513     }
   514 
   515     /**
   516      * Log a message, with one object parameter.
   517      * <p>
   518      * If the logger is currently enabled for the given message
   519      * level then a corresponding LogRecord is created and forwarded
   520      * to all the registered output Handler objects.
   521      * <p>
   522      * @param   level   One of the message level identifiers, e.g., SEVERE
   523      * @param   msg     The string message (or a key in the message catalog)
   524      * @param   param1  parameter to the message
   525      */
   526     public void log(Level level, String msg, Object param1) {
   527         if (level.intValue() < levelValue || levelValue == offValue) {
   528             return;
   529         }
   530         LogRecord lr = new LogRecord(level, msg);
   531         Object params[] = { param1 };
   532         lr.setParameters(params);
   533         doLog(lr);
   534     }
   535 
   536     /**
   537      * Log a message, with an array of object arguments.
   538      * <p>
   539      * If the logger is currently enabled for the given message
   540      * level then a corresponding LogRecord is created and forwarded
   541      * to all the registered output Handler objects.
   542      * <p>
   543      * @param   level   One of the message level identifiers, e.g., SEVERE
   544      * @param   msg     The string message (or a key in the message catalog)
   545      * @param   params  array of parameters to the message
   546      */
   547     public void log(Level level, String msg, Object params[]) {
   548         if (level.intValue() < levelValue || levelValue == offValue) {
   549             return;
   550         }
   551         LogRecord lr = new LogRecord(level, msg);
   552         lr.setParameters(params);
   553         doLog(lr);
   554     }
   555 
   556     /**
   557      * Log a message, with associated Throwable information.
   558      * <p>
   559      * If the logger is currently enabled for the given message
   560      * level then the given arguments are stored in a LogRecord
   561      * which is forwarded to all registered output handlers.
   562      * <p>
   563      * Note that the thrown argument is stored in the LogRecord thrown
   564      * property, rather than the LogRecord parameters property.  Thus is it
   565      * processed specially by output Formatters and is not treated
   566      * as a formatting parameter to the LogRecord message property.
   567      * <p>
   568      * @param   level   One of the message level identifiers, e.g., SEVERE
   569      * @param   msg     The string message (or a key in the message catalog)
   570      * @param   thrown  Throwable associated with log message.
   571      */
   572     public void log(Level level, String msg, Throwable thrown) {
   573         if (level.intValue() < levelValue || levelValue == offValue) {
   574             return;
   575         }
   576         LogRecord lr = new LogRecord(level, msg);
   577         lr.setThrown(thrown);
   578         doLog(lr);
   579     }
   580 
   581     //================================================================
   582     // Start of convenience methods WITH className and methodName
   583     //================================================================
   584 
   585     /**
   586      * Log a message, specifying source class and method,
   587      * with no arguments.
   588      * <p>
   589      * If the logger is currently enabled for the given message
   590      * level then the given message is forwarded to all the
   591      * registered output Handler objects.
   592      * <p>
   593      * @param   level   One of the message level identifiers, e.g., SEVERE
   594      * @param   sourceClass    name of class that issued the logging request
   595      * @param   sourceMethod   name of method that issued the logging request
   596      * @param   msg     The string message (or a key in the message catalog)
   597      */
   598     public void logp(Level level, String sourceClass, String sourceMethod, String msg) {
   599         if (level.intValue() < levelValue || levelValue == offValue) {
   600             return;
   601         }
   602         LogRecord lr = new LogRecord(level, msg);
   603         lr.setSourceClassName(sourceClass);
   604         lr.setSourceMethodName(sourceMethod);
   605         doLog(lr);
   606     }
   607 
   608     /**
   609      * Log a message, specifying source class and method,
   610      * with a single object parameter to the log message.
   611      * <p>
   612      * If the logger is currently enabled for the given message
   613      * level then a corresponding LogRecord is created and forwarded
   614      * to all the registered output Handler objects.
   615      * <p>
   616      * @param   level   One of the message level identifiers, e.g., SEVERE
   617      * @param   sourceClass    name of class that issued the logging request
   618      * @param   sourceMethod   name of method that issued the logging request
   619      * @param   msg      The string message (or a key in the message catalog)
   620      * @param   param1    Parameter to the log message.
   621      */
   622     public void logp(Level level, String sourceClass, String sourceMethod,
   623                                                 String msg, Object param1) {
   624         if (level.intValue() < levelValue || levelValue == offValue) {
   625             return;
   626         }
   627         LogRecord lr = new LogRecord(level, msg);
   628         lr.setSourceClassName(sourceClass);
   629         lr.setSourceMethodName(sourceMethod);
   630         Object params[] = { param1 };
   631         lr.setParameters(params);
   632         doLog(lr);
   633     }
   634 
   635     /**
   636      * Log a message, specifying source class and method,
   637      * with an array of object arguments.
   638      * <p>
   639      * If the logger is currently enabled for the given message
   640      * level then a corresponding LogRecord is created and forwarded
   641      * to all the registered output Handler objects.
   642      * <p>
   643      * @param   level   One of the message level identifiers, e.g., SEVERE
   644      * @param   sourceClass    name of class that issued the logging request
   645      * @param   sourceMethod   name of method that issued the logging request
   646      * @param   msg     The string message (or a key in the message catalog)
   647      * @param   params  Array of parameters to the message
   648      */
   649     public void logp(Level level, String sourceClass, String sourceMethod,
   650                                                 String msg, Object params[]) {
   651         if (level.intValue() < levelValue || levelValue == offValue) {
   652             return;
   653         }
   654         LogRecord lr = new LogRecord(level, msg);
   655         lr.setSourceClassName(sourceClass);
   656         lr.setSourceMethodName(sourceMethod);
   657         lr.setParameters(params);
   658         doLog(lr);
   659     }
   660 
   661     /**
   662      * Log a message, specifying source class and method,
   663      * with associated Throwable information.
   664      * <p>
   665      * If the logger is currently enabled for the given message
   666      * level then the given arguments are stored in a LogRecord
   667      * which is forwarded to all registered output handlers.
   668      * <p>
   669      * Note that the thrown argument is stored in the LogRecord thrown
   670      * property, rather than the LogRecord parameters property.  Thus is it
   671      * processed specially by output Formatters and is not treated
   672      * as a formatting parameter to the LogRecord message property.
   673      * <p>
   674      * @param   level   One of the message level identifiers, e.g., SEVERE
   675      * @param   sourceClass    name of class that issued the logging request
   676      * @param   sourceMethod   name of method that issued the logging request
   677      * @param   msg     The string message (or a key in the message catalog)
   678      * @param   thrown  Throwable associated with log message.
   679      */
   680     public void logp(Level level, String sourceClass, String sourceMethod,
   681                                                         String msg, Throwable thrown) {
   682         if (level.intValue() < levelValue || levelValue == offValue) {
   683             return;
   684         }
   685         LogRecord lr = new LogRecord(level, msg);
   686         lr.setSourceClassName(sourceClass);
   687         lr.setSourceMethodName(sourceMethod);
   688         lr.setThrown(thrown);
   689         doLog(lr);
   690     }
   691 
   692 
   693     //=========================================================================
   694     // Start of convenience methods WITH className, methodName and bundle name.
   695     //=========================================================================
   696 
   697 
   698     /**
   699      * Log a message, specifying source class, method, and resource bundle name
   700      * with no arguments.
   701      * <p>
   702      * If the logger is currently enabled for the given message
   703      * level then the given message is forwarded to all the
   704      * registered output Handler objects.
   705      * <p>
   706      * The msg string is localized using the named resource bundle.  If the
   707      * resource bundle name is null, or an empty String or invalid
   708      * then the msg string is not localized.
   709      * <p>
   710      * @param   level   One of the message level identifiers, e.g., SEVERE
   711      * @param   sourceClass    name of class that issued the logging request
   712      * @param   sourceMethod   name of method that issued the logging request
   713      * @param   bundleName     name of resource bundle to localize msg,
   714      *                         can be null
   715      * @param   msg     The string message (or a key in the message catalog)
   716      */
   717 
   718     public void logrb(Level level, String sourceClass, String sourceMethod,
   719                                 String bundleName, String msg) {
   720         if (level.intValue() < levelValue || levelValue == offValue) {
   721             return;
   722         }
   723         LogRecord lr = new LogRecord(level, msg);
   724         lr.setSourceClassName(sourceClass);
   725         lr.setSourceMethodName(sourceMethod);
   726         doLog(lr, bundleName);
   727     }
   728 
   729     /**
   730      * Log a message, specifying source class, method, and resource bundle name,
   731      * with a single object parameter to the log message.
   732      * <p>
   733      * If the logger is currently enabled for the given message
   734      * level then a corresponding LogRecord is created and forwarded
   735      * to all the registered output Handler objects.
   736      * <p>
   737      * The msg string is localized using the named resource bundle.  If the
   738      * resource bundle name is null, or an empty String or invalid
   739      * then the msg string is not localized.
   740      * <p>
   741      * @param   level   One of the message level identifiers, e.g., SEVERE
   742      * @param   sourceClass    name of class that issued the logging request
   743      * @param   sourceMethod   name of method that issued the logging request
   744      * @param   bundleName     name of resource bundle to localize msg,
   745      *                         can be null
   746      * @param   msg      The string message (or a key in the message catalog)
   747      * @param   param1    Parameter to the log message.
   748      */
   749     public void logrb(Level level, String sourceClass, String sourceMethod,
   750                                 String bundleName, String msg, Object param1) {
   751         if (level.intValue() < levelValue || levelValue == offValue) {
   752             return;
   753         }
   754         LogRecord lr = new LogRecord(level, msg);
   755         lr.setSourceClassName(sourceClass);
   756         lr.setSourceMethodName(sourceMethod);
   757         Object params[] = { param1 };
   758         lr.setParameters(params);
   759         doLog(lr, bundleName);
   760     }
   761 
   762     /**
   763      * Log a message, specifying source class, method, and resource bundle name,
   764      * with an array of object arguments.
   765      * <p>
   766      * If the logger is currently enabled for the given message
   767      * level then a corresponding LogRecord is created and forwarded
   768      * to all the registered output Handler objects.
   769      * <p>
   770      * The msg string is localized using the named resource bundle.  If the
   771      * resource bundle name is null, or an empty String or invalid
   772      * then the msg string is not localized.
   773      * <p>
   774      * @param   level   One of the message level identifiers, e.g., SEVERE
   775      * @param   sourceClass    name of class that issued the logging request
   776      * @param   sourceMethod   name of method that issued the logging request
   777      * @param   bundleName     name of resource bundle to localize msg,
   778      *                         can be null.
   779      * @param   msg     The string message (or a key in the message catalog)
   780      * @param   params  Array of parameters to the message
   781      */
   782     public void logrb(Level level, String sourceClass, String sourceMethod,
   783                                 String bundleName, String msg, Object params[]) {
   784         if (level.intValue() < levelValue || levelValue == offValue) {
   785             return;
   786         }
   787         LogRecord lr = new LogRecord(level, msg);
   788         lr.setSourceClassName(sourceClass);
   789         lr.setSourceMethodName(sourceMethod);
   790         lr.setParameters(params);
   791         doLog(lr, bundleName);
   792     }
   793 
   794     /**
   795      * Log a message, specifying source class, method, and resource bundle name,
   796      * with associated Throwable information.
   797      * <p>
   798      * If the logger is currently enabled for the given message
   799      * level then the given arguments are stored in a LogRecord
   800      * which is forwarded to all registered output handlers.
   801      * <p>
   802      * The msg string is localized using the named resource bundle.  If the
   803      * resource bundle name is null, or an empty String or invalid
   804      * then the msg string is not localized.
   805      * <p>
   806      * Note that the thrown argument is stored in the LogRecord thrown
   807      * property, rather than the LogRecord parameters property.  Thus is it
   808      * processed specially by output Formatters and is not treated
   809      * as a formatting parameter to the LogRecord message property.
   810      * <p>
   811      * @param   level   One of the message level identifiers, e.g., SEVERE
   812      * @param   sourceClass    name of class that issued the logging request
   813      * @param   sourceMethod   name of method that issued the logging request
   814      * @param   bundleName     name of resource bundle to localize msg,
   815      *                         can be null
   816      * @param   msg     The string message (or a key in the message catalog)
   817      * @param   thrown  Throwable associated with log message.
   818      */
   819     public void logrb(Level level, String sourceClass, String sourceMethod,
   820                                         String bundleName, String msg, Throwable thrown) {
   821         if (level.intValue() < levelValue || levelValue == offValue) {
   822             return;
   823         }
   824         LogRecord lr = new LogRecord(level, msg);
   825         lr.setSourceClassName(sourceClass);
   826         lr.setSourceMethodName(sourceMethod);
   827         lr.setThrown(thrown);
   828         doLog(lr, bundleName);
   829     }
   830 
   831 
   832     //======================================================================
   833     // Start of convenience methods for logging method entries and returns.
   834     //======================================================================
   835 
   836     /**
   837      * Log a method entry.
   838      * <p>
   839      * This is a convenience method that can be used to log entry
   840      * to a method.  A LogRecord with message "ENTRY", log level
   841      * FINER, and the given sourceMethod and sourceClass is logged.
   842      * <p>
   843      * @param   sourceClass    name of class that issued the logging request
   844      * @param   sourceMethod   name of method that is being entered
   845      */
   846     public void entering(String sourceClass, String sourceMethod) {
   847         if (Level.FINER.intValue() < levelValue) {
   848             return;
   849         }
   850         logp(Level.FINER, sourceClass, sourceMethod, "ENTRY");
   851     }
   852 
   853     /**
   854      * Log a method entry, with one parameter.
   855      * <p>
   856      * This is a convenience method that can be used to log entry
   857      * to a method.  A LogRecord with message "ENTRY {0}", log level
   858      * FINER, and the given sourceMethod, sourceClass, and parameter
   859      * is logged.
   860      * <p>
   861      * @param   sourceClass    name of class that issued the logging request
   862      * @param   sourceMethod   name of method that is being entered
   863      * @param   param1         parameter to the method being entered
   864      */
   865     public void entering(String sourceClass, String sourceMethod, Object param1) {
   866         if (Level.FINER.intValue() < levelValue) {
   867             return;
   868         }
   869         Object params[] = { param1 };
   870         logp(Level.FINER, sourceClass, sourceMethod, "ENTRY {0}", params);
   871     }
   872 
   873     /**
   874      * Log a method entry, with an array of parameters.
   875      * <p>
   876      * This is a convenience method that can be used to log entry
   877      * to a method.  A LogRecord with message "ENTRY" (followed by a
   878      * format {N} indicator for each entry in the parameter array),
   879      * log level FINER, and the given sourceMethod, sourceClass, and
   880      * parameters is logged.
   881      * <p>
   882      * @param   sourceClass    name of class that issued the logging request
   883      * @param   sourceMethod   name of method that is being entered
   884      * @param   params         array of parameters to the method being entered
   885      */
   886     public void entering(String sourceClass, String sourceMethod, Object params[]) {
   887         if (Level.FINER.intValue() < levelValue) {
   888             return;
   889         }
   890         String msg = "ENTRY";
   891         if (params == null ) {
   892            logp(Level.FINER, sourceClass, sourceMethod, msg);
   893            return;
   894         }
   895         for (int i = 0; i < params.length; i++) {
   896             msg = msg + " {" + i + "}";
   897         }
   898         logp(Level.FINER, sourceClass, sourceMethod, msg, params);
   899     }
   900 
   901     /**
   902      * Log a method return.
   903      * <p>
   904      * This is a convenience method that can be used to log returning
   905      * from a method.  A LogRecord with message "RETURN", log level
   906      * FINER, and the given sourceMethod and sourceClass is logged.
   907      * <p>
   908      * @param   sourceClass    name of class that issued the logging request
   909      * @param   sourceMethod   name of the method
   910      */
   911     public void exiting(String sourceClass, String sourceMethod) {
   912         if (Level.FINER.intValue() < levelValue) {
   913             return;
   914         }
   915         logp(Level.FINER, sourceClass, sourceMethod, "RETURN");
   916     }
   917 
   918 
   919     /**
   920      * Log a method return, with result object.
   921      * <p>
   922      * This is a convenience method that can be used to log returning
   923      * from a method.  A LogRecord with message "RETURN {0}", log level
   924      * FINER, and the gives sourceMethod, sourceClass, and result
   925      * object is logged.
   926      * <p>
   927      * @param   sourceClass    name of class that issued the logging request
   928      * @param   sourceMethod   name of the method
   929      * @param   result  Object that is being returned
   930      */
   931     public void exiting(String sourceClass, String sourceMethod, Object result) {
   932         if (Level.FINER.intValue() < levelValue) {
   933             return;
   934         }
   935         Object params[] = { result };
   936         logp(Level.FINER, sourceClass, sourceMethod, "RETURN {0}", result);
   937     }
   938 
   939     /**
   940      * Log throwing an exception.
   941      * <p>
   942      * This is a convenience method to log that a method is
   943      * terminating by throwing an exception.  The logging is done
   944      * using the FINER level.
   945      * <p>
   946      * If the logger is currently enabled for the given message
   947      * level then the given arguments are stored in a LogRecord
   948      * which is forwarded to all registered output handlers.  The
   949      * LogRecord's message is set to "THROW".
   950      * <p>
   951      * Note that the thrown argument is stored in the LogRecord thrown
   952      * property, rather than the LogRecord parameters property.  Thus is it
   953      * processed specially by output Formatters and is not treated
   954      * as a formatting parameter to the LogRecord message property.
   955      * <p>
   956      * @param   sourceClass    name of class that issued the logging request
   957      * @param   sourceMethod  name of the method.
   958      * @param   thrown  The Throwable that is being thrown.
   959      */
   960     public void throwing(String sourceClass, String sourceMethod, Throwable thrown) {
   961         if (Level.FINER.intValue() < levelValue || levelValue == offValue ) {
   962             return;
   963         }
   964         LogRecord lr = new LogRecord(Level.FINER, "THROW");
   965         lr.setSourceClassName(sourceClass);
   966         lr.setSourceMethodName(sourceMethod);
   967         lr.setThrown(thrown);
   968         doLog(lr);
   969     }
   970 
   971     //=======================================================================
   972     // Start of simple convenience methods using level names as method names
   973     //=======================================================================
   974 
   975     /**
   976      * Log a SEVERE message.
   977      * <p>
   978      * If the logger is currently enabled for the SEVERE message
   979      * level then the given message is forwarded to all the
   980      * registered output Handler objects.
   981      * <p>
   982      * @param   msg     The string message (or a key in the message catalog)
   983      */
   984     public void severe(String msg) {
   985         if (Level.SEVERE.intValue() < levelValue) {
   986             return;
   987         }
   988         log(Level.SEVERE, msg);
   989     }
   990 
   991     /**
   992      * Log a WARNING message.
   993      * <p>
   994      * If the logger is currently enabled for the WARNING message
   995      * level then the given message is forwarded to all the
   996      * registered output Handler objects.
   997      * <p>
   998      * @param   msg     The string message (or a key in the message catalog)
   999      */
  1000     public void warning(String msg) {
  1001         if (Level.WARNING.intValue() < levelValue) {
  1002             return;
  1003         }
  1004         log(Level.WARNING, msg);
  1005     }
  1006 
  1007     /**
  1008      * Log an INFO message.
  1009      * <p>
  1010      * If the logger is currently enabled for the INFO message
  1011      * level then the given message is forwarded to all the
  1012      * registered output Handler objects.
  1013      * <p>
  1014      * @param   msg     The string message (or a key in the message catalog)
  1015      */
  1016     public void info(String msg) {
  1017         if (Level.INFO.intValue() < levelValue) {
  1018             return;
  1019         }
  1020         log(Level.INFO, msg);
  1021     }
  1022 
  1023     /**
  1024      * Log a CONFIG message.
  1025      * <p>
  1026      * If the logger is currently enabled for the CONFIG message
  1027      * level then the given message is forwarded to all the
  1028      * registered output Handler objects.
  1029      * <p>
  1030      * @param   msg     The string message (or a key in the message catalog)
  1031      */
  1032     public void config(String msg) {
  1033         if (Level.CONFIG.intValue() < levelValue) {
  1034             return;
  1035         }
  1036         log(Level.CONFIG, msg);
  1037     }
  1038 
  1039     /**
  1040      * Log a FINE message.
  1041      * <p>
  1042      * If the logger is currently enabled for the FINE message
  1043      * level then the given message is forwarded to all the
  1044      * registered output Handler objects.
  1045      * <p>
  1046      * @param   msg     The string message (or a key in the message catalog)
  1047      */
  1048     public void fine(String msg) {
  1049         if (Level.FINE.intValue() < levelValue) {
  1050             return;
  1051         }
  1052         log(Level.FINE, msg);
  1053     }
  1054 
  1055     /**
  1056      * Log a FINER message.
  1057      * <p>
  1058      * If the logger is currently enabled for the FINER message
  1059      * level then the given message is forwarded to all the
  1060      * registered output Handler objects.
  1061      * <p>
  1062      * @param   msg     The string message (or a key in the message catalog)
  1063      */
  1064     public void finer(String msg) {
  1065         if (Level.FINER.intValue() < levelValue) {
  1066             return;
  1067         }
  1068         log(Level.FINER, msg);
  1069     }
  1070 
  1071     /**
  1072      * Log a FINEST message.
  1073      * <p>
  1074      * If the logger is currently enabled for the FINEST message
  1075      * level then the given message is forwarded to all the
  1076      * registered output Handler objects.
  1077      * <p>
  1078      * @param   msg     The string message (or a key in the message catalog)
  1079      */
  1080     public void finest(String msg) {
  1081         if (Level.FINEST.intValue() < levelValue) {
  1082             return;
  1083         }
  1084         log(Level.FINEST, msg);
  1085     }
  1086 
  1087     //================================================================
  1088     // End of convenience methods
  1089     //================================================================
  1090 
  1091     /**
  1092      * Set the log level specifying which message levels will be
  1093      * logged by this logger.  Message levels lower than this
  1094      * value will be discarded.  The level value Level.OFF
  1095      * can be used to turn off logging.
  1096      * <p>
  1097      * If the new level is null, it means that this node should
  1098      * inherit its level from its nearest ancestor with a specific
  1099      * (non-null) level value.
  1100      *
  1101      * @param newLevel   the new value for the log level (may be null)
  1102      * @exception  SecurityException  if a security manager exists and if
  1103      *             the caller does not have LoggingPermission("control").
  1104      */
  1105     public void setLevel(Level newLevel) throws SecurityException {
  1106         levelValue = newLevel.intValue();
  1107         levelObject = newLevel;
  1108     }
  1109 
  1110     /**
  1111      * Get the log Level that has been specified for this Logger.
  1112      * The result may be null, which means that this logger's
  1113      * effective level will be inherited from its parent.
  1114      *
  1115      * @return  this Logger's level
  1116      */
  1117     public Level getLevel() {
  1118         return levelObject;
  1119     }
  1120 
  1121     /**
  1122      * Check if a message of the given level would actually be logged
  1123      * by this logger.  This check is based on the Loggers effective level,
  1124      * which may be inherited from its parent.
  1125      *
  1126      * @param   level   a message logging level
  1127      * @return  true if the given message level is currently being logged.
  1128      */
  1129     public boolean isLoggable(Level level) {
  1130         if (level.intValue() < levelValue || levelValue == offValue) {
  1131             return false;
  1132         }
  1133         return true;
  1134     }
  1135 
  1136     /**
  1137      * Get the name for this logger.
  1138      * @return logger name.  Will be null for anonymous Loggers.
  1139      */
  1140     public String getName() {
  1141         return name;
  1142     }
  1143 
  1144     /**
  1145      * Add a log Handler to receive logging messages.
  1146      * <p>
  1147      * By default, Loggers also send their output to their parent logger.
  1148      * Typically the root Logger is configured with a set of Handlers
  1149      * that essentially act as default handlers for all loggers.
  1150      *
  1151      * @param   handler a logging Handler
  1152      * @exception  SecurityException  if a security manager exists and if
  1153      *             the caller does not have LoggingPermission("control").
  1154      */
  1155 //    public void addHandler(Handler handler) throws SecurityException {
  1156 //        // Check for null handler
  1157 //        handler.getClass();
  1158 //        checkAccess();
  1159 //        handlers.add(handler);
  1160 //    }
  1161 
  1162     /**
  1163      * Remove a log Handler.
  1164      * <P>
  1165      * Returns silently if the given Handler is not found or is null
  1166      *
  1167      * @param   handler a logging Handler
  1168      * @exception  SecurityException  if a security manager exists and if
  1169      *             the caller does not have LoggingPermission("control").
  1170      */
  1171 //    public void removeHandler(Handler handler) throws SecurityException {
  1172 //        checkAccess();
  1173 //        if (handler == null) {
  1174 //            return;
  1175 //        }
  1176 //        handlers.remove(handler);
  1177 //    }
  1178 
  1179     /**
  1180      * Get the Handlers associated with this logger.
  1181      * <p>
  1182      * @return  an array of all registered Handlers
  1183      */
  1184 //    public Handler[] getHandlers() {
  1185 //        return handlers.toArray(emptyHandlers);
  1186 //    }
  1187 
  1188     /**
  1189      * Specify whether or not this logger should send its output
  1190      * to its parent Logger.  This means that any LogRecords will
  1191      * also be written to the parent's Handlers, and potentially
  1192      * to its parent, recursively up the namespace.
  1193      *
  1194      * @param useParentHandlers   true if output is to be sent to the
  1195      *          logger's parent.
  1196      * @exception  SecurityException  if a security manager exists and if
  1197      *             the caller does not have LoggingPermission("control").
  1198      */
  1199     public void setUseParentHandlers(boolean useParentHandlers) {
  1200         checkAccess();
  1201     }
  1202 
  1203     /**
  1204      * Discover whether or not this logger is sending its output
  1205      * to its parent logger.
  1206      *
  1207      * @return  true if output is to be sent to the logger's parent
  1208      */
  1209     public boolean getUseParentHandlers() {
  1210         return true;
  1211     }
  1212 
  1213     /**
  1214      * Return the parent for this Logger.
  1215      * <p>
  1216      * This method returns the nearest extant parent in the namespace.
  1217      * Thus if a Logger is called "a.b.c.d", and a Logger called "a.b"
  1218      * has been created but no logger "a.b.c" exists, then a call of
  1219      * getParent on the Logger "a.b.c.d" will return the Logger "a.b".
  1220      * <p>
  1221      * The result will be null if it is called on the root Logger
  1222      * in the namespace.
  1223      *
  1224      * @return nearest existing parent Logger
  1225      */
  1226     public Logger getParent() {
  1227         // Note: this used to be synchronized on treeLock.  However, this only
  1228         // provided memory semantics, as there was no guarantee that the caller
  1229         // would synchronize on treeLock (in fact, there is no way for external
  1230         // callers to so synchronize).  Therefore, we have made parent volatile
  1231         // instead.
  1232         String n = getName();
  1233         int at = n.length();
  1234         for (;;) {
  1235             int last = n.lastIndexOf('.', at - 1);
  1236             if (last == -1) {
  1237                 return getGlobal();
  1238             }
  1239             Logger p = ALL.get(n.substring(0, last));
  1240             if (p != null) {
  1241                 return p;
  1242             }
  1243             at = last;
  1244         }
  1245     }
  1246 
  1247     /**
  1248      * Set the parent for this Logger.  This method is used by
  1249      * the LogManager to update a Logger when the namespace changes.
  1250      * <p>
  1251      * It should not be called from application code.
  1252      * <p>
  1253      * @param  parent   the new parent logger
  1254      * @exception  SecurityException  if a security manager exists and if
  1255      *             the caller does not have LoggingPermission("control").
  1256      */
  1257     public void setParent(Logger parent) {
  1258         if (parent == null) {
  1259             throw new NullPointerException();
  1260         }
  1261         checkAccess();
  1262     }
  1263 
  1264 }