Пример #1
0
 /**
  * Create a PatternParser.
  *
  * @param config The Configuration.
  * @param filterClass Filter the returned plugins after calling the plugin manager.
  * @return The PatternParser.
  */
 private static PatternParser createPatternParser(
     final Configuration config, final Class<? extends PatternConverter> filterClass) {
   if (config == null) {
     return new PatternParser(
         config, PatternLayout.KEY, LogEventPatternConverter.class, filterClass);
   }
   PatternParser parser = config.getComponent(COMPONENT_KEY);
   if (parser == null) {
     parser = new PatternParser(config, PatternLayout.KEY, ThrowablePatternConverter.class);
     config.addComponent(COMPONENT_KEY, parser);
     parser = (PatternParser) config.getComponent(COMPONENT_KEY);
   }
   return parser;
 }
Пример #2
0
  /**
   * Sets the Configuration to be used.
   *
   * @param config The new Configuration.
   * @return The previous Configuration.
   */
  private Configuration setConfiguration(final Configuration config) {
    Objects.requireNonNull(config, "No Configuration was provided");
    configLock.lock();
    try {
      final Configuration prev = this.configuration;
      config.addListener(this);
      final ConcurrentMap<String, String> map =
          config.getComponent(Configuration.CONTEXT_PROPERTIES);

      try { // LOG4J2-719 network access may throw android.os.NetworkOnMainThreadException
        map.putIfAbsent("hostName", NetUtils.getLocalHostname());
      } catch (final Exception ex) {
        LOGGER.debug("Ignoring {}, setting hostName to 'unknown'", ex.toString());
        map.putIfAbsent("hostName", "unknown");
      }
      map.putIfAbsent("contextName", contextName);
      config.start();
      this.configuration = config;
      updateLoggers();
      if (prev != null) {
        prev.removeListener(this);
        prev.stop();
      }

      firePropertyChangeEvent(new PropertyChangeEvent(this, PROPERTY_CONFIG, prev, config));

      try {
        Server.reregisterMBeansAfterReconfigure();
      } catch (final Throwable t) {
        // LOG4J2-716: Android has no java.lang.management
        LOGGER.error("Could not reconfigure JMX", t);
      }
      Log4jLogEvent.setNanoClock(NanoClockFactory.createNanoClock());
      AsyncLogger.setNanoClock(NanoClockFactory.createNanoClock());
      return prev;
    } finally {
      configLock.unlock();
    }
  }