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

The following examples show how to use org.apache.logging.log4j.core.config.LoggerConfig#getAppenders() . 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: StartupLoggingUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Disables all log4j2 ConsoleAppender's by modifying log4j configuration dynamically.
 * Must only be used during early startup
 * @return true if ok or else false if something happened, e.g. log4j2 classes were not in classpath
 */
@SuppressForbidden(reason = "Legitimate log4j2 access")
public static boolean muteConsole() {
  try {
    if (!isLog4jActive()) {
      logNotSupported("Could not mute logging to console.");
      return false;
    }
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME);
    Map<String, Appender> appenders = loggerConfig.getAppenders();
    appenders.forEach((name, appender) -> {
      if (appender instanceof ConsoleAppender) {
        loggerConfig.removeAppender(name);
        ctx.updateLoggers();
      }
    });
    return true;
  } catch (Exception e) {
    logNotSupported("Could not mute logging to console.");
    return false;
  }
}
 
Example 2
Source File: Log4j2Helper.java    From arthas with Apache License 2.0 5 votes vote down vote up
private static List<Map<String, Object>> doGetLoggerAppenders(LoggerConfig loggerConfig) {
    List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();

    Map<String, Appender> appenders = loggerConfig.getAppenders();

    for (Entry<String, Appender> entry : appenders.entrySet()) {
        Map<String, Object> info = new HashMap<String, Object>();
        Appender appender = entry.getValue();
        info.put(LoggerHelper.name, appender.getName());
        info.put(LoggerHelper.clazz, appender.getClass());

        result.add(info);
        if (appender instanceof FileAppender) {
            info.put(LoggerHelper.file, ((FileAppender) appender).getFileName());
        } else if (appender instanceof ConsoleAppender) {
            info.put(LoggerHelper.target, ((ConsoleAppender) appender).getTarget());
        } else if (appender instanceof AsyncAppender) {

            AsyncAppender asyncAppender = ((AsyncAppender) appender);
            String[] appenderRefStrings = asyncAppender.getAppenderRefStrings();

            info.put(LoggerHelper.blocking, asyncAppender.isBlocking());
            info.put(LoggerHelper.appenderRef, Arrays.asList(appenderRefStrings));
        }
    }
    return result;
}