Java Code Examples for org.apache.logging.log4j.core.config.LoggerConfig#removeAppender()

The following examples show how to use org.apache.logging.log4j.core.config.LoggerConfig#removeAppender() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeobfuscatingRewritePolicy.java    From VanillaFix with MIT License 6 votes vote down vote up
/** Modifies the log4j config to add the policy **/
public static void install() {
    Logger rootLogger = (Logger) LogManager.getRootLogger();
    LoggerConfig loggerConfig = rootLogger.get();

    // Remove appender refs from config
    List<AppenderRef> appenderRefs = new ArrayList<>(loggerConfig.getAppenderRefs());
    for (AppenderRef appenderRef : appenderRefs) {
        loggerConfig.removeAppender(appenderRef.getRef());
    }

    // Create the RewriteAppender, which wraps the appenders
    RewriteAppender rewriteAppender = RewriteAppender.createAppender(
            "VanillaFixDeobfuscatingAppender",
            "true",
            appenderRefs.toArray(new AppenderRef[0]),
            rootLogger.getContext().getConfiguration(),
            new DeobfuscatingRewritePolicy(),
            null
    );
    rewriteAppender.start();

    // Add the new appender
    loggerConfig.addAppender(rewriteAppender, null, null);
}
 
Example 2
Source File: LoggingMixin.java    From picocli with Apache License 2.0 6 votes vote down vote up
/**
 * Configures the Log4j2 console appender(s), using the specified verbosity:
 * <ul>
 *   <li>{@code -vvv} : enable TRACE level</li>
 *   <li>{@code -vv} : enable DEBUG level</li>
 *   <li>{@code -v} : enable INFO level</li>
 *   <li>(not specified) : enable WARN level</li>
 * </ul>
 */
public void configureLoggers() {
    Level level = getTopLevelCommandLoggingMixin(mixee).calcLogLevel();

    LoggerContext loggerContext = LoggerContext.getContext(false);
    LoggerConfig rootConfig = loggerContext.getConfiguration().getRootLogger();
    for (Appender appender : rootConfig.getAppenders().values()) {
        if (appender instanceof ConsoleAppender) {
            rootConfig.removeAppender(appender.getName());
            rootConfig.addAppender(appender, level, null);
        }
    }
    if (rootConfig.getLevel().isMoreSpecificThan(level)) {
        rootConfig.setLevel(level);
    }
    loggerContext.updateLoggers(); // apply the changes
}
 
Example 3
Source File: Log4j2Watcher.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public void registerListener(ListenerConfig cfg) {
  if (history != null)
    throw new IllegalStateException("History already registered");

  history = new CircularList<LogEvent>(cfg.size);

  Level threshold = (cfg.threshold != null) ? Level.toLevel(cfg.threshold) : Level.WARN;
  ThresholdFilter filter = ThresholdFilter.createFilter(threshold, Filter.Result.ACCEPT, Filter.Result.DENY);

  // If there's already an appender like this, remove it
  LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
  LoggerConfig config = getLoggerConfig(ctx, LoggerInfo.ROOT_NAME);

  appender = new Log4j2Appender(this, filter, threshold); // "Log4j2WatcherAppender"

  config.removeAppender(appender.getName());

  if (!appender.isStarted())
    appender.start();

  config.addAppender(appender, threshold, filter);
  ctx.updateLoggers();
}
 
Example 4
Source File: LoggerStringWriter.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
private static void removeWriterAppender(final LoggerContext context) {
    final Configuration config = context.getConfiguration();
    LoggerConfig rootConfig = config.getRootLogger();
    // Stopping the logger is subject to race conditions where logging during cleanup on global executor
    // may still try to log and raise an error.
    WriterAppender writerAppender = (WriterAppender) rootConfig.getAppenders().get(APPENDER_NAME);
    if (writerAppender != null) {
        writerAppender.stop(0, NANOSECONDS);
    }
    // Don't remove directly from map, because the root logger also cleans up filters.
    rootConfig.removeAppender(APPENDER_NAME);
}
 
Example 5
Source File: LoggingMixin.java    From picocli with Apache License 2.0 5 votes vote down vote up
private void configureAppender(LoggerContext loggerContext, Level level) {
    final LoggerConfig rootConfig = loggerContext.getConfiguration().getRootLogger();
    for (Appender appender : rootConfig.getAppenders().values()) {
        if (appender instanceof ConsoleAppender) {
            rootConfig.removeAppender(appender.getName());
            rootConfig.addAppender(appender, level, null);
        }
    }
    if (rootConfig.getLevel().isMoreSpecificThan(level)) {
        rootConfig.setLevel(level);
    }
    loggerContext.updateLoggers();
}
 
Example 6
Source File: LogTestRule.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Override
protected void after() {
    if (loggerClass == null) {
        return;
    }

    final LoggerContext ctx = getLoggerContext();
    LoggerConfig loggerConfig = ctx.getConfiguration().getLoggerConfig(loggerClass.getName());
    loggerConfig.setLevel(oldLevel);
    loggerConfig.removeAppender(APPENDER_NAME);
    ctx.updateLoggers();
}
 
Example 7
Source File: Log4j2Watcher.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public void setThreshold(String level) {
  Log4j2Appender app = getAppender();
  Level current = app.getThreshold();
  app.setThreshold(Level.toLevel(level));
  LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
  LoggerConfig config = getLoggerConfig(ctx, LoggerInfo.ROOT_NAME);
  config.removeAppender(app.getName());
  config.addAppender(app, app.getThreshold(), app.getFilter());
  ((LoggerContext)LogManager.getContext(false)).updateLoggers();
  if (log.isInfoEnabled()) {
    log.info("Updated watcher threshold from {} to {} ", current, level);
  }
}
 
Example 8
Source File: RequestLoggingTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testLogBeforeExecute(Logger logger) throws InterruptedException {
  Level level = logger.getLevel();

  LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
  LoggerConfig config = ctx.getConfiguration().getLoggerConfig(logger.getName());
  config.setLevel(Level.DEBUG);
  config.addAppender(appender, Level.DEBUG, null);
  ctx.updateLoggers();

  try {
    assertQ(req("q", "*:*"));

    TimeOut timeOut = new TimeOut(10, TimeUnit.SECONDS, TimeSource.NANO_TIME);
    boolean found = false;
    Matcher matcher;
    String pat = "DEBUG.*q=\\*:\\*.*";
    String output = "";
    Pattern pattern = Pattern.compile(pat);
    do {
      output = writer.toString();
      matcher = pattern.matcher(output);
      found = matcher.find();
      if (found) {
        break;
      }
      timeOut.sleep(10);
    } while (timeOut.hasTimedOut() == false);
    assertTrue("Did not find expected pattern: '" + pat + "' in output: '" + output + "'", found);
    final String group = matcher.group();
    final String msg = "Should not have post query information";
    assertFalse(msg, group.contains("hits"));
    assertFalse(msg, group.contains("status"));
    assertFalse(msg, group.contains("QTime"));
  } finally {
    config.setLevel(level);
    config.removeAppender(appender.getName());
    ctx.updateLoggers();
  }
}
 
Example 9
Source File: JavaInstanceRunnable.java    From pulsar with Apache License 2.0 5 votes vote down vote up
private void removeLogTopicHandler() {
    if (logAppender == null) return;
    LoggerContext context = LoggerContext.getContext(false);
    Configuration config = context.getConfiguration();
    for (final LoggerConfig loggerConfig : config.getLoggers().values()) {
        loggerConfig.removeAppender(logAppender.getName());
    }
    config.getRootLogger().removeAppender(logAppender.getName());
}
 
Example 10
Source File: Log4J2Appender.java    From javamelody with Apache License 2.0 5 votes vote down vote up
void deregister() {
	if (LogManager.getContext(false) instanceof LoggerContext) {
		final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
		if (ctx.getConfiguration() instanceof AbstractConfiguration) {
			final AbstractConfiguration config = (AbstractConfiguration) ctx.getConfiguration();
			final Appender appender = getSingleton();
			appender.stop();
			config.removeAppender(appender.getName());
			final Logger rootLogger = LogManager.getRootLogger();
			final LoggerConfig loggerConfig = config.getLoggerConfig(rootLogger.getName());
			loggerConfig.removeAppender(appender.getName());
			ctx.updateLoggers();
		}
	}
}
 
Example 11
Source File: S2STestService.java    From Openfire with Apache License 2.0 5 votes vote down vote up
void removeAppender(final String name) {
    final LoggerContext context = LoggerContext.getContext(false);
    final Configuration config = context.getConfiguration();
    config.getAppenders().remove( name ).stop();

    for (final LoggerConfig loggerConfig : config.getLoggers().values()) {
        loggerConfig.removeAppender( name );
    }
    config.getRootLogger().removeAppender( name );
}