use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class KafkaAppenderTest method testAppend.
@Test
public void testAppend() throws Exception {
final Appender appender = ctx.getRequiredAppender("KafkaAppender");
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 WebLookupTest method testLookup2.
@Test
public void testLookup2() throws Exception {
ContextAnchor.THREAD_CONTEXT.remove();
final ServletContext servletContext = new MockServletContext();
servletContext.setAttribute("TestAttr", "AttrValue");
servletContext.setInitParameter("myapp.logdir", "target");
servletContext.setAttribute("Name1", "Ben");
servletContext.setInitParameter("Name2", "Jerry");
servletContext.setInitParameter("log4jConfiguration", "WEB-INF/classes/log4j-webvar.xml");
final Log4jWebLifeCycle initializer = WebLoggerContextUtils.getWebLifeCycle(servletContext);
initializer.start();
initializer.setLoggerContext();
final LoggerContext ctx = ContextAnchor.THREAD_CONTEXT.get();
assertNotNull("No LoggerContext", ctx);
assertNotNull("No ServletContext", ctx.getExternalContext());
final Configuration config = ctx.getConfiguration();
assertNotNull("No Configuration", config);
final Map<String, Appender> appenders = config.getAppenders();
for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
if (entry.getKey().equals("file")) {
final FileAppender fa = (FileAppender) entry.getValue();
assertEquals("target/myapp.log", fa.getFileName());
}
}
initializer.stop();
ContextAnchor.THREAD_CONTEXT.remove();
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class AsyncAppender method start.
@Override
public void start() {
final Map<String, Appender> map = config.getAppenders();
final List<AppenderControl> appenders = new ArrayList<>();
for (final AppenderRef appenderRef : appenderRefs) {
final Appender appender = map.get(appenderRef.getRef());
if (appender != null) {
appenders.add(new AppenderControl(appender, appenderRef.getLevel(), appenderRef.getFilter()));
} else {
LOGGER.error("No appender named {} was configured", appenderRef);
}
}
if (errorRef != null) {
final Appender appender = map.get(errorRef);
if (appender != null) {
errorAppender = new AppenderControl(appender, null, null);
} else {
LOGGER.error("Unable to set up error Appender. No appender named {} was configured", errorRef);
}
}
if (appenders.size() > 0) {
thread = new AsyncThread(appenders, queue);
thread.setName("AsyncAppender-" + getName());
} else if (errorRef == null) {
throw new ConfigurationException("No appenders are available for AsyncAppender " + getName());
}
asyncQueueFullPolicy = AsyncQueueFullPolicyFactory.create();
thread.start();
super.start();
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class FailoverAppender method start.
@Override
public void start() {
final Map<String, Appender> map = config.getAppenders();
int errors = 0;
final Appender appender = map.get(primaryRef);
if (appender != null) {
primary = new AppenderControl(appender, null, null);
} else {
LOGGER.error("Unable to locate primary Appender " + primaryRef);
++errors;
}
for (final String name : failovers) {
final Appender foAppender = map.get(name);
if (foAppender != null) {
failoverAppenders.add(new AppenderControl(foAppender, null, null));
} else {
LOGGER.error("Failover appender " + name + " is not configured");
}
}
if (failoverAppenders.isEmpty()) {
LOGGER.error("No failover appenders are available");
++errors;
}
if (errors == 0) {
super.start();
}
}
use of org.apache.logging.log4j.core.Appender in project logging-log4j2 by apache.
the class RewriteAppender method start.
@Override
public void start() {
for (final AppenderRef ref : appenderRefs) {
final String name = ref.getRef();
final Appender appender = config.getAppender(name);
if (appender != null) {
final Filter filter = appender instanceof AbstractAppender ? ((AbstractAppender) appender).getFilter() : null;
appenders.put(name, new AppenderControl(appender, ref.getLevel(), filter));
} else {
LOGGER.error("Appender " + ref + " cannot be located. Reference ignored");
}
}
super.start();
}
Aggregations