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));
}
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());
}
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);
}
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}"));
}
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());
}
Aggregations