Search in sources :

Example 66 with Appender

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

the class KafkaAppenderTest method testAsyncAppend.

@Test
public void testAsyncAppend() throws Exception {
    final Appender appender = ctx.getRequiredAppender("AsyncKafkaAppender");
    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 67 with Appender

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

the class KafkaAppenderTest method testAppendWithSerializedLayout.

@Test
public void testAppendWithSerializedLayout() throws Exception {
    final Appender appender = ctx.getRequiredAppender("KafkaAppenderWithSerializedLayout");
    final LogEvent logEvent = createLogEvent();
    appender.append(logEvent);
    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, deserializeLogEvent(item.value()).getMessage().getFormattedMessage());
}
Also used : Appender(org.apache.logging.log4j.core.Appender) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) Test(org.junit.Test)

Example 68 with Appender

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

the class WriterAppenderTest method addAppender.

private void addAppender(final Writer writer, final String writerName) {
    final LoggerContext context = LoggerContext.getContext(false);
    final Configuration config = context.getConfiguration();
    final PatternLayout layout = PatternLayout.createDefaultLayout(config);
    final Appender appender = WriterAppender.createAppender(layout, null, writer, writerName, false, true);
    appender.start();
    config.addAppender(appender);
    ConfigurationTestUtils.updateLoggers(appender, config);
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Configuration(org.apache.logging.log4j.core.config.Configuration) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 69 with Appender

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

the class TestConfigurator method testEnvironment.

@Test
public void testEnvironment() throws Exception {
    ctx = Configurator.initialize("-config", null);
    LogManager.getLogger("org.apache.test.TestConfigurator");
    final Configuration config = ctx.getConfiguration();
    assertNotNull("No configuration", config);
    assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
    final Map<String, Appender> map = config.getAppenders();
    assertNotNull("Appenders map should not be null.", map);
    assertThat(map, hasSize(greaterThan(0)));
    assertThat("No ListAppender named List2", map, hasKey("List2"));
    final Appender app = map.get("List2");
    final Layout<? extends Serializable> layout = app.getLayout();
    assertNotNull("Appender List2 does not have a Layout", layout);
    assertThat("Appender List2 is not configured with a PatternLayout", layout, instanceOf(PatternLayout.class));
    final String pattern = ((PatternLayout) layout).getConversionPattern();
    assertNotNull("No conversion pattern for List2 PatternLayout", pattern);
    assertFalse("Environment variable was not substituted", pattern.startsWith("${env:PATH}"));
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) PatternLayout(org.apache.logging.log4j.core.layout.PatternLayout) Test(org.junit.Test)

Example 70 with Appender

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

the class TestConfigurator method testInitialize_NullClassLoader_ConfigurationSourceWithInputStream_NoId.

@Test
public void testInitialize_NullClassLoader_ConfigurationSourceWithInputStream_NoId() throws Exception {
    final InputStream is = new FileInputStream("target/test-classes/log4j2-config.xml");
    final ConfigurationSource source = new ConfigurationSource(is);
    ctx = Configurator.initialize(null, source);
    LogManager.getLogger("org.apache.test.TestConfigurator");
    Configuration config = ctx.getConfiguration();
    assertNotNull("No configuration", config);
    assertEquals("Incorrect Configuration.", CONFIG_NAME, config.getName());
    final Map<String, Appender> map = config.getAppenders();
    assertNotNull("Appenders map should not be null.", map);
    assertThat(map, hasSize(greaterThan(0)));
    assertThat("Wrong configuration", map, hasKey("List"));
    Configurator.shutdown(ctx);
    config = ctx.getConfiguration();
    assertEquals("Unexpected Configuration.", NullConfiguration.NULL_NAME, config.getName());
}
Also used : Appender(org.apache.logging.log4j.core.Appender) ConsoleAppender(org.apache.logging.log4j.core.appender.ConsoleAppender) BuiltConfiguration(org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileInputStream(java.io.FileInputStream) 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