Example #1
0
  @Override
  protected void registerMBeans() {
    MBeanServer server = ManagementFactory.getPlatformMBeanServer();

    try {
      server.registerMBean(this, new ObjectName(MBEAN_BOOTSTRAP_HOST));
    } catch (Exception e) {
      logger.error("Unable to register " + MBEAN_BOOTSTRAP_HOST + " mbean", e);
    }

    try {
      HierarchyDynamicMBean hdm = new HierarchyDynamicMBean();
      server.registerMBean(hdm, new ObjectName(MBEAN_LOG4J_HIERARCHY));

      // Add the root logger to the Hierarchy MBean
      hdm.addLoggerMBean(Logger.getRootLogger().getName());

      LoggerRepository r = LogManager.getLoggerRepository();

      @SuppressWarnings("rawtypes")
      Enumeration loggers = r.getCurrentLoggers();
      while (loggers.hasMoreElements()) {
        hdm.addLoggerMBean(((Logger) loggers.nextElement()).getName());
      }
    } catch (Exception e) {
      logger.error("Unable to register " + MBEAN_LOG4J_HIERARCHY + " mbean", e);
    }

    super.registerMBeans();
  }
 private void parseErrorHandler(
     final ErrorHandler eh,
     final String errorHandlerPrefix,
     final Properties props,
     final LoggerRepository hierarchy) {
   boolean rootRef =
       OptionConverter.toBoolean(
           OptionConverter.findAndSubst(errorHandlerPrefix + ROOT_REF, props), false);
   if (rootRef) {
     eh.setLogger(hierarchy.getRootLogger());
   }
   String loggerName = OptionConverter.findAndSubst(errorHandlerPrefix + LOGGER_REF, props);
   if (loggerName != null) {
     Logger logger =
         (loggerFactory == null)
             ? hierarchy.getLogger(loggerName)
             : hierarchy.getLogger(loggerName, loggerFactory);
     eh.setLogger(logger);
   }
   String appenderName =
       OptionConverter.findAndSubst(errorHandlerPrefix + APPENDER_REF_TAG, props);
   if (appenderName != null) {
     Appender backup = parseAppender(props, appenderName);
     if (backup != null) {
       eh.setBackupAppender(backup);
     }
   }
 }
  /**
   * Read configuration options from <code>properties</code>.
   *
   * <p>See {@link #doConfigure(String, LoggerRepository)} for the expected format.
   */
  public void doConfigure(Properties properties, LoggerRepository hierarchy) {
    repository = hierarchy;
    String value = properties.getProperty(LogLog.DEBUG_KEY);
    if (value == null) {
      value = properties.getProperty("log4j.configDebug");
      if (value != null)
        LogLog.warn("[log4j.configDebug] is deprecated. Use [log4j.debug] instead.");
    }

    if (value != null) {
      LogLog.setInternalDebugging(OptionConverter.toBoolean(value, true));
    }

    //
    //   if log4j.reset=true then
    //        reset hierarchy
    String reset = properties.getProperty(RESET_KEY);
    if (reset != null && OptionConverter.toBoolean(reset, false)) {
      hierarchy.resetConfiguration();
    }

    String thresholdStr = OptionConverter.findAndSubst(THRESHOLD_PREFIX, properties);
    if (thresholdStr != null) {
      hierarchy.setThreshold(OptionConverter.toLevel(thresholdStr, (Level) Level.ALL));
      LogLog.debug("Hierarchy threshold set to [" + hierarchy.getThreshold() + "].");
    }

    configureRootCategory(properties, hierarchy);
    configureLoggerFactory(properties);
    parseCatsAndRenderers(properties, hierarchy);

    LogLog.debug("Finished configuring.");
    // We don't want to hold references to appenders preventing their
    // garbage collection.
    registry.clear();
  }
  void configureRootCategory(Properties props, LoggerRepository hierarchy) {
    String effectiveFrefix = ROOT_LOGGER_PREFIX;
    String value = OptionConverter.findAndSubst(ROOT_LOGGER_PREFIX, props);

    if (value == null) {
      value = OptionConverter.findAndSubst(ROOT_CATEGORY_PREFIX, props);
      effectiveFrefix = ROOT_CATEGORY_PREFIX;
    }

    if (value == null) LogLog.debug("Could not find root logger information. Is this OK?");
    else {
      Logger root = hierarchy.getRootLogger();
      synchronized (root) {
        parseCategory(props, root, effectiveFrefix, INTERNAL_ROOT_NAME, value);
      }
    }
  }
 /** Parse non-root elements, such non-root categories and renderers. */
 protected void parseCatsAndRenderers(Properties props, LoggerRepository hierarchy) {
   Enumeration enumeration = props.propertyNames();
   while (enumeration.hasMoreElements()) {
     String key = (String) enumeration.nextElement();
     if (key.startsWith(CATEGORY_PREFIX) || key.startsWith(LOGGER_PREFIX)) {
       String loggerName = null;
       if (key.startsWith(CATEGORY_PREFIX)) {
         loggerName = key.substring(CATEGORY_PREFIX.length());
       } else if (key.startsWith(LOGGER_PREFIX)) {
         loggerName = key.substring(LOGGER_PREFIX.length());
       }
       String value = OptionConverter.findAndSubst(key, props);
       Logger logger = hierarchy.getLogger(loggerName, loggerFactory);
       synchronized (logger) {
         parseCategory(props, logger, key, loggerName, value);
         parseAdditivityForLogger(props, logger, loggerName);
       }
     } else if (key.startsWith(RENDERER_PREFIX)) {
       String renderedClass = key.substring(RENDERER_PREFIX.length());
       String renderingClass = OptionConverter.findAndSubst(key, props);
       if (hierarchy instanceof RendererSupport) {
         RendererMap.addRenderer((RendererSupport) hierarchy, renderedClass, renderingClass);
       }
     } else if (key.equals(THROWABLE_RENDERER_PREFIX)) {
       if (hierarchy instanceof ThrowableRendererSupport) {
         ThrowableRenderer tr =
             (ThrowableRenderer)
                 OptionConverter.instantiateByKey(
                     props,
                     THROWABLE_RENDERER_PREFIX,
                     org.apache.log4j.spi.ThrowableRenderer.class,
                     null);
         if (tr == null) {
           LogLog.error("Could not instantiate throwableRenderer.");
         } else {
           PropertySetter setter = new PropertySetter(tr);
           setter.setProperties(props, THROWABLE_RENDERER_PREFIX + ".");
           ((ThrowableRendererSupport) hierarchy).setThrowableRenderer(tr);
         }
       }
     }
   }
 }