rt/emul/compact/src/main/java/java/util/logging/Logger.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 09 Oct 2013 16:07:19 +0200
changeset 1355 ae20214a816c
parent 1277 853245102164
child 1356 637f7fb50abd
permissions -rw-r--r--
Replace parameters and log only if console object is present
     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         } else {
   465             for (int i = 0; i < params.length; i++) {
   466                 msg = msg.replace("{" + i + "}", params[i] == null ? "null" : params[i].toString());
   467             }
   468         }
   469 
   470         consoleLog(
   471             method, 
   472             record.getLoggerName(), msg);
   473     }
   474     
   475     @JavaScriptBody(args = { "method", "logger", "msg" }, body = 
   476         "if (console) console[method]('[' + logger + ']: ' + msg);"
   477     )
   478     private static native void consoleLog(
   479         String method, String logger, String msg
   480     );
   481 
   482     // private support method for logging.
   483     // We fill in the logger name, resource bundle name, and
   484     // resource bundle and then call "void log(LogRecord)".
   485     private void doLog(LogRecord lr) {
   486         doLog(lr, lr.getResourceBundleName());
   487     }
   488     private void doLog(LogRecord lr, String bundleName) {
   489         lr.setLoggerName(name);
   490         log(lr);
   491     }
   492 
   493 
   494     //================================================================
   495     // Start of convenience methods WITHOUT className and methodName
   496     //================================================================
   497 
   498     /**
   499      * Log a message, with no arguments.
   500      * <p>
   501      * If the logger is currently enabled for the given message
   502      * level then the given message is forwarded to all the
   503      * registered output Handler objects.
   504      * <p>
   505      * @param   level   One of the message level identifiers, e.g., SEVERE
   506      * @param   msg     The string message (or a key in the message catalog)
   507      */
   508     public void log(Level level, String msg) {
   509         if (level.intValue() < levelValue || levelValue == offValue) {
   510             return;
   511         }
   512         LogRecord lr = new LogRecord(level, msg);
   513         doLog(lr);
   514     }
   515 
   516     /**
   517      * Log a message, with one object parameter.
   518      * <p>
   519      * If the logger is currently enabled for the given message
   520      * level then a corresponding LogRecord is created and forwarded
   521      * to all the registered output Handler objects.
   522      * <p>
   523      * @param   level   One of the message level identifiers, e.g., SEVERE
   524      * @param   msg     The string message (or a key in the message catalog)
   525      * @param   param1  parameter to the message
   526      */
   527     public void log(Level level, String msg, Object param1) {
   528         if (level.intValue() < levelValue || levelValue == offValue) {
   529             return;
   530         }
   531         LogRecord lr = new LogRecord(level, msg);
   532         Object params[] = { param1 };
   533         lr.setParameters(params);
   534         doLog(lr);
   535     }
   536 
   537     /**
   538      * Log a message, with an array of object arguments.
   539      * <p>
   540      * If the logger is currently enabled for the given message
   541      * level then a corresponding LogRecord is created and forwarded
   542      * to all the registered output Handler objects.
   543      * <p>
   544      * @param   level   One of the message level identifiers, e.g., SEVERE
   545      * @param   msg     The string message (or a key in the message catalog)
   546      * @param   params  array of parameters to the message
   547      */
   548     public void log(Level level, String msg, Object params[]) {
   549         if (level.intValue() < levelValue || levelValue == offValue) {
   550             return;
   551         }
   552         LogRecord lr = new LogRecord(level, msg);
   553         lr.setParameters(params);
   554         doLog(lr);
   555     }
   556 
   557     /**
   558      * Log a message, with associated Throwable information.
   559      * <p>
   560      * If the logger is currently enabled for the given message
   561      * level then the given arguments are stored in a LogRecord
   562      * which is forwarded to all registered output handlers.
   563      * <p>
   564      * Note that the thrown argument is stored in the LogRecord thrown
   565      * property, rather than the LogRecord parameters property.  Thus is it
   566      * processed specially by output Formatters and is not treated
   567      * as a formatting parameter to the LogRecord message property.
   568      * <p>
   569      * @param   level   One of the message level identifiers, e.g., SEVERE
   570      * @param   msg     The string message (or a key in the message catalog)
   571      * @param   thrown  Throwable associated with log message.
   572      */
   573     public void log(Level level, String msg, Throwable thrown) {
   574         if (level.intValue() < levelValue || levelValue == offValue) {
   575             return;
   576         }
   577         LogRecord lr = new LogRecord(level, msg);
   578         lr.setThrown(thrown);
   579         doLog(lr);
   580     }
   581 
   582     //================================================================
   583     // Start of convenience methods WITH className and methodName
   584     //================================================================
   585 
   586     /**
   587      * Log a message, specifying source class and method,
   588      * with no arguments.
   589      * <p>
   590      * If the logger is currently enabled for the given message
   591      * level then the given message is forwarded to all the
   592      * registered output Handler objects.
   593      * <p>
   594      * @param   level   One of the message level identifiers, e.g., SEVERE
   595      * @param   sourceClass    name of class that issued the logging request
   596      * @param   sourceMethod   name of method that issued the logging request
   597      * @param   msg     The string message (or a key in the message catalog)
   598      */
   599     public void logp(Level level, String sourceClass, String sourceMethod, String msg) {
   600         if (level.intValue() < levelValue || levelValue == offValue) {
   601             return;
   602         }
   603         LogRecord lr = new LogRecord(level, msg);
   604         lr.setSourceClassName(sourceClass);
   605         lr.setSourceMethodName(sourceMethod);
   606         doLog(lr);
   607     }
   608 
   609     /**
   610      * Log a message, specifying source class and method,
   611      * with a single object parameter to the log message.
   612      * <p>
   613      * If the logger is currently enabled for the given message
   614      * level then a corresponding LogRecord is created and forwarded
   615      * to all the registered output Handler objects.
   616      * <p>
   617      * @param   level   One of the message level identifiers, e.g., SEVERE
   618      * @param   sourceClass    name of class that issued the logging request
   619      * @param   sourceMethod   name of method that issued the logging request
   620      * @param   msg      The string message (or a key in the message catalog)
   621      * @param   param1    Parameter to the log message.
   622      */
   623     public void logp(Level level, String sourceClass, String sourceMethod,
   624                                                 String msg, Object param1) {
   625         if (level.intValue() < levelValue || levelValue == offValue) {
   626             return;
   627         }
   628         LogRecord lr = new LogRecord(level, msg);
   629         lr.setSourceClassName(sourceClass);
   630         lr.setSourceMethodName(sourceMethod);
   631         Object params[] = { param1 };
   632         lr.setParameters(params);
   633         doLog(lr);
   634     }
   635 
   636     /**
   637      * Log a message, specifying source class and method,
   638      * with an array of object arguments.
   639      * <p>
   640      * If the logger is currently enabled for the given message
   641      * level then a corresponding LogRecord is created and forwarded
   642      * to all the registered output Handler objects.
   643      * <p>
   644      * @param   level   One of the message level identifiers, e.g., SEVERE
   645      * @param   sourceClass    name of class that issued the logging request
   646      * @param   sourceMethod   name of method that issued the logging request
   647      * @param   msg     The string message (or a key in the message catalog)
   648      * @param   params  Array of parameters to the message
   649      */
   650     public void logp(Level level, String sourceClass, String sourceMethod,
   651                                                 String msg, Object params[]) {
   652         if (level.intValue() < levelValue || levelValue == offValue) {
   653             return;
   654         }
   655         LogRecord lr = new LogRecord(level, msg);
   656         lr.setSourceClassName(sourceClass);
   657         lr.setSourceMethodName(sourceMethod);
   658         lr.setParameters(params);
   659         doLog(lr);
   660     }
   661 
   662     /**
   663      * Log a message, specifying source class and method,
   664      * with associated Throwable information.
   665      * <p>
   666      * If the logger is currently enabled for the given message
   667      * level then the given arguments are stored in a LogRecord
   668      * which is forwarded to all registered output handlers.
   669      * <p>
   670      * Note that the thrown argument is stored in the LogRecord thrown
   671      * property, rather than the LogRecord parameters property.  Thus is it
   672      * processed specially by output Formatters and is not treated
   673      * as a formatting parameter to the LogRecord message property.
   674      * <p>
   675      * @param   level   One of the message level identifiers, e.g., SEVERE
   676      * @param   sourceClass    name of class that issued the logging request
   677      * @param   sourceMethod   name of method that issued the logging request
   678      * @param   msg     The string message (or a key in the message catalog)
   679      * @param   thrown  Throwable associated with log message.
   680      */
   681     public void logp(Level level, String sourceClass, String sourceMethod,
   682                                                         String msg, Throwable thrown) {
   683         if (level.intValue() < levelValue || levelValue == offValue) {
   684             return;
   685         }
   686         LogRecord lr = new LogRecord(level, msg);
   687         lr.setSourceClassName(sourceClass);
   688         lr.setSourceMethodName(sourceMethod);
   689         lr.setThrown(thrown);
   690         doLog(lr);
   691     }
   692 
   693 
   694     //=========================================================================
   695     // Start of convenience methods WITH className, methodName and bundle name.
   696     //=========================================================================
   697 
   698 
   699     /**
   700      * Log a message, specifying source class, method, and resource bundle name
   701      * with no arguments.
   702      * <p>
   703      * If the logger is currently enabled for the given message
   704      * level then the given message is forwarded to all the
   705      * registered output Handler objects.
   706      * <p>
   707      * The msg string is localized using the named resource bundle.  If the
   708      * resource bundle name is null, or an empty String or invalid
   709      * then the msg string is not localized.
   710      * <p>
   711      * @param   level   One of the message level identifiers, e.g., SEVERE
   712      * @param   sourceClass    name of class that issued the logging request
   713      * @param   sourceMethod   name of method that issued the logging request
   714      * @param   bundleName     name of resource bundle to localize msg,
   715      *                         can be null
   716      * @param   msg     The string message (or a key in the message catalog)
   717      */
   718 
   719     public void logrb(Level level, String sourceClass, String sourceMethod,
   720                                 String bundleName, String msg) {
   721         if (level.intValue() < levelValue || levelValue == offValue) {
   722             return;
   723         }
   724         LogRecord lr = new LogRecord(level, msg);
   725         lr.setSourceClassName(sourceClass);
   726         lr.setSourceMethodName(sourceMethod);
   727         doLog(lr, bundleName);
   728     }
   729 
   730     /**
   731      * Log a message, specifying source class, method, and resource bundle name,
   732      * with a single object parameter to the log message.
   733      * <p>
   734      * If the logger is currently enabled for the given message
   735      * level then a corresponding LogRecord is created and forwarded
   736      * to all the registered output Handler objects.
   737      * <p>
   738      * The msg string is localized using the named resource bundle.  If the
   739      * resource bundle name is null, or an empty String or invalid
   740      * then the msg string is not localized.
   741      * <p>
   742      * @param   level   One of the message level identifiers, e.g., SEVERE
   743      * @param   sourceClass    name of class that issued the logging request
   744      * @param   sourceMethod   name of method that issued the logging request
   745      * @param   bundleName     name of resource bundle to localize msg,
   746      *                         can be null
   747      * @param   msg      The string message (or a key in the message catalog)
   748      * @param   param1    Parameter to the log message.
   749      */
   750     public void logrb(Level level, String sourceClass, String sourceMethod,
   751                                 String bundleName, String msg, Object param1) {
   752         if (level.intValue() < levelValue || levelValue == offValue) {
   753             return;
   754         }
   755         LogRecord lr = new LogRecord(level, msg);
   756         lr.setSourceClassName(sourceClass);
   757         lr.setSourceMethodName(sourceMethod);
   758         Object params[] = { param1 };
   759         lr.setParameters(params);
   760         doLog(lr, bundleName);
   761     }
   762 
   763     /**
   764      * Log a message, specifying source class, method, and resource bundle name,
   765      * with an array of object arguments.
   766      * <p>
   767      * If the logger is currently enabled for the given message
   768      * level then a corresponding LogRecord is created and forwarded
   769      * to all the registered output Handler objects.
   770      * <p>
   771      * The msg string is localized using the named resource bundle.  If the
   772      * resource bundle name is null, or an empty String or invalid
   773      * then the msg string is not localized.
   774      * <p>
   775      * @param   level   One of the message level identifiers, e.g., SEVERE
   776      * @param   sourceClass    name of class that issued the logging request
   777      * @param   sourceMethod   name of method that issued the logging request
   778      * @param   bundleName     name of resource bundle to localize msg,
   779      *                         can be null.
   780      * @param   msg     The string message (or a key in the message catalog)
   781      * @param   params  Array of parameters to the message
   782      */
   783     public void logrb(Level level, String sourceClass, String sourceMethod,
   784                                 String bundleName, String msg, Object params[]) {
   785         if (level.intValue() < levelValue || levelValue == offValue) {
   786             return;
   787         }
   788         LogRecord lr = new LogRecord(level, msg);
   789         lr.setSourceClassName(sourceClass);
   790         lr.setSourceMethodName(sourceMethod);
   791         lr.setParameters(params);
   792         doLog(lr, bundleName);
   793     }
   794 
   795     /**
   796      * Log a message, specifying source class, method, and resource bundle name,
   797      * with associated Throwable information.
   798      * <p>
   799      * If the logger is currently enabled for the given message
   800      * level then the given arguments are stored in a LogRecord
   801      * which is forwarded to all registered output handlers.
   802      * <p>
   803      * The msg string is localized using the named resource bundle.  If the
   804      * resource bundle name is null, or an empty String or invalid
   805      * then the msg string is not localized.
   806      * <p>
   807      * Note that the thrown argument is stored in the LogRecord thrown
   808      * property, rather than the LogRecord parameters property.  Thus is it
   809      * processed specially by output Formatters and is not treated
   810      * as a formatting parameter to the LogRecord message property.
   811      * <p>
   812      * @param   level   One of the message level identifiers, e.g., SEVERE
   813      * @param   sourceClass    name of class that issued the logging request
   814      * @param   sourceMethod   name of method that issued the logging request
   815      * @param   bundleName     name of resource bundle to localize msg,
   816      *                         can be null
   817      * @param   msg     The string message (or a key in the message catalog)
   818      * @param   thrown  Throwable associated with log message.
   819      */
   820     public void logrb(Level level, String sourceClass, String sourceMethod,
   821                                         String bundleName, String msg, Throwable thrown) {
   822         if (level.intValue() < levelValue || levelValue == offValue) {
   823             return;
   824         }
   825         LogRecord lr = new LogRecord(level, msg);
   826         lr.setSourceClassName(sourceClass);
   827         lr.setSourceMethodName(sourceMethod);
   828         lr.setThrown(thrown);
   829         doLog(lr, bundleName);
   830     }
   831 
   832 
   833     //======================================================================
   834     // Start of convenience methods for logging method entries and returns.
   835     //======================================================================
   836 
   837     /**
   838      * Log a method entry.
   839      * <p>
   840      * This is a convenience method that can be used to log entry
   841      * to a method.  A LogRecord with message "ENTRY", log level
   842      * FINER, and the given sourceMethod and sourceClass is logged.
   843      * <p>
   844      * @param   sourceClass    name of class that issued the logging request
   845      * @param   sourceMethod   name of method that is being entered
   846      */
   847     public void entering(String sourceClass, String sourceMethod) {
   848         if (Level.FINER.intValue() < levelValue) {
   849             return;
   850         }
   851         logp(Level.FINER, sourceClass, sourceMethod, "ENTRY");
   852     }
   853 
   854     /**
   855      * Log a method entry, with one parameter.
   856      * <p>
   857      * This is a convenience method that can be used to log entry
   858      * to a method.  A LogRecord with message "ENTRY {0}", log level
   859      * FINER, and the given sourceMethod, sourceClass, and parameter
   860      * is logged.
   861      * <p>
   862      * @param   sourceClass    name of class that issued the logging request
   863      * @param   sourceMethod   name of method that is being entered
   864      * @param   param1         parameter to the method being entered
   865      */
   866     public void entering(String sourceClass, String sourceMethod, Object param1) {
   867         if (Level.FINER.intValue() < levelValue) {
   868             return;
   869         }
   870         Object params[] = { param1 };
   871         logp(Level.FINER, sourceClass, sourceMethod, "ENTRY {0}", params);
   872     }
   873 
   874     /**
   875      * Log a method entry, with an array of parameters.
   876      * <p>
   877      * This is a convenience method that can be used to log entry
   878      * to a method.  A LogRecord with message "ENTRY" (followed by a
   879      * format {N} indicator for each entry in the parameter array),
   880      * log level FINER, and the given sourceMethod, sourceClass, and
   881      * parameters is logged.
   882      * <p>
   883      * @param   sourceClass    name of class that issued the logging request
   884      * @param   sourceMethod   name of method that is being entered
   885      * @param   params         array of parameters to the method being entered
   886      */
   887     public void entering(String sourceClass, String sourceMethod, Object params[]) {
   888         if (Level.FINER.intValue() < levelValue) {
   889             return;
   890         }
   891         String msg = "ENTRY";
   892         if (params == null ) {
   893            logp(Level.FINER, sourceClass, sourceMethod, msg);
   894            return;
   895         }
   896         for (int i = 0; i < params.length; i++) {
   897             msg = msg + " {" + i + "}";
   898         }
   899         logp(Level.FINER, sourceClass, sourceMethod, msg, params);
   900     }
   901 
   902     /**
   903      * Log a method return.
   904      * <p>
   905      * This is a convenience method that can be used to log returning
   906      * from a method.  A LogRecord with message "RETURN", log level
   907      * FINER, and the given sourceMethod and sourceClass is logged.
   908      * <p>
   909      * @param   sourceClass    name of class that issued the logging request
   910      * @param   sourceMethod   name of the method
   911      */
   912     public void exiting(String sourceClass, String sourceMethod) {
   913         if (Level.FINER.intValue() < levelValue) {
   914             return;
   915         }
   916         logp(Level.FINER, sourceClass, sourceMethod, "RETURN");
   917     }
   918 
   919 
   920     /**
   921      * Log a method return, with result object.
   922      * <p>
   923      * This is a convenience method that can be used to log returning
   924      * from a method.  A LogRecord with message "RETURN {0}", log level
   925      * FINER, and the gives sourceMethod, sourceClass, and result
   926      * object is logged.
   927      * <p>
   928      * @param   sourceClass    name of class that issued the logging request
   929      * @param   sourceMethod   name of the method
   930      * @param   result  Object that is being returned
   931      */
   932     public void exiting(String sourceClass, String sourceMethod, Object result) {
   933         if (Level.FINER.intValue() < levelValue) {
   934             return;
   935         }
   936         Object params[] = { result };
   937         logp(Level.FINER, sourceClass, sourceMethod, "RETURN {0}", result);
   938     }
   939 
   940     /**
   941      * Log throwing an exception.
   942      * <p>
   943      * This is a convenience method to log that a method is
   944      * terminating by throwing an exception.  The logging is done
   945      * using the FINER level.
   946      * <p>
   947      * If the logger is currently enabled for the given message
   948      * level then the given arguments are stored in a LogRecord
   949      * which is forwarded to all registered output handlers.  The
   950      * LogRecord's message is set to "THROW".
   951      * <p>
   952      * Note that the thrown argument is stored in the LogRecord thrown
   953      * property, rather than the LogRecord parameters property.  Thus is it
   954      * processed specially by output Formatters and is not treated
   955      * as a formatting parameter to the LogRecord message property.
   956      * <p>
   957      * @param   sourceClass    name of class that issued the logging request
   958      * @param   sourceMethod  name of the method.
   959      * @param   thrown  The Throwable that is being thrown.
   960      */
   961     public void throwing(String sourceClass, String sourceMethod, Throwable thrown) {
   962         if (Level.FINER.intValue() < levelValue || levelValue == offValue ) {
   963             return;
   964         }
   965         LogRecord lr = new LogRecord(Level.FINER, "THROW");
   966         lr.setSourceClassName(sourceClass);
   967         lr.setSourceMethodName(sourceMethod);
   968         lr.setThrown(thrown);
   969         doLog(lr);
   970     }
   971 
   972     //=======================================================================
   973     // Start of simple convenience methods using level names as method names
   974     //=======================================================================
   975 
   976     /**
   977      * Log a SEVERE message.
   978      * <p>
   979      * If the logger is currently enabled for the SEVERE message
   980      * level then the given message is forwarded to all the
   981      * registered output Handler objects.
   982      * <p>
   983      * @param   msg     The string message (or a key in the message catalog)
   984      */
   985     public void severe(String msg) {
   986         if (Level.SEVERE.intValue() < levelValue) {
   987             return;
   988         }
   989         log(Level.SEVERE, msg);
   990     }
   991 
   992     /**
   993      * Log a WARNING message.
   994      * <p>
   995      * If the logger is currently enabled for the WARNING message
   996      * level then the given message is forwarded to all the
   997      * registered output Handler objects.
   998      * <p>
   999      * @param   msg     The string message (or a key in the message catalog)
  1000      */
  1001     public void warning(String msg) {
  1002         if (Level.WARNING.intValue() < levelValue) {
  1003             return;
  1004         }
  1005         log(Level.WARNING, msg);
  1006     }
  1007 
  1008     /**
  1009      * Log an INFO message.
  1010      * <p>
  1011      * If the logger is currently enabled for the INFO message
  1012      * level then the given message is forwarded to all the
  1013      * registered output Handler objects.
  1014      * <p>
  1015      * @param   msg     The string message (or a key in the message catalog)
  1016      */
  1017     public void info(String msg) {
  1018         if (Level.INFO.intValue() < levelValue) {
  1019             return;
  1020         }
  1021         log(Level.INFO, msg);
  1022     }
  1023 
  1024     /**
  1025      * Log a CONFIG message.
  1026      * <p>
  1027      * If the logger is currently enabled for the CONFIG message
  1028      * level then the given message is forwarded to all the
  1029      * registered output Handler objects.
  1030      * <p>
  1031      * @param   msg     The string message (or a key in the message catalog)
  1032      */
  1033     public void config(String msg) {
  1034         if (Level.CONFIG.intValue() < levelValue) {
  1035             return;
  1036         }
  1037         log(Level.CONFIG, msg);
  1038     }
  1039 
  1040     /**
  1041      * Log a FINE message.
  1042      * <p>
  1043      * If the logger is currently enabled for the FINE message
  1044      * level then the given message is forwarded to all the
  1045      * registered output Handler objects.
  1046      * <p>
  1047      * @param   msg     The string message (or a key in the message catalog)
  1048      */
  1049     public void fine(String msg) {
  1050         if (Level.FINE.intValue() < levelValue) {
  1051             return;
  1052         }
  1053         log(Level.FINE, msg);
  1054     }
  1055 
  1056     /**
  1057      * Log a FINER message.
  1058      * <p>
  1059      * If the logger is currently enabled for the FINER message
  1060      * level then the given message is forwarded to all the
  1061      * registered output Handler objects.
  1062      * <p>
  1063      * @param   msg     The string message (or a key in the message catalog)
  1064      */
  1065     public void finer(String msg) {
  1066         if (Level.FINER.intValue() < levelValue) {
  1067             return;
  1068         }
  1069         log(Level.FINER, msg);
  1070     }
  1071 
  1072     /**
  1073      * Log a FINEST message.
  1074      * <p>
  1075      * If the logger is currently enabled for the FINEST message
  1076      * level then the given message is forwarded to all the
  1077      * registered output Handler objects.
  1078      * <p>
  1079      * @param   msg     The string message (or a key in the message catalog)
  1080      */
  1081     public void finest(String msg) {
  1082         if (Level.FINEST.intValue() < levelValue) {
  1083             return;
  1084         }
  1085         log(Level.FINEST, msg);
  1086     }
  1087 
  1088     //================================================================
  1089     // End of convenience methods
  1090     //================================================================
  1091 
  1092     /**
  1093      * Set the log level specifying which message levels will be
  1094      * logged by this logger.  Message levels lower than this
  1095      * value will be discarded.  The level value Level.OFF
  1096      * can be used to turn off logging.
  1097      * <p>
  1098      * If the new level is null, it means that this node should
  1099      * inherit its level from its nearest ancestor with a specific
  1100      * (non-null) level value.
  1101      *
  1102      * @param newLevel   the new value for the log level (may be null)
  1103      * @exception  SecurityException  if a security manager exists and if
  1104      *             the caller does not have LoggingPermission("control").
  1105      */
  1106     public void setLevel(Level newLevel) throws SecurityException {
  1107         levelValue = newLevel.intValue();
  1108         levelObject = newLevel;
  1109     }
  1110 
  1111     /**
  1112      * Get the log Level that has been specified for this Logger.
  1113      * The result may be null, which means that this logger's
  1114      * effective level will be inherited from its parent.
  1115      *
  1116      * @return  this Logger's level
  1117      */
  1118     public Level getLevel() {
  1119         return levelObject;
  1120     }
  1121 
  1122     /**
  1123      * Check if a message of the given level would actually be logged
  1124      * by this logger.  This check is based on the Loggers effective level,
  1125      * which may be inherited from its parent.
  1126      *
  1127      * @param   level   a message logging level
  1128      * @return  true if the given message level is currently being logged.
  1129      */
  1130     public boolean isLoggable(Level level) {
  1131         if (level.intValue() < levelValue || levelValue == offValue) {
  1132             return false;
  1133         }
  1134         return true;
  1135     }
  1136 
  1137     /**
  1138      * Get the name for this logger.
  1139      * @return logger name.  Will be null for anonymous Loggers.
  1140      */
  1141     public String getName() {
  1142         return name;
  1143     }
  1144 
  1145     /**
  1146      * Add a log Handler to receive logging messages.
  1147      * <p>
  1148      * By default, Loggers also send their output to their parent logger.
  1149      * Typically the root Logger is configured with a set of Handlers
  1150      * that essentially act as default handlers for all loggers.
  1151      *
  1152      * @param   handler a logging Handler
  1153      * @exception  SecurityException  if a security manager exists and if
  1154      *             the caller does not have LoggingPermission("control").
  1155      */
  1156 //    public void addHandler(Handler handler) throws SecurityException {
  1157 //        // Check for null handler
  1158 //        handler.getClass();
  1159 //        checkAccess();
  1160 //        handlers.add(handler);
  1161 //    }
  1162 
  1163     /**
  1164      * Remove a log Handler.
  1165      * <P>
  1166      * Returns silently if the given Handler is not found or is null
  1167      *
  1168      * @param   handler a logging Handler
  1169      * @exception  SecurityException  if a security manager exists and if
  1170      *             the caller does not have LoggingPermission("control").
  1171      */
  1172 //    public void removeHandler(Handler handler) throws SecurityException {
  1173 //        checkAccess();
  1174 //        if (handler == null) {
  1175 //            return;
  1176 //        }
  1177 //        handlers.remove(handler);
  1178 //    }
  1179 
  1180     /**
  1181      * Get the Handlers associated with this logger.
  1182      * <p>
  1183      * @return  an array of all registered Handlers
  1184      */
  1185 //    public Handler[] getHandlers() {
  1186 //        return handlers.toArray(emptyHandlers);
  1187 //    }
  1188 
  1189     /**
  1190      * Specify whether or not this logger should send its output
  1191      * to its parent Logger.  This means that any LogRecords will
  1192      * also be written to the parent's Handlers, and potentially
  1193      * to its parent, recursively up the namespace.
  1194      *
  1195      * @param useParentHandlers   true if output is to be sent to the
  1196      *          logger's parent.
  1197      * @exception  SecurityException  if a security manager exists and if
  1198      *             the caller does not have LoggingPermission("control").
  1199      */
  1200     public void setUseParentHandlers(boolean useParentHandlers) {
  1201         checkAccess();
  1202     }
  1203 
  1204     /**
  1205      * Discover whether or not this logger is sending its output
  1206      * to its parent logger.
  1207      *
  1208      * @return  true if output is to be sent to the logger's parent
  1209      */
  1210     public boolean getUseParentHandlers() {
  1211         return true;
  1212     }
  1213 
  1214     /**
  1215      * Return the parent for this Logger.
  1216      * <p>
  1217      * This method returns the nearest extant parent in the namespace.
  1218      * Thus if a Logger is called "a.b.c.d", and a Logger called "a.b"
  1219      * has been created but no logger "a.b.c" exists, then a call of
  1220      * getParent on the Logger "a.b.c.d" will return the Logger "a.b".
  1221      * <p>
  1222      * The result will be null if it is called on the root Logger
  1223      * in the namespace.
  1224      *
  1225      * @return nearest existing parent Logger
  1226      */
  1227     public Logger getParent() {
  1228         // Note: this used to be synchronized on treeLock.  However, this only
  1229         // provided memory semantics, as there was no guarantee that the caller
  1230         // would synchronize on treeLock (in fact, there is no way for external
  1231         // callers to so synchronize).  Therefore, we have made parent volatile
  1232         // instead.
  1233         String n = getName();
  1234         int at = n.length();
  1235         for (;;) {
  1236             int last = n.lastIndexOf('.', at - 1);
  1237             if (last == -1) {
  1238                 return getGlobal();
  1239             }
  1240             Logger p = ALL.get(n.substring(0, last));
  1241             if (p != null) {
  1242                 return p;
  1243             }
  1244             at = last;
  1245         }
  1246     }
  1247 
  1248     /**
  1249      * Set the parent for this Logger.  This method is used by
  1250      * the LogManager to update a Logger when the namespace changes.
  1251      * <p>
  1252      * It should not be called from application code.
  1253      * <p>
  1254      * @param  parent   the new parent logger
  1255      * @exception  SecurityException  if a security manager exists and if
  1256      *             the caller does not have LoggingPermission("control").
  1257      */
  1258     public void setParent(Logger parent) {
  1259         if (parent == null) {
  1260             throw new NullPointerException();
  1261         }
  1262         checkAccess();
  1263     }
  1264 
  1265 }