Search in sources :

Example 21 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class FlumeAppenderTest method testStructured.

@Test
public void testStructured() throws IOException {
    final Agent[] agents = new Agent[] { Agent.createAgent("localhost", testPort) };
    final FlumeAppender avroAppender = FlumeAppender.createAppender(agents, null, null, "false", "Avro", null, "1000", "1000", "1", "1000", "avro", "false", null, null, null, "ReqCtx_", null, "true", "1", null, null, null, null);
    avroAppender.start();
    final Logger eventLogger = (Logger) LogManager.getLogger("EventLogger");
    Assert.assertNotNull(eventLogger);
    eventLogger.addAppender(avroAppender);
    eventLogger.setLevel(Level.ALL);
    final StructuredDataMessage msg = new StructuredDataMessage("Transfer", "Success", "Audit");
    msg.put("memo", "This is a memo");
    msg.put("acct", "12345");
    msg.put("amount", "100.00");
    ThreadContext.put("id", UUID.randomUUID().toString());
    ThreadContext.put("memo", null);
    ThreadContext.put("test", "123");
    EventLogger.logEvent(msg);
    final Transaction transaction = channel.getTransaction();
    transaction.begin();
    final Event event = channel.take();
    Assert.assertNotNull(event);
    Assert.assertTrue("Channel contained event, but not expected message", getBody(event).endsWith("Success"));
    transaction.commit();
    transaction.close();
    eventSource.stop();
    eventLogger.removeAppender(avroAppender);
    avroAppender.stop();
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) Transaction(org.apache.flume.Transaction) Event(org.apache.flume.Event) Logger(org.apache.logging.log4j.core.Logger) EventLogger(org.apache.logging.log4j.EventLogger) StatusLogger(org.apache.logging.log4j.status.StatusLogger) Test(org.junit.Test)

Example 22 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class FlumeEmbeddedAgentTest method testMultiple.

@Test
public void testMultiple() throws IOException {
    for (int i = 0; i < 10; ++i) {
        final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Multiple " + i, "Test");
        EventLogger.logEvent(msg);
    }
    for (int i = 0; i < 10; ++i) {
        final Event event = primary.poll();
        Assert.assertNotNull(event);
        final String body = getBody(event);
        final String expected = "Test Multiple " + i;
        Assert.assertTrue("Channel contained event, but not expected message. Received: " + body, body.endsWith(expected));
    }
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) Event(org.apache.flume.Event) AvroFlumeEvent(org.apache.flume.source.avro.AvroFlumeEvent) Test(org.junit.Test)

Example 23 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class FlumeEmbeddedAgentTest method testFailover.

@Test
public void testFailover() throws InterruptedException, IOException {
    final Logger logger = LogManager.getLogger("testFailover");
    logger.debug("Starting testFailover");
    for (int i = 0; i < 10; ++i) {
        final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Primary " + i, "Test");
        EventLogger.logEvent(msg);
    }
    for (int i = 0; i < 10; ++i) {
        final Event event = primary.poll();
        Assert.assertNotNull(event);
        final String body = getBody(event);
        final String expected = "Test Primary " + i;
        Assert.assertTrue("Channel contained event, but not expected message. Received: " + body, body.endsWith(expected));
    }
    // Give the AvroSink time to receive notification and notify the channel.
    Thread.sleep(500);
    primary.stop();
    for (int i = 0; i < 10; ++i) {
        final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Alternate " + i, "Test");
        EventLogger.logEvent(msg);
    }
    for (int i = 0; i < 10; ++i) {
        final Event event = alternate.poll();
        Assert.assertNotNull(event);
        final String body = getBody(event);
        final String expected = "Test Alternate " + i;
        /* When running in Gump Flume consistently returns the last event from the primary channel after
               the failover, which fails this test */
        Assert.assertTrue("Channel contained event, but not expected message. Expected: " + expected + " Received: " + body, body.endsWith(expected));
    }
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) Event(org.apache.flume.Event) AvroFlumeEvent(org.apache.flume.source.avro.AvroFlumeEvent) EventLogger(org.apache.logging.log4j.EventLogger) Logger(org.apache.logging.log4j.Logger) StatusLogger(org.apache.logging.log4j.status.StatusLogger) Test(org.junit.Test)

Example 24 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class FlumePersistentAppenderTest method testFailover.

@Test
public void testFailover() throws InterruptedException {
    final Logger logger = LogManager.getLogger("testFailover");
    logger.debug("Starting testFailover");
    for (int i = 0; i < 10; ++i) {
        final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Primary " + i, "Test");
        msg.put("counter", Integer.toString(i));
        EventLogger.logEvent(msg);
    }
    boolean[] fields = new boolean[10];
    for (int i = 0; i < 10; ++i) {
        final Event event = primary.poll();
        Assert.assertNotNull("Received " + i + " events. Event " + (i + 1) + " is null", event);
        final String value = event.getHeaders().get("counter");
        Assert.assertNotNull("Missing counter", value);
        final int counter = Integer.parseInt(value);
        if (fields[counter]) {
            Assert.fail("Duplicate event");
        } else {
            fields[counter] = true;
        }
    }
    for (int i = 0; i < 10; ++i) {
        Assert.assertTrue("Channel contained event, but not expected message " + i, fields[i]);
    }
    // Give the AvroSink time to receive notification and notify the channel.
    Thread.sleep(500);
    primary.stop();
    for (int i = 0; i < 10; ++i) {
        final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Alternate " + i, "Test");
        msg.put("cntr", Integer.toString(i));
        EventLogger.logEvent(msg);
    }
    fields = new boolean[10];
    for (int i = 0; i < 10; ++i) {
        final Event event = alternate.poll();
        Assert.assertNotNull("Received " + i + " events. Event " + (i + 1) + " is null", event);
        final String value = event.getHeaders().get("cntr");
        Assert.assertNotNull("Missing counter", value);
        final int counter = Integer.parseInt(value);
        if (fields[counter]) {
            Assert.fail("Duplicate event");
        } else {
            fields[counter] = true;
        }
    }
    for (int i = 0; i < 10; ++i) {
        Assert.assertTrue("Channel contained event, but not expected message " + i, fields[i]);
    }
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) Event(org.apache.flume.Event) AvroFlumeEvent(org.apache.flume.source.avro.AvroFlumeEvent) EventLogger(org.apache.logging.log4j.EventLogger) Logger(org.apache.logging.log4j.Logger) StatusLogger(org.apache.logging.log4j.status.StatusLogger) Test(org.junit.Test)

Example 25 with StructuredDataMessage

use of org.apache.logging.log4j.message.StructuredDataMessage in project logging-log4j2 by apache.

the class FlumePersistentAppenderTest method testLog4Event.

@Test
public void testLog4Event() throws IOException {
    final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Log4j", "Test");
    EventLogger.logEvent(msg);
    final Event event = primary.poll();
    Assert.assertNotNull(event);
    final String body = getBody(event);
    Assert.assertTrue("Channel contained event, but not expected message. Received: " + body, body.endsWith("Test Log4j"));
}
Also used : StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) Event(org.apache.flume.Event) AvroFlumeEvent(org.apache.flume.source.avro.AvroFlumeEvent) Test(org.junit.Test)

Aggregations

StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)40 Test (org.junit.Test)32 Event (org.apache.flume.Event)12 AvroFlumeEvent (org.apache.flume.source.avro.AvroFlumeEvent)11 LogEvent (org.apache.logging.log4j.core.LogEvent)11 File (java.io.File)6 Message (org.apache.logging.log4j.message.Message)6 EventLogger (org.apache.logging.log4j.EventLogger)5 Logger (org.apache.logging.log4j.Logger)4 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)4 StatusLogger (org.apache.logging.log4j.status.StatusLogger)4 Appender (org.apache.logging.log4j.core.Appender)3 ListAppender (org.apache.logging.log4j.test.appender.ListAppender)3 Map (java.util.Map)2 Marker (org.apache.logging.log4j.Marker)2 KeyValuePair (org.apache.logging.log4j.core.util.KeyValuePair)2 MapMessage (org.apache.logging.log4j.message.MapMessage)2 HashMap (java.util.HashMap)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1