Search in sources :

Example 36 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project hive by apache.

the class StringAppender method removeFromLogger.

@VisibleForTesting
public void removeFromLogger(String loggerName) {
    LoggerConfig loggerConfig = configuration.getLoggerConfig(loggerName);
    loggerConfig.removeAppender(APPENDER_NAME);
    context.updateLoggers();
}
Also used : LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 37 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project elasticsearch by elastic.

the class Loggers method findAppender.

public static Appender findAppender(final Logger logger, final Class<? extends Appender> clazz) {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    final LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
    for (final Map.Entry<String, Appender> entry : loggerConfig.getAppenders().entrySet()) {
        if (entry.getValue().getClass().equals(clazz)) {
            return entry.getValue();
        }
    }
    return null;
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Configuration(org.apache.logging.log4j.core.config.Configuration) Configuration.getConfiguration(javax.security.auth.login.Configuration.getConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Map(java.util.Map) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 38 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project elasticsearch by elastic.

the class Loggers method removeAppender.

public static void removeAppender(final Logger logger, final Appender appender) {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
    if (!logger.getName().equals(loggerConfig.getName())) {
        loggerConfig = new LoggerConfig(logger.getName(), logger.getLevel(), true);
        config.addLogger(logger.getName(), loggerConfig);
    }
    loggerConfig.removeAppender(appender.getName());
    ctx.updateLoggers();
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Configuration.getConfiguration(javax.security.auth.login.Configuration.getConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 39 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project elasticsearch by elastic.

the class Loggers method setLevel.

public static void setLevel(Logger logger, Level level) {
    if (!LogManager.ROOT_LOGGER_NAME.equals(logger.getName())) {
        Configurator.setLevel(logger.getName(), level);
    } else {
        final LoggerContext ctx = LoggerContext.getContext(false);
        final Configuration config = ctx.getConfiguration();
        final LoggerConfig loggerConfig = config.getLoggerConfig(logger.getName());
        loggerConfig.setLevel(level);
        ctx.updateLoggers();
    }
    // we have to descend the hierarchy
    final LoggerContext ctx = LoggerContext.getContext(false);
    for (final LoggerConfig loggerConfig : ctx.getConfiguration().getLoggers().values()) {
        if (LogManager.ROOT_LOGGER_NAME.equals(logger.getName()) || loggerConfig.getName().startsWith(logger.getName() + ".")) {
            Configurator.setLevel(loggerConfig.getName(), level);
        }
    }
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) Configuration.getConfiguration(javax.security.auth.login.Configuration.getConfiguration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 40 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project elasticsearch by elastic.

the class EvilLoggerConfigurationTests method testResolveMultipleConfigs.

public void testResolveMultipleConfigs() throws Exception {
    final Level level = ESLoggerFactory.getLogger("test").getLevel();
    try {
        final Path configDir = getDataPath("config");
        final Settings settings = Settings.builder().put(Environment.PATH_CONF_SETTING.getKey(), configDir.toAbsolutePath()).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).build();
        final Environment environment = new Environment(settings);
        LogConfigurator.configure(environment);
        {
            final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
            final Configuration config = ctx.getConfiguration();
            final LoggerConfig loggerConfig = config.getLoggerConfig("test");
            final Appender appender = loggerConfig.getAppenders().get("console");
            assertThat(appender, notNullValue());
        }
        {
            final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
            final Configuration config = ctx.getConfiguration();
            final LoggerConfig loggerConfig = config.getLoggerConfig("second");
            final Appender appender = loggerConfig.getAppenders().get("console2");
            assertThat(appender, notNullValue());
        }
        {
            final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
            final Configuration config = ctx.getConfiguration();
            final LoggerConfig loggerConfig = config.getLoggerConfig("third");
            final Appender appender = loggerConfig.getAppenders().get("console3");
            assertThat(appender, notNullValue());
        }
    } finally {
        Configurator.setLevel("test", level);
    }
}
Also used : Path(java.nio.file.Path) Appender(org.apache.logging.log4j.core.Appender) Configuration(org.apache.logging.log4j.core.config.Configuration) Environment(org.elasticsearch.env.Environment) Level(org.apache.logging.log4j.Level) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Settings(org.elasticsearch.common.settings.Settings) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Aggregations

LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)63 Configuration (org.apache.logging.log4j.core.config.Configuration)38 LoggerContext (org.apache.logging.log4j.core.LoggerContext)32 Appender (org.apache.logging.log4j.core.Appender)11 Level (org.apache.logging.log4j.Level)9 Test (org.junit.Test)7 VisibleForTesting (com.google.common.annotations.VisibleForTesting)6 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)6 Logger (org.apache.logging.log4j.Logger)5 Filter (org.apache.logging.log4j.core.Filter)5 Configuration.getConfiguration (javax.security.auth.login.Configuration.getConfiguration)4 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)4 ThresholdFilter (org.apache.logging.log4j.core.filter.ThresholdFilter)4 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)3 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)3 PropertyChangeListener (java.beans.PropertyChangeListener)2 File (java.io.File)2 Path (java.nio.file.Path)2 Map (java.util.Map)2 AppenderContext (org.apache.geode.internal.logging.log4j.AppenderContext)2