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 AppenderSet method createAppender.
public Appender createAppender(final String appenderName, final String actualName) {
final Node node = nodeMap.get(appenderName);
if (node == null) {
LOGGER.error("No node named {} in {}", appenderName, this);
return null;
}
node.getAttributes().put("name", actualName);
if (node.getType().getElementName().equals(Appender.ELEMENT_TYPE)) {
final Node appNode = new Node(node);
configuration.createConfiguration(appNode, null);
if (appNode.getObject() instanceof Appender) {
final Appender app = appNode.getObject();
app.start();
return app;
}
LOGGER.error("Unable to create Appender of type " + node.getName());
return null;
}
LOGGER.error("No Appender was configured for name {} " + appenderName);
return null;
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class ConfigurationTest method testLogger.
@Test
public void testLogger() throws Exception {
final Logger logger = this.ctx.getLogger(LOGGER_NAME);
assertThat(logger, is(instanceOf(org.apache.logging.log4j.core.Logger.class)));
final org.apache.logging.log4j.core.Logger l = (org.apache.logging.log4j.core.Logger) logger;
assertThat(l.getLevel(), is(equalTo(Level.DEBUG)));
assertThat(l.filterCount(), is(equalTo(1)));
final Iterator<Filter> iterator = l.getFilters();
assertThat(iterator.hasNext(), is(true));
final Filter filter = iterator.next();
assertThat(filter, is(instanceOf(ThreadContextMapFilter.class)));
final Map<String, Appender> appenders = l.getAppenders();
assertThat(appenders, is(notNullValue()));
assertThat(appenders.size(), is(equalTo(1)));
final Appender appender = appenders.get(APPENDER_NAME);
assertThat(appender, is(notNullValue()));
assertThat(appender.getName(), is(equalTo("STDOUT")));
}
Aggregations