changeset 31150:2aa1d300cd75

8080933: LogManager.demandSystemLogger should accept a 'caller' argument. Summary: The caller's class is now passed to LogManager.demandSystemLogger Reviewed-by: mchung
author dfuchs
date Tue, 16 Jun 2015 12:15:54 +0200
parents 117fb33bcd51
children 5535c077def0
files jdk/src/java.logging/share/classes/java/util/logging/LogManager.java jdk/src/java.logging/share/classes/java/util/logging/Logger.java
diffstat 2 files changed, 53 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/jdk/src/java.logging/share/classes/java/util/logging/LogManager.java	Mon Jun 15 10:56:31 2015 -0700
+++ b/jdk/src/java.logging/share/classes/java/util/logging/LogManager.java	Tue Jun 16 12:15:54 2015 +0200
@@ -540,9 +540,10 @@
         return result;
     }
 
-    Logger demandSystemLogger(String name, String resourceBundleName) {
+    Logger demandSystemLogger(String name, String resourceBundleName, Class<?> caller) {
         // Add a system logger in the system context's namespace
-        final Logger sysLogger = getSystemContext().demandLogger(name, resourceBundleName);
+        final Logger sysLogger = getSystemContext()
+                .demandLogger(name, resourceBundleName, caller);
 
         // Add the system logger to the LogManager's namespace if not exist
         // so that there is only one single logger of the given name.
@@ -627,11 +628,11 @@
             return global;
         }
 
-        Logger demandLogger(String name, String resourceBundleName) {
+        Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
             // a LogManager subclass may have its own implementation to add and
             // get a Logger.  So delegate to the LogManager to do the work.
             final LogManager owner = getOwner();
-            return owner.demandLogger(name, resourceBundleName, null);
+            return owner.demandLogger(name, resourceBundleName, caller);
         }
 
 
@@ -869,7 +870,7 @@
                     owner.getProperty(pname + ".handlers") != null) {
                     // This pname has a level/handlers definition.
                     // Make sure it exists.
-                    demandLogger(pname, null);
+                    demandLogger(pname, null, null);
                 }
                 ix = ix2+1;
             }
@@ -912,11 +913,11 @@
         // one single logger of the given name.  System loggers are visible
         // to applications unless a logger of the same name has been added.
         @Override
-        Logger demandLogger(String name, String resourceBundleName) {
+        Logger demandLogger(String name, String resourceBundleName, Class<?> caller) {
             Logger result = findLogger(name);
             if (result == null) {
                 // only allocate the new system logger once
-                Logger newLogger = new Logger(name, resourceBundleName, null, getOwner(), true);
+                Logger newLogger = new Logger(name, resourceBundleName, caller, getOwner(), true);
                 do {
                     if (addLocalLogger(newLogger)) {
                         // We successfully added the new Logger that we
--- a/jdk/src/java.logging/share/classes/java/util/logging/Logger.java	Mon Jun 15 10:56:31 2015 -0700
+++ b/jdk/src/java.logging/share/classes/java/util/logging/Logger.java	Tue Jun 16 12:15:54 2015 +0200
@@ -450,7 +450,7 @@
         SecurityManager sm = System.getSecurityManager();
         if (sm != null && !SystemLoggerHelper.disableCallerCheck) {
             if (caller.getClassLoader() == null) {
-                return manager.demandSystemLogger(name, resourceBundleName);
+                return manager.demandSystemLogger(name, resourceBundleName, caller);
             }
         }
         return manager.demandLogger(name, resourceBundleName, caller);
@@ -500,7 +500,23 @@
         // would throw an IllegalArgumentException in the second call
         // because the wrapper would result in an attempt to replace
         // the existing "resourceBundleForFoo" with null.
-        return demandLogger(name, null, Reflection.getCallerClass());
+        return Logger.getLogger(name, Reflection.getCallerClass());
+    }
+
+    /**
+     * Find or create a logger for a named subsystem on behalf
+     * of the given caller.
+     *
+     * This method is called by {@link #getLogger(java.lang.String)} after
+     * it has obtained a reference to its caller's class.
+     *
+     * @param   name            A name for the logger.
+     * @param   callerClass     The class that called {@link
+     *                          #getLogger(java.lang.String)}.
+     * @return a suitable Logger for {@code callerClass}.
+     */
+    private static Logger getLogger(String name, Class<?> callerClass) {
+        return demandLogger(name, null, callerClass);
     }
 
     /**
@@ -550,7 +566,30 @@
     // adding a new Logger object is handled by LogManager.addLogger().
     @CallerSensitive
     public static Logger getLogger(String name, String resourceBundleName) {
-        Class<?> callerClass = Reflection.getCallerClass();
+        return Logger.getLogger(name, resourceBundleName, Reflection.getCallerClass());
+    }
+
+    /**
+     * Find or create a logger for a named subsystem on behalf
+     * of the given caller.
+     *
+     * This method is called by {@link
+     * #getLogger(java.lang.String, java.lang.String)} after
+     * it has obtained a reference to its caller's class.
+     *
+     * @param   name            A name for the logger.
+     * @param   resourceBundleName  name of ResourceBundle to be used for localizing
+     *                          messages for this logger. May be {@code null}
+     *                          if none of the messages require localization.
+     * @param   callerClass     The class that called {@link
+     *                          #getLogger(java.lang.String, java.lang.String)}.
+     *                          This class will also be used for locating the
+     *                          resource bundle if {@code resourceBundleName} is
+     *                          not {@code null}.
+     * @return a suitable Logger for {@code callerClass}.
+     */
+    private static Logger getLogger(String name, String resourceBundleName,
+                                    Class<?> callerClass) {
         Logger result = demandLogger(name, resourceBundleName, callerClass);
 
         // MissingResourceException or IllegalArgumentException can be
@@ -573,8 +612,9 @@
         LogManager manager = LogManager.getLogManager();
 
         // all loggers in the system context will default to
-        // the system logger's resource bundle
-        Logger result = manager.demandSystemLogger(name, SYSTEM_LOGGER_RB_NAME);
+        // the system logger's resource bundle - therefore the caller won't
+        // be needed and can be null.
+        Logger result = manager.demandSystemLogger(name, SYSTEM_LOGGER_RB_NAME, null);
         return result;
     }