Search in sources :

Example 61 with Configuration

use of org.apache.logging.log4j.core.config.Configuration 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)

Example 62 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project camel by apache.

the class ConsumingAppender method newAppender.

public static Appender newAppender(String loggerName, String appenderName, String patter, Level level, Consumer<LogEvent> consumer) {
    final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    final Configuration config = ctx.getConfiguration();
    config.removeLogger(loggerName);
    ConsumingAppender appender = new ConsumingAppender(appenderName, patter, consumer);
    appender.start();
    LoggerConfig loggerConfig = LoggerConfig.createLogger(true, level, loggerName, "true", new AppenderRef[] { AppenderRef.createAppenderRef(appenderName, null, null) }, null, config, null);
    loggerConfig.addAppender(appender, null, null);
    config.addLogger(loggerName, loggerConfig);
    ctx.updateLoggers();
    return appender;
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) LoggerConfig(org.apache.logging.log4j.core.config.LoggerConfig)

Example 63 with Configuration

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

the class Log4J2LoggingSystemTests method withFile.

@Test
public void withFile() throws Exception {
    this.loggingSystem.beforeInitialize();
    this.logger.info("Hidden");
    this.loggingSystem.initialize(null, null, getLogFile(null, tmpDir()));
    this.logger.info("Hello world");
    String output = this.output.toString().trim();
    Configuration configuration = this.loggingSystem.getConfiguration();
    assertThat(output).contains("Hello world").doesNotContain("Hidden");
    assertThat(new File(tmpDir() + "/spring.log").exists()).isTrue();
    assertThat(configuration.getConfigurationSource().getFile()).isNotNull();
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerConfiguration(org.springframework.boot.logging.LoggerConfiguration) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 64 with Configuration

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

the class Log4J2LoggingSystemTests method testNonDefaultConfigLocation.

@Test
public void testNonDefaultConfigLocation() throws Exception {
    this.loggingSystem.beforeInitialize();
    this.loggingSystem.initialize(null, "classpath:log4j2-nondefault.xml", getLogFile(tmpDir() + "/tmp.log", null));
    this.logger.info("Hello world");
    String output = this.output.toString().trim();
    Configuration configuration = this.loggingSystem.getConfiguration();
    assertThat(output).contains("Hello world").contains(tmpDir() + "/tmp.log");
    assertThat(new File(tmpDir() + "/tmp.log").exists()).isFalse();
    assertThat(configuration.getConfigurationSource().getFile().getAbsolutePath()).contains("log4j2-nondefault.xml");
    assertThat(configuration.getWatchManager().getIntervalSeconds()).isEqualTo(30);
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerConfiguration(org.springframework.boot.logging.LoggerConfiguration) Matchers.containsString(org.hamcrest.Matchers.containsString) File(java.io.File) Test(org.junit.Test)

Example 65 with Configuration

use of org.apache.logging.log4j.core.config.Configuration in project graylog2-server by Graylog2.

the class LoggersResource method getLoggerConfigs.

@VisibleForTesting
protected Collection<LoggerConfig> getLoggerConfigs() {
    final LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);
    final Configuration configuration = loggerContext.getConfiguration();
    return configuration.getLoggers().values();
}
Also used : Configuration(org.apache.logging.log4j.core.config.Configuration) LoggerContext(org.apache.logging.log4j.core.LoggerContext) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

Configuration (org.apache.logging.log4j.core.config.Configuration)105 LoggerContext (org.apache.logging.log4j.core.LoggerContext)54 Test (org.junit.Test)43 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)39 Appender (org.apache.logging.log4j.core.Appender)21 DefaultConfiguration (org.apache.logging.log4j.core.config.DefaultConfiguration)15 File (java.io.File)12 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)9 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)9 Path (java.nio.file.Path)7 Level (org.apache.logging.log4j.Level)7 NullConfiguration (org.apache.logging.log4j.core.config.NullConfiguration)7 ArrayList (java.util.ArrayList)6 Logger (org.apache.logging.log4j.Logger)6 Filter (org.apache.logging.log4j.core.Filter)6 AbstractLogger (org.apache.logging.log4j.spi.AbstractLogger)6 PatternLayout (org.apache.logging.log4j.core.layout.PatternLayout)5 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)5 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)4 AppenderRef (org.apache.logging.log4j.core.config.AppenderRef)4