Search in sources :

Example 6 with LoggerConfig

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

the class EvilLoggerConfigurationTests method testLoggingLevelsFromSettings.

public void testLoggingLevelsFromSettings() throws IOException, UserException {
    final Level rootLevel = randomFrom(Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR);
    final Level fooLevel = randomFrom(Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR);
    final Level barLevel = randomFrom(Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR);
    final Path configDir = getDataPath("minimal");
    final Settings settings = Settings.builder().put(Environment.PATH_CONF_SETTING.getKey(), configDir.toAbsolutePath()).put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString()).put("logger.level", rootLevel.name()).put("logger.foo", fooLevel.name()).put("logger.bar", barLevel.name()).build();
    final Environment environment = new Environment(settings);
    LogConfigurator.configure(environment);
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    final Map<String, LoggerConfig> loggerConfigs = config.getLoggers();
    assertThat(loggerConfigs.size(), equalTo(3));
    assertThat(loggerConfigs, hasKey(""));
    assertThat(loggerConfigs.get("").getLevel(), equalTo(rootLevel));
    assertThat(loggerConfigs, hasKey("foo"));
    assertThat(loggerConfigs.get("foo").getLevel(), equalTo(fooLevel));
    assertThat(loggerConfigs, hasKey("bar"));
    assertThat(loggerConfigs.get("bar").getLevel(), equalTo(barLevel));
    assertThat(ctx.getLogger(randomAsciiOfLength(16)).getLevel(), equalTo(rootLevel));
}
Also used : Path(java.nio.file.Path) Configuration(org.apache.logging.log4j.core.config.Configuration) Environment(org.elasticsearch.env.Environment) Level(org.apache.logging.log4j.Level) Matchers.hasToString(org.hamcrest.Matchers.hasToString) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) LoggerContext(org.apache.logging.log4j.core.LoggerContext) Settings(org.elasticsearch.common.settings.Settings) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 7 with LoggerConfig

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

the class Loggers method addAppender.

public static void addAppender(final Logger logger, final Appender appender) {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    config.addAppender(appender);
    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.addAppender(appender, null, null);
    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 8 with LoggerConfig

use of org.apache.logging.log4j.core.config.LoggerConfig in project spring-boot by spring-projects.

the class Log4J2LoggingSystem method getLoggerConfigurations.

@Override
public List<LoggerConfiguration> getLoggerConfigurations() {
    List<LoggerConfiguration> result = new ArrayList<>();
    Configuration configuration = getLoggerContext().getConfiguration();
    for (LoggerConfig loggerConfig : configuration.getLoggers().values()) {
        result.add(convertLoggerConfiguration(loggerConfig));
    }
    Collections.sort(result, CONFIGURATION_COMPARATOR);
    return result;
}
Also used : LoggerConfiguration(org.springframework.boot.logging.LoggerConfiguration) Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerConfiguration(org.springframework.boot.logging.LoggerConfiguration) ArrayList(java.util.ArrayList) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 9 with LoggerConfig

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

the class Log4jUtils method appendToLogFile.

public static void appendToLogFile(String logFile) {
    LoggerContext coreContext = (LoggerContext) LogManager.getContext(false);
    Configuration configuration = coreContext.getConfiguration();
    final Layout<? extends Serializable> layout = PatternLayout.newBuilder().withConfiguration(configuration).withPattern("%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n").build();
    final Appender appender = FileAppender.createAppender(logFile, "false", "false", "File", "true", "false", null, null, layout, null, "false", null, configuration);
    appender.start();
    for (LoggerConfig loggerConfig : configuration.getLoggers().values()) {
        configuration.addLoggerAppender(coreContext.getLogger(loggerConfig.getName()), appender);
    }
}
Also used : AbstractAppender(org.apache.logging.log4j.core.appender.AbstractAppender) Appender(org.apache.logging.log4j.core.Appender) FileAppender(org.apache.logging.log4j.core.appender.FileAppender) Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 10 with LoggerConfig

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

the class Log4j2StringAppender method removeFromLogger.

public void removeFromLogger(String loggerName) {
    LoggerConfig loggerConfig = LOGGER_CONTEXT_CONFIGURATION.getLoggerConfig(loggerName);
    loggerConfig.removeAppender(getName());
    LOGGER_CONTEXT.updateLoggers();
}
Also used : 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