Search in sources :

Example 71 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 72 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 73 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 74 with Appender

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;
}
Also used : Appender(org.apache.logging.log4j.core.Appender) PluginNode(org.apache.logging.log4j.core.config.plugins.PluginNode) Node(org.apache.logging.log4j.core.config.Node)

Example 75 with Appender

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")));
}
Also used : Appender(org.apache.logging.log4j.core.Appender) Filter(org.apache.logging.log4j.core.Filter) ThreadContextMapFilter(org.apache.logging.log4j.core.filter.ThreadContextMapFilter) Logger(org.apache.logging.log4j.Logger) Test(org.junit.Test)

Aggregations

Appender (org.apache.logging.log4j.core.Appender)98 Test (org.junit.Test)50 ConsoleAppender (org.apache.logging.log4j.core.appender.ConsoleAppender)25 Configuration (org.apache.logging.log4j.core.config.Configuration)21 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)18 LoggerContext (org.apache.logging.log4j.core.LoggerContext)15 FileAppender (org.apache.logging.log4j.core.appender.FileAppender)13 Logger (org.apache.logging.log4j.Logger)12 BuiltConfiguration (org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration)12 LoggerConfig (org.apache.logging.log4j.core.config.LoggerConfig)11 AbstractAppender (org.apache.logging.log4j.core.appender.AbstractAppender)9 RollingFileAppender (org.apache.logging.log4j.core.appender.RollingFileAppender)9 Map (java.util.Map)8 Filter (org.apache.logging.log4j.core.Filter)8 AppenderControl (org.apache.logging.log4j.core.config.AppenderControl)8 URL (java.net.URL)7 IOException (java.io.IOException)5 LogEvent (org.apache.logging.log4j.core.LogEvent)5 Logger (org.apache.logging.log4j.core.Logger)5 AsyncAppender (org.apache.logging.log4j.core.appender.AsyncAppender)5