Beispiel #1
0
  public void testDefaultLoggerProperties() throws Exception {
    // mock LogManager has no default logger
    assertNull(mockManager.getLogger(""));
    assertNull(mockManager.getLogger("global"));

    // non-mock LogManager has two default logger
    Logger global = manager.getLogger("global");
    Logger root = manager.getLogger("");

    assertSame(global, Logger.global);
    assertSame(root, global.getParent());

    // root properties
    manager.readConfiguration(EnvironmentHelper.PropertiesToInputStream(props));
    assertNull(root.getFilter());
    assertEquals(2, root.getHandlers().length);
    assertEquals(Level.FINE, root.getLevel());
    assertEquals("", root.getName());
    assertSame(root.getParent(), null);
    assertNull(root.getResourceBundle());
    assertNull(root.getResourceBundleName());
    assertTrue(root.getUseParentHandlers());
  }
Beispiel #2
0
  /**
   * Finds a registered logger for a subsystem, or creates one in case no logger has been registered
   * yet.
   *
   * <p>If a logger with the specified name has already been registered, the behavior depends on the
   * resource bundle that is currently associated with the existing logger.
   *
   * <ul>
   *   <li>If the existing logger uses the same resource bundle as specified by <code>
   *       resourceBundleName</code>, the existing logger is returned.
   *   <li>If the existing logger currently does not localize messages, the existing logger is
   *       modified to use the bundle specified by <code>resourceBundleName</code>. The existing
   *       logger is then returned. Therefore, all subsystems currently using this logger will
   *       produce localized messages from now on.
   *   <li>If the existing logger already has an associated resource bundle, but a different one
   *       than specified by <code>resourceBundleName</code>, an <code>IllegalArgumentException
   *       </code> is thrown.
   * </ul>
   *
   * @param name the name for the logger, for example "java.awt" or "org.gnu.foo". The name should
   *     be based on the name of the package issuing log records and consist of dot-separated Java
   *     identifiers.
   * @param resourceBundleName the name of a resource bundle for localizing messages, or <code>null
   *     </code> to indicate that messages do not need to be localized.
   * @return a logger for the subsystem specified by <code>name</code>.
   * @throws java.util.MissingResourceException if <code>resourceBundleName</code> is not <code>null
   *     </code> and no such bundle could be located.
   * @throws IllegalArgumentException if a logger for the subsystem identified by <code>name</code>
   *     has already been created, but uses a different resource bundle for localizing messages.
   * @throws NullPointerException if <code>name</code> is <code>null</code>.
   */
  public static Logger getLogger(String name, String resourceBundleName) {
    LogManager lm = LogManager.getLogManager();
    Logger result;

    if (name == null) throw new NullPointerException();

    /*
     * Without synchronized(lm), it could happen that another thread would
     * create a logger between our calls to getLogger and addLogger. While
     * addLogger would indicate this by returning false, we could not be sure
     * that this other logger was still existing when we called getLogger a
     * second time in order to retrieve it -- note that LogManager is only
     * allowed to keep weak references to registered loggers, so Loggers can be
     * garbage collected at any time in general, and between our call to
     * addLogger and our second call go getLogger in particular. Of course, we
     * assume here that LogManager.addLogger etc. are synchronizing on the
     * global LogManager object. There is a comment in the implementation of
     * LogManager.addLogger referring to this comment here, so that any change
     * in the synchronization of LogManager will be reflected here.
     */
    synchronized (lock) {
      synchronized (lm) {
        result = lm.getLogger(name);
        if (result == null) {
          boolean couldBeAdded;

          result = new Logger(name, resourceBundleName);
          couldBeAdded = lm.addLogger(result);
          if (!couldBeAdded) throw new IllegalStateException("cannot register new logger");
        } else {
          /*
           * The logger already exists. Make sure it uses the same
           * resource bundle for localizing messages.
           */
          String existingBundleName = result.getResourceBundleName();

          /*
           * The Sun J2SE 1.4 reference implementation will return the
           * registered logger object, even if it does not have a resource
           * bundle associated with it. However, it seems to change the
           * resourceBundle of the registered logger to the bundle whose
           * name was passed to getLogger.
           */
          if ((existingBundleName == null) && (resourceBundleName != null)) {
            /*
             * If ResourceBundle.getBundle throws an exception, the
             * existing logger will be unchanged. This would be
             * different if the assignment to resourceBundleName came
             * first.
             */
            result.resourceBundle = ResourceBundle.getBundle(resourceBundleName);

            result.resourceBundleName = resourceBundleName;
            return result;
          }

          if ((existingBundleName != resourceBundleName)
              && ((existingBundleName == null) || !existingBundleName.equals(resourceBundleName))) {
            throw new IllegalArgumentException();
          }
        }
      }
    }

    return result;
  }
Beispiel #3
0
 /** @see java.util.logging.Logger#getResourceBundleName() */
 @Override
 public String getResourceBundleName() {
   return delegate.getResourceBundleName();
 }