org.apache.logging.log4j.message.StructuredDataMessage Java Examples
The following examples show how to use
org.apache.logging.log4j.message.StructuredDataMessage.
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: MapMessageLookupTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@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 #2
Source File: FlumeEmbeddedAgentTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@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)); } }
Example #3
Source File: Rfc5424Layout.java From logging-log4j2 with Apache License 2.0 | 6 votes |
private void appendMessage(final StringBuilder buffer, final LogEvent event) { final Message message = event.getMessage(); // This layout formats StructuredDataMessages instead of delegating to the Message itself. final String text = (message instanceof StructuredDataMessage || message instanceof MessageCollectionMessage) ? message.getFormat() : message.getFormattedMessage(); if (text != null && text.length() > 0) { buffer.append(' ').append(escapeNewlines(text, escapeNewLine)); } if (exceptionFormatters != null && event.getThrown() != null) { final StringBuilder exception = new StringBuilder(LF); for (final PatternFormatter formatter : exceptionFormatters) { formatter.format(event, exception); } buffer.append(escapeNewlines(exception.toString(), escapeNewLine)); } if (includeNewLine) { buffer.append(LF); } }
Example #4
Source File: StructuredDataFilter.java From logging-log4j2 with Apache License 2.0 | 6 votes |
private StringBuilder getValue(final StructuredDataMessage data, final String key) { final StringBuilder sb = getStringBuilder(); if (key.equalsIgnoreCase("id")) { data.getId().formatTo(sb); return sb; } else if (key.equalsIgnoreCase("id.name")) { return appendOrNull(data.getId().getName(), sb); } else if (key.equalsIgnoreCase("type")) { return appendOrNull(data.getType(), sb); } else if (key.equalsIgnoreCase("message")) { data.formatTo(sb); return sb; } else { return appendOrNull(data.get(key), sb); } }
Example #5
Source File: EventDataConverter.java From logging-log4j2 with Apache License 2.0 | 6 votes |
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 #6
Source File: FlumeEmbeddedAppenderTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@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 should not be null", 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)); } }
Example #7
Source File: StructuredDataFilter.java From logging-log4j2 with Apache License 2.0 | 6 votes |
protected Result filter(final StructuredDataMessage message) { boolean match = false; final IndexedReadOnlyStringMap map = getStringMap(); for (int i = 0; i < map.size(); i++) { final StringBuilder toMatch = getValue(message, map.getKeyAt(i)); if (toMatch != null) { match = listContainsValue((List<String>) map.getValueAt(i), toMatch); } else { match = false; } if ((!isAnd() && match) || (isAnd() && !match)) { break; } } return match ? onMatch : onMismatch; }
Example #8
Source File: FlumePersistentAppenderTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@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 #9
Source File: RewriteAppenderTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@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 #10
Source File: MapRewritePolicyTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void addTest() { final MapRewritePolicy addPolicy = MapRewritePolicy.createPolicy("Add", rewrite); LogEvent rewritten = addPolicy.rewrite(logEvent0); compareLogEvents(logEvent0, rewritten); assertEquals("Simple log message changed", logEvent0.getMessage(), rewritten.getMessage()); rewritten = addPolicy.rewrite(logEvent1); compareLogEvents(logEvent1, rewritten); checkAdded(((StringMapMessage)rewritten.getMessage()).getData()); rewritten = addPolicy.rewrite(logEvent2); compareLogEvents(logEvent2, rewritten); checkAdded(((StructuredDataMessage)rewritten.getMessage()).getData()); rewritten = addPolicy.rewrite(logEvent3); compareLogEvents(logEvent3, rewritten); checkAdded(((StringMapMessage)rewritten.getMessage()).getData()); }
Example #11
Source File: MapRewritePolicyTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void updateTest() { final MapRewritePolicy updatePolicy = MapRewritePolicy.createPolicy("Update", rewrite); LogEvent rewritten = updatePolicy.rewrite(logEvent0); compareLogEvents(logEvent0, rewritten); assertEquals("Simple log message changed", logEvent0.getMessage(), rewritten.getMessage()); rewritten = updatePolicy.rewrite(logEvent1); compareLogEvents(logEvent1, rewritten); checkUpdated(((StringMapMessage)rewritten.getMessage()).getData()); rewritten = updatePolicy.rewrite(logEvent2); compareLogEvents(logEvent2, rewritten); checkUpdated(((StructuredDataMessage)rewritten.getMessage()).getData()); rewritten = updatePolicy.rewrite(logEvent3); compareLogEvents(logEvent3, rewritten); checkUpdated(((StringMapMessage)rewritten.getMessage()).getData()); }
Example #12
Source File: MapRewritePolicyTest.java From logging-log4j2 with Apache License 2.0 | 6 votes |
@Test public void defaultIsAdd() { final MapRewritePolicy addPolicy = MapRewritePolicy.createPolicy(null, rewrite); LogEvent rewritten = addPolicy.rewrite(logEvent0); compareLogEvents(logEvent0, rewritten); assertEquals("Simple log message changed", logEvent0.getMessage(), rewritten.getMessage()); rewritten = addPolicy.rewrite(logEvent1); compareLogEvents(logEvent1, rewritten); checkAdded(((StringMapMessage)rewritten.getMessage()).getData()); rewritten = addPolicy.rewrite(logEvent2); compareLogEvents(logEvent2, rewritten); checkAdded(((StructuredDataMessage)rewritten.getMessage()).getData()); rewritten = addPolicy.rewrite(logEvent3); compareLogEvents(logEvent3, rewritten); checkAdded(((StringMapMessage)rewritten.getMessage()).getData()); }
Example #13
Source File: StructuredDataFilter.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg, final Throwable t) { if (msg instanceof StructuredDataMessage) { return filter((StructuredDataMessage) msg); } return Result.NEUTRAL; }
Example #14
Source File: StructuredDataFilter.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Override public Result filter(final LogEvent event) { final Message msg = event.getMessage(); if (msg instanceof StructuredDataMessage) { return filter((StructuredDataMessage) msg); } return super.filter(event); }
Example #15
Source File: StructuredDataLookup.java From logging-log4j2 with Apache License 2.0 | 5 votes |
/** * Looks up the value for the key using the data in the LogEvent. * @param event The current LogEvent. * @param key the key to be looked up, may be null * @return The value associated with the key. */ @Override public String lookup(final LogEvent event, final String key) { if (event == null || !(event.getMessage() instanceof StructuredDataMessage)) { return null; } final StructuredDataMessage msg = (StructuredDataMessage) event.getMessage(); if (key.equalsIgnoreCase("id")) { return msg.getId().getName(); } else if (key.equalsIgnoreCase("type")) { return msg.getType(); } return msg.get(key); }
Example #16
Source File: Rfc5424Layout.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private void appendMessageId(final StringBuilder buffer, final Message message) { final boolean isStructured = message instanceof StructuredDataMessage; final String type = isStructured ? ((StructuredDataMessage) message).getType() : null; if (type != null) { buffer.append(type); } else if (messageId != null) { buffer.append(messageId); } else { buffer.append('-'); } }
Example #17
Source File: LogEventFactory.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@SuppressWarnings("unchecked") public static <T extends AuditEvent> T getEvent(final Class<T> intrface) { final String eventId = NamingUtils.lowerFirst(intrface.getSimpleName()); final StructuredDataMessage msg = new StructuredDataMessage(eventId, null, "Audit"); return (T)Proxy.newProxyInstance(intrface .getClassLoader(), new Class<?>[]{intrface}, new AuditProxy(msg, intrface)); }
Example #18
Source File: MarkerPatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testLookup() { final Message msg = new StructuredDataMessage("Test", "This is a test", "Audit"); final Marker eventMarker = MarkerManager.getMarker("EVENT"); final Marker auditMarker = MarkerManager.getMarker("AUDIT").setParents(eventMarker); final LogEvent event = Log4jLogEvent.newBuilder().setLoggerName("MyLogger").setMarker(auditMarker) .setLevel(Level.DEBUG).setMessage(msg).build(); final StringBuilder sb = new StringBuilder(); final MarkerPatternConverter converter = MarkerPatternConverter.newInstance(null); converter.format(event, sb); assertEquals(auditMarker.toString(), sb.toString()); }
Example #19
Source File: Rfc5424Layout.java From logging-log4j2 with Apache License 2.0 | 5 votes |
private void addStructuredData(final Map<String, StructuredDataElement> sdElements, final StructuredDataMessage data) { final Map<String, String> map = data.getData(); final StructuredDataId id = data.getId(); final String sdId = getId(id); if (sdElements.containsKey(sdId)) { final StructuredDataElement union = sdElements.get(id.toString()); union.union(map); sdElements.put(sdId, union); } else { final StructuredDataElement formattedData = new StructuredDataElement(map, eventPrefix, false); sdElements.put(sdId, formattedData); } }
Example #20
Source File: FlumePersistentAppenderTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@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 #21
Source File: MarkerSimpleNamePatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testLookup() { final Message msg = new StructuredDataMessage("Test", "This is a test", "Audit"); final Marker eventMarker = MarkerManager.getMarker("EVENT"); final Marker auditMarker = MarkerManager.getMarker("AUDIT").setParents(eventMarker); final LogEvent event = Log4jLogEvent.newBuilder().setLoggerName("MyLogger").setMarker(auditMarker) .setLevel(Level.DEBUG).setMessage(msg).build(); final StringBuilder sb = new StringBuilder(); final MarkerSimpleNamePatternConverter converter = MarkerSimpleNamePatternConverter.newInstance(null); converter.format(event, sb); assertEquals(auditMarker.getName(), sb.toString()); }
Example #22
Source File: MessagePatternConverterTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testStructuredDataFormatFull() throws Exception { final MessagePatternConverter converter = MessagePatternConverter.newInstance(null, new String[]{"FULL"}); Message msg = new StructuredDataMessage("id", "message", "type") .with("key", "val"); LogEvent event = Log4jLogEvent.newBuilder() // .setLoggerName("MyLogger") // .setLevel(Level.DEBUG) // .setMessage(msg).build(); StringBuilder sb = new StringBuilder(); converter.format(event, sb); assertEquals("Unexpected result", "type [id key=\"val\"] message", sb.toString()); }
Example #23
Source File: StrictXmlConfigTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@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 #24
Source File: StructuredDataLookupTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void testLookup() { final Message msg = new StructuredDataMessage("Test", "This is a test", "Audit"); final LogEvent event = Log4jLogEvent.newBuilder().setLevel(Level.DEBUG).setMessage(msg).build(); final StrLookup lookup = new StructuredDataLookup(); String value = lookup.lookup(event, TESTKEY); assertEquals(TESTVAL, value); value = lookup.lookup("BadKey"); assertNull(value); }
Example #25
Source File: SyslogAppenderTestBase.java From logging-log4j2 with Apache License 2.0 | 5 votes |
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 #26
Source File: MapRewritePolicyTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@BeforeClass public static void setupClass() { stringMap.putValue("test1", "one"); stringMap.putValue("test2", "two"); map = stringMap.toMap(); logEvent0 = Log4jLogEvent.newBuilder() // .setLoggerName("test") // .setContextData(stringMap) // .setLoggerFqcn("MapRewritePolicyTest.setupClass()") // .setLevel(Level.ERROR) // .setMessage(new SimpleMessage("Test")) // .setThrown(new RuntimeException("test")) // .setThreadName("none") .setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 28)) .setTimeMillis(2).build(); logEvent1 = ((Log4jLogEvent) logEvent0).asBuilder() // .setMessage(new StringMapMessage(map)) // .setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 29)) // .build(); final ThreadContextStack stack = new MutableThreadContextStack(new ArrayList<>(map.values())); logEvent2 = ((Log4jLogEvent) logEvent0).asBuilder() // .setContextStack(stack) // .setMarker(MarkerManager.getMarker("test")) // .setLevel(Level.TRACE) // .setMessage(new StructuredDataMessage("test", "Nothing", "test", map)) // .setTimeMillis(20000000) // .setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 30)) // .build(); logEvent3 = ((Log4jLogEvent) logEvent0).asBuilder() // .setContextStack(stack) // .setLevel(Level.ALL) // .setMessage(new StringMapMessage(map)) // .setTimeMillis(Long.MAX_VALUE) // .setSource(new StackTraceElement("MapRewritePolicyTest", "setupClass", "MapRewritePolicyTest", 31)) // .build(); rewrite = new KeyValuePair[]{new KeyValuePair("test2", "2"), new KeyValuePair("test3", "three")}; }
Example #27
Source File: RoutingAppender2767Test.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@Test public void routingTest() throws Exception { StructuredDataMessage msg = new StructuredDataMessage("Test", "This is a test", "Service"); EventLogger.logEvent(msg); File file = new File(ACTIVITY_LOG_FILE); assertTrue("Activity file was not created", file.exists()); List<String> lines = Files.lines(file.toPath()).collect(Collectors.toList()); assertEquals("Incorrect number of lines", 1, lines.size()); assertTrue("Incorrect content", lines.get(0).contains("This is a test")); }
Example #28
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@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); }
Example #29
Source File: LoggerTest.java From logging-log4j2 with Apache License 2.0 | 5 votes |
@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 #30
Source File: UserLogoutListenerTest.java From audit-log-plugin with MIT License | 5 votes |
@Issue("JENKINS-54087") @Test public void testValidUsernameInMessageLogged() throws Exception { assertEventCount(app.getEvents(), 0); client.login("debbie", "debbie"); logout(client); StructuredDataMessage logMessage = (StructuredDataMessage) app.getEvents().get(1).getMessage(); assertEventCount(app.getEvents(), 2); assertTrue(logMessage.toString().contains("logout")); assertEquals("debbie", logMessage.get("userId")); }