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