< prev index next >

src/java.logging/share/classes/java/util/logging/Logger.java

Print this page

  23  * questions.
  24  */
  25 
  26 package java.util.logging;
  27 
  28 import java.lang.ref.WeakReference;
  29 import java.security.AccessController;
  30 import java.security.PrivilegedAction;
  31 import java.util.ArrayList;
  32 import java.util.Iterator;
  33 import java.util.Locale;
  34 import java.util.MissingResourceException;
  35 import java.util.Objects;
  36 import java.util.ResourceBundle;
  37 import java.util.concurrent.CopyOnWriteArrayList;
  38 import java.util.function.Supplier;
  39 
  40 import jdk.internal.access.JavaUtilResourceBundleAccess;
  41 import jdk.internal.access.SharedSecrets;
  42 import jdk.internal.reflect.CallerSensitive;

  43 import jdk.internal.reflect.Reflection;
  44 import static jdk.internal.logger.DefaultLoggerFinder.isSystem;
  45 
  46 /**
  47  * A Logger object is used to log messages for a specific
  48  * system or application component.  Loggers are normally named,
  49  * using a hierarchical dot-separated namespace.  Logger names
  50  * can be arbitrary strings, but they should normally be based on
  51  * the package name or class name of the logged component, such
  52  * as java.net or javax.swing.  In addition it is possible to create
  53  * "anonymous" Loggers that are not stored in the Logger namespace.
  54  * <p>
  55  * Logger objects may be obtained by calls on one of the getLogger
  56  * factory methods.  These will either create a new Logger or
  57  * return a suitable existing Logger. It is important to note that
  58  * the Logger returned by one of the {@code getLogger} factory methods
  59  * may be garbage collected at any time if a strong reference to the
  60  * Logger is not kept.
  61  * <p>
  62  * Logging messages will be forwarded to registered Handler

 697         //     getLogger("Foo");
 698         //
 699         // would throw an IllegalArgumentException in the second call
 700         // because the wrapper would result in an attempt to replace
 701         // the existing "resourceBundleForFoo" with null.
 702         return Logger.getLogger(name, Reflection.getCallerClass());
 703     }
 704 
 705     /**
 706      * Find or create a logger for a named subsystem on behalf
 707      * of the given caller.
 708      *
 709      * This method is called by {@link #getLogger(java.lang.String)} after
 710      * it has obtained a reference to its caller's class.
 711      *
 712      * @param   name            A name for the logger.
 713      * @param   callerClass     The class that called {@link
 714      *                          #getLogger(java.lang.String)}.
 715      * @return a suitable Logger for {@code callerClass}.
 716      */

 717     private static Logger getLogger(String name, Class<?> callerClass) {
 718         return demandLogger(name, null, callerClass);
 719     }
 720 
 721     /**
 722      * Find or create a logger for a named subsystem.  If a logger has
 723      * already been created with the given name it is returned.  Otherwise
 724      * a new logger is created.
 725      *
 726      * <p>
 727      * If a new logger is created its log level will be configured
 728      * based on the LogManager and it will be configured to also send logging
 729      * output to its parent's Handlers.  It will be registered in
 730      * the LogManager global namespace.
 731      * <p>
 732      * Note: The LogManager may only retain a weak reference to the newly
 733      * created Logger. It is important to understand that a previously
 734      * created Logger with the given name may be garbage collected at any
 735      * time if there is no strong reference to the Logger. In particular,
 736      * this means that two back-to-back calls like

 771 
 772     /**
 773      * Find or create a logger for a named subsystem on behalf
 774      * of the given caller.
 775      *
 776      * This method is called by {@link
 777      * #getLogger(java.lang.String, java.lang.String)} after
 778      * it has obtained a reference to its caller's class.
 779      *
 780      * @param   name            A name for the logger.
 781      * @param   resourceBundleName  name of ResourceBundle to be used for localizing
 782      *                          messages for this logger. May be {@code null}
 783      *                          if none of the messages require localization.
 784      * @param   callerClass     The class that called {@link
 785      *                          #getLogger(java.lang.String, java.lang.String)}.
 786      *                          This class will also be used for locating the
 787      *                          resource bundle if {@code resourceBundleName} is
 788      *                          not {@code null}.
 789      * @return a suitable Logger for {@code callerClass}.
 790      */

 791     private static Logger getLogger(String name, String resourceBundleName,
 792                                     Class<?> callerClass) {
 793         Logger result = demandLogger(name, resourceBundleName, callerClass);
 794 
 795         // MissingResourceException or IllegalArgumentException can be
 796         // thrown by setupResourceInfo().
 797         // We have to set the callers ClassLoader here in case demandLogger
 798         // above found a previously created Logger.  This can happen, for
 799         // example, if Logger.getLogger(name) is called and subsequently
 800         // Logger.getLogger(name, resourceBundleName) is called.  In this case
 801         // we won't necessarily have the correct classloader saved away, so
 802         // we need to set it here, too.
 803 
 804         result.setupResourceInfo(resourceBundleName, callerClass);
 805         return result;
 806     }
 807 
 808     // package-private
 809     // Add a platform logger to the system context.
 810     // i.e. caller of sun.util.logging.PlatformLogger.getLogger

  23  * questions.
  24  */
  25 
  26 package java.util.logging;
  27 
  28 import java.lang.ref.WeakReference;
  29 import java.security.AccessController;
  30 import java.security.PrivilegedAction;
  31 import java.util.ArrayList;
  32 import java.util.Iterator;
  33 import java.util.Locale;
  34 import java.util.MissingResourceException;
  35 import java.util.Objects;
  36 import java.util.ResourceBundle;
  37 import java.util.concurrent.CopyOnWriteArrayList;
  38 import java.util.function.Supplier;
  39 
  40 import jdk.internal.access.JavaUtilResourceBundleAccess;
  41 import jdk.internal.access.SharedSecrets;
  42 import jdk.internal.reflect.CallerSensitive;
  43 import jdk.internal.reflect.CallerSensitiveAdapter;
  44 import jdk.internal.reflect.Reflection;
  45 import static jdk.internal.logger.DefaultLoggerFinder.isSystem;
  46 
  47 /**
  48  * A Logger object is used to log messages for a specific
  49  * system or application component.  Loggers are normally named,
  50  * using a hierarchical dot-separated namespace.  Logger names
  51  * can be arbitrary strings, but they should normally be based on
  52  * the package name or class name of the logged component, such
  53  * as java.net or javax.swing.  In addition it is possible to create
  54  * "anonymous" Loggers that are not stored in the Logger namespace.
  55  * <p>
  56  * Logger objects may be obtained by calls on one of the getLogger
  57  * factory methods.  These will either create a new Logger or
  58  * return a suitable existing Logger. It is important to note that
  59  * the Logger returned by one of the {@code getLogger} factory methods
  60  * may be garbage collected at any time if a strong reference to the
  61  * Logger is not kept.
  62  * <p>
  63  * Logging messages will be forwarded to registered Handler

 698         //     getLogger("Foo");
 699         //
 700         // would throw an IllegalArgumentException in the second call
 701         // because the wrapper would result in an attempt to replace
 702         // the existing "resourceBundleForFoo" with null.
 703         return Logger.getLogger(name, Reflection.getCallerClass());
 704     }
 705 
 706     /**
 707      * Find or create a logger for a named subsystem on behalf
 708      * of the given caller.
 709      *
 710      * This method is called by {@link #getLogger(java.lang.String)} after
 711      * it has obtained a reference to its caller's class.
 712      *
 713      * @param   name            A name for the logger.
 714      * @param   callerClass     The class that called {@link
 715      *                          #getLogger(java.lang.String)}.
 716      * @return a suitable Logger for {@code callerClass}.
 717      */
 718     @CallerSensitiveAdapter
 719     private static Logger getLogger(String name, Class<?> callerClass) {
 720         return demandLogger(name, null, callerClass);
 721     }
 722 
 723     /**
 724      * Find or create a logger for a named subsystem.  If a logger has
 725      * already been created with the given name it is returned.  Otherwise
 726      * a new logger is created.
 727      *
 728      * <p>
 729      * If a new logger is created its log level will be configured
 730      * based on the LogManager and it will be configured to also send logging
 731      * output to its parent's Handlers.  It will be registered in
 732      * the LogManager global namespace.
 733      * <p>
 734      * Note: The LogManager may only retain a weak reference to the newly
 735      * created Logger. It is important to understand that a previously
 736      * created Logger with the given name may be garbage collected at any
 737      * time if there is no strong reference to the Logger. In particular,
 738      * this means that two back-to-back calls like

 773 
 774     /**
 775      * Find or create a logger for a named subsystem on behalf
 776      * of the given caller.
 777      *
 778      * This method is called by {@link
 779      * #getLogger(java.lang.String, java.lang.String)} after
 780      * it has obtained a reference to its caller's class.
 781      *
 782      * @param   name            A name for the logger.
 783      * @param   resourceBundleName  name of ResourceBundle to be used for localizing
 784      *                          messages for this logger. May be {@code null}
 785      *                          if none of the messages require localization.
 786      * @param   callerClass     The class that called {@link
 787      *                          #getLogger(java.lang.String, java.lang.String)}.
 788      *                          This class will also be used for locating the
 789      *                          resource bundle if {@code resourceBundleName} is
 790      *                          not {@code null}.
 791      * @return a suitable Logger for {@code callerClass}.
 792      */
 793     @CallerSensitiveAdapter
 794     private static Logger getLogger(String name, String resourceBundleName,
 795                                     Class<?> callerClass) {
 796         Logger result = demandLogger(name, resourceBundleName, callerClass);
 797 
 798         // MissingResourceException or IllegalArgumentException can be
 799         // thrown by setupResourceInfo().
 800         // We have to set the callers ClassLoader here in case demandLogger
 801         // above found a previously created Logger.  This can happen, for
 802         // example, if Logger.getLogger(name) is called and subsequently
 803         // Logger.getLogger(name, resourceBundleName) is called.  In this case
 804         // we won't necessarily have the correct classloader saved away, so
 805         // we need to set it here, too.
 806 
 807         result.setupResourceInfo(resourceBundleName, callerClass);
 808         return result;
 809     }
 810 
 811     // package-private
 812     // Add a platform logger to the system context.
 813     // i.e. caller of sun.util.logging.PlatformLogger.getLogger
< prev index next >