Search in sources :

Example 61 with Appender

use of org.apache.logging.log4j.core.Appender 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 62 with Appender

use of org.apache.logging.log4j.core.Appender 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 63 with Appender

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

the class EvilLoggerTests method testFindAppender.

public void testFindAppender() throws IOException, UserException {
    setupLogging("find_appender");
    final Logger hasConsoleAppender = ESLoggerFactory.getLogger("has_console_appender");
    final Appender testLoggerConsoleAppender = Loggers.findAppender(hasConsoleAppender, ConsoleAppender.class);
    assertNotNull(testLoggerConsoleAppender);
    assertThat(testLoggerConsoleAppender.getName(), equalTo("console"));
    final Logger hasCountingNoOpAppender = ESLoggerFactory.getLogger("has_counting_no_op_appender");
    assertNull(Loggers.findAppender(hasCountingNoOpAppender, ConsoleAppender.class));
    final Appender countingNoOpAppender = Loggers.findAppender(hasCountingNoOpAppender, CountingNoOpAppender.class);
    assertThat(countingNoOpAppender.getName(), equalTo("counting_no_op"));
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) CountingNoOpAppender(org.apache.logging.log4j.core.appender.CountingNoOpAppender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) Logger(org.apache.logging.log4j.Logger)

Example 64 with Appender

use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.

the class KafkaAppenderTest method testAppendWithLayout.

@Test
public void testAppendWithLayout() throws Exception {
    final Appender appender = ctx.getRequiredAppender("KafkaAppenderWithLayout");
    appender.append(createLogEvent());
    final List<ProducerRecord<byte[], byte[]>> history = kafka.history();
    assertEquals(1, history.size());
    final ProducerRecord<byte[], byte[]> item = history.get(0);
    assertNotNull(item);
    assertEquals(TOPIC_NAME, item.topic());
    assertNull(item.key());
    assertEquals("[" + LOG_MESSAGE + "]", new String(item.value(), StandardCharsets.UTF_8));
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Test(org.junit.Test)

Example 65 with Appender

use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.

the class KafkaAppenderTest method testClose.

@Test(timeout = 2000)
public void testClose() throws Exception {
    final Appender appender = ctx.getRequiredAppender("KafkaAppender");
    appender.stop();
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Test(org.junit.Test)

Aggregations

Appender (org.apache.logging.log4j.core.Appender)105 Test (org.junit.Test)52 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)25 Configuration (org.apache.logging.log4j.core.config.Configuration)23 LoggerContext (org.apache.logging.log4j.core.LoggerContext)18 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)18 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)15 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)14 Logger (org.apache.logging.log4j.Logger)13 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)12 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)10 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)10 AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)10 Map (java.util.Map)8 Filter (org.apache.logging.log4j.core.Filter)8 URL (java.net.URL)7 IOException (java.io.IOException)6 LogEvent (org.apache.logging.log4j.core.LogEvent)6 File (java.io.File)5 Logger (org.apache.logging.log4j.core.Logger)5