Java Code Examples for org.apache.logging.log4j.message.StructuredDataMessage#put()

The following examples show how to use org.apache.logging.log4j.message.StructuredDataMessage#put() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EventDataConverter.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
public Message convertEvent(final String message, final Object[] objects, final Throwable throwable) {
    try {
        final EventData data = objects != null && objects[0] instanceof EventData ?
                (EventData) objects[0] : new EventData(message);
        final StructuredDataMessage msg =
                new StructuredDataMessage(data.getEventId(), data.getMessage(), data.getEventType());
        for (final Map.Entry<String, Object> entry : data.getEventMap().entrySet()) {
            final String key = entry.getKey();
            if (EventData.EVENT_TYPE.equals(key) || EventData.EVENT_ID.equals(key)
                    || EventData.EVENT_MESSAGE.equals(key)) {
                continue;
            }
            msg.put(key, String.valueOf(entry.getValue()));
        }
        return msg;
    } catch (final Exception ex) {
        return new ParameterizedMessage(message, objects, throwable);
    }
}
 
Example 2
Source File: FlumePersistentAppenderTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiple() {

    for (int i = 0; i < 10; ++i) {
        final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Multiple " + i, "Test");
        msg.put("counter", Integer.toString(i));
        EventLogger.logEvent(msg);
    }
    final 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' in map " + event.getHeaders() + ", i = " + i, 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]);
    }
}
 
Example 3
Source File: MapMessageLookupTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testStructuredDataMessageLookup() {
    // GIVEN: A StructuredDataMessage object
    final StructuredDataMessage message = new StructuredDataMessage("id", "msg", "type");

    message.put("A", "a");
    message.put("B", "b");
    message.put("C", "c");

    // AND: An event with that message
    final LogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.DEBUG).setMessage(message).build();

    // AND: A MapMessageLookup object
    final MapMessageLookup lookup = new MapMessageLookup();

    // WHEN: Lookup is performed
    final String a = lookup.lookup(event, "A");
    final String b = lookup.lookup(event, "B");
    final String c = lookup.lookup(event, "C");

    // THEN: The looked up values are correct
    assertEquals("a", a);
    assertEquals("b", b);
    assertEquals("c", c);
}
 
Example 4
Source File: RewriteAppenderTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void rewriteTest() {
    final StructuredDataMessage msg = new StructuredDataMessage("Test", "This is a test", "Service");
    msg.put("Key1", "Value1");
    msg.put("Key2", "Value2");
    EventLogger.logEvent(msg);
    final List<LogEvent> list = app.getEvents();
    assertNotNull("No events generated", list);
    assertTrue("Incorrect number of events. Expected 1, got " + list.size(), list.size() == 1);
    final LogEvent event = list.get(0);
    final Message m = event.getMessage();
    assertTrue("Message is not a StringMapMessage: " + m.getClass(), m instanceof StructuredDataMessage);
    final StructuredDataMessage message = (StructuredDataMessage) m;
    final Map<String, String> map = message.getData();
    assertNotNull("No Map", map);
    assertTrue("Incorrect number of map entries, expected 3 got " + map.size(), map.size() == 3);
    final String value = map.get("Key1");
    assertEquals("Apache", value);
}
 
Example 5
Source File: EventLoggerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void structuredData() {
    ThreadContext.put("loginId", "JohnDoe");
    ThreadContext.put("ipAddress", "192.168.0.120");
    ThreadContext.put("locale", Locale.US.getDisplayName());
    final StructuredDataMessage msg = new StructuredDataMessage("Transfer@18060", "Transfer Complete", "Audit");
    msg.put("ToAccount", "123456");
    msg.put("FromAccount", "123457");
    msg.put("Amount", "200.00");
    EventLogger.logEvent(msg);
    ThreadContext.clearMap();
    assertEquals(1, results.size());
    final String expected = "EVENT OFF Audit [Transfer@18060 Amount=\"200.00\" FromAccount=\"123457\" ToAccount=\"123456\"] Transfer Complete";
    assertThat("Incorrect structured data", results.get(0), startsWith(expected));
}
 
Example 6
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void structuredData() {
    ThreadContext.put("loginId", "JohnDoe");
    ThreadContext.put("ipAddress", "192.168.0.120");
    ThreadContext.put("locale", Locale.US.getDisplayName());
    final StructuredDataMessage msg = new StructuredDataMessage("Audit@18060", "Transfer Complete", "Transfer");
    msg.put("ToAccount", "123456");
    msg.put("FromAccount", "123457");
    msg.put("Amount", "200.00");
    logger.info(MarkerManager.getMarker("EVENT"), msg);
    ThreadContext.clearMap();
    assertEquals(1, results.size());
    assertThat("Incorrect structured data: ", results.get(0), startsWith(
            "EVENT INFO Transfer [Audit@18060 Amount=\"200.00\" FromAccount=\"123457\" ToAccount=\"123456\"] Transfer Complete"));
}
 
Example 7
Source File: FlumeAppenderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 8
Source File: FlumePersistentPerf.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testPerformance() throws Exception {
    final long start = System.currentTimeMillis();
    final int count = 10000;
    for (int i = 0; i < count; ++i) {
        final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Primary " + i, "Test");
        msg.put("counter", Integer.toString(i));
        EventLogger.logEvent(msg);
    }
    final long elapsed = System.currentTimeMillis() - start;
    System.out.println("Time to log " + count + " events " + elapsed + "ms");
}
 
Example 9
Source File: FlumeEmbeddedAppenderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore
public void testPerformance() throws Exception {
    final long start = System.currentTimeMillis();
    final int count = 10000;
    for (int i = 0; i < count; ++i) {
        final StructuredDataMessage msg = new StructuredDataMessage("Test", "Test Primary " + i, "Test");
        msg.put("counter", Integer.toString(i));
        EventLogger.logEvent(msg);
    }
    final long elapsed = System.currentTimeMillis() - start;
    System.out.println("Time to log " + count + " events " + elapsed + "ms");
}
 
Example 10
Source File: FlumePersistentAppenderTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    for (int i = start; i < stop; ++i) {
        final StructuredDataMessage msg = new StructuredDataMessage(
            "Test", "Test Multiple " + i, "Test");
        msg.put("counter", Integer.toString(i));
        EventLogger.logEvent(msg);
    }
}
 
Example 11
Source File: StrictXmlConfigTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void structuredData() {
    ThreadContext.put("loginId", "JohnDoe");
    ThreadContext.put("ipAddress", "192.168.0.120");
    ThreadContext.put("locale", Locale.US.getDisplayName());
    final StructuredDataMessage msg = new StructuredDataMessage("Audit@18060", "Transfer Complete", "Transfer");
    msg.put("ToAccount", "123456");
    msg.put("FromAccount", "123457");
    msg.put("Amount", "200.00");
    logger.info(MarkerManager.getMarker("EVENT"), msg);
    ThreadContext.clearMap();
    final List<LogEvent> events = app.getEvents();
    assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
 
Example 12
Source File: SyslogAppenderTestBase.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
protected void sendInfoStructuredMessage() {
    ThreadContext.put("loginId", "JohnDoe");
    ThreadContext.put("ipAddress", "192.168.0.120");
    ThreadContext.put("locale", Locale.US.getDisplayName());
    final StructuredDataMessage msg = new StructuredDataMessage("Transfer@18060", "Transfer Complete", "Audit");
    msg.put("ToAccount", "123456");
    msg.put("FromAccount", "123457");
    msg.put("Amount", "200.00");
    // the msg.toString() doesn't contain the parameters of the ThreadContext, so we must use the line1 string
    final String str = msg.asString(null, null);
    sentMessages.add(str);
    root.info(MarkerManager.getMarker("EVENT"), msg);
}
 
Example 13
Source File: LoggerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void structuredData() {
    ThreadContext.put("loginId", "JohnDoe");
    ThreadContext.put("ipAddress", "192.168.0.120");
    ThreadContext.put("locale", Locale.US.getDisplayName());
    final StructuredDataMessage msg = new StructuredDataMessage("Audit@18060", "Transfer Complete", "Transfer");
    msg.put("ToAccount", "123456");
    msg.put("FromAccount", "123457");
    msg.put("Amount", "200.00");
    logger.info(MarkerManager.getMarker("EVENT"), msg);
    ThreadContext.clearMap();
    final List<LogEvent> events = app.getEvents();
    assertEventCount(events, 1);
}