throwable, event.getThreadName(), event.getContextData().toMap() ));
protected void assertLogEvent(final LogEvent logEvent) { assertThat(logEvent, is(notNullValue())); assertThat(logEvent.getInstant().getEpochMillisecond(), equalTo(1493121664118L)); assertThat(logEvent.getThreadName(), equalTo("main")); assertThat(logEvent.getThreadId(), equalTo(1L)); assertThat(logEvent.getThreadPriority(), equalTo(5)); assertThat(logEvent.getLevel(), equalTo(Level.INFO)); assertThat(logEvent.getLoggerName(), equalTo("HelloWorld")); assertThat(logEvent.getMarker().getName(), equalTo("child")); assertThat(logEvent.getMarker().getParents()[0].getName(), equalTo("parent")); assertThat(logEvent.getMarker().getParents()[0].getParents()[0].getName(), equalTo("grandparent")); assertThat(logEvent.getMessage().getFormattedMessage(), equalTo("Hello, world!")); assertThat(logEvent.getThrown(), is(nullValue())); assertThat(logEvent.getThrownProxy().getMessage(), equalTo("error message")); assertThat(logEvent.getThrownProxy().getName(), equalTo("java.lang.RuntimeException")); assertThat(logEvent.getThrownProxy().getExtendedStackTrace()[0].getClassName(), equalTo("logtest.Main")); assertThat(logEvent.getLoggerFqcn(), equalTo("org.apache.logging.log4j.spi.AbstractLogger")); assertThat(logEvent.getContextStack().asList(), equalTo(Arrays.asList("one", "two"))); assertThat((String) logEvent.getContextData().getValue("foo"), equalTo("FOO")); assertThat((String) logEvent.getContextData().getValue("bar"), equalTo("BAR")); assertThat(logEvent.getSource().getClassName(), equalTo("logtest.Main")); } }
@Override public void putAll(final ReadOnlyStringMap source) { if (source == this || source == null || source.isEmpty()) { // this.putAll(this) does not modify this collection // this.putAll(null) does not modify this collection // this.putAll(empty ReadOnlyStringMap) does not modify this collection return; } assertNotFrozen(); assertNoConcurrentModification(); if (source instanceof SortedArrayStringMap) { if (this.size == 0) { initFrom0((SortedArrayStringMap) source); } else { merge((SortedArrayStringMap) source); } } else if (source != null) { source.forEach(PUT_ALL, this); } }
if (event.getContextData() != null && !event.getContextData().isEmpty()) { sbuf.append("<tr><td bgcolor=\"#EEEEEE\" style=\"font-size : ").append(fontSize); sbuf.append(";\" colspan=\"6\" "); sbuf.append("title=\"Mapped Diagnostic Context\">"); sbuf.append("MDC: ").append(Transform.escapeHtmlTags(event.getContextData().toMap().toString())); sbuf.append("</td></tr>").append(Strings.LINE_SEPARATOR);
/** * {@inheritDoc} */ @Override public void format(final LogEvent event, final StringBuilder toAppendTo) { if (key == null) { // if there is no additional options, we output every single Key/Value pair for the MDC toAppendTo.append('{'); event.getContextData().forEach(APPEND_EACH, toAppendTo); toAppendTo.append('}'); } else { // otherwise they just want a single key output final Object val = event.getContextData().getValue(key); if (val != null) { toAppendTo.append(val); } } }
/** * {@inheritDoc} */ @Override public void format(final LogEvent event, final StringBuilder toAppendTo) { final ReadOnlyStringMap contextData = event.getContextData(); // if there is no additional options, we output every single // Key/Value pair for the MDC in a similar format to Hashtable.toString() if (full) { if (contextData == null || contextData.size() == 0) { toAppendTo.append("{}"); return; } appendFully(contextData, toAppendTo); } else { if (keys != null) { if (contextData == null || contextData.size() == 0) { toAppendTo.append("{}"); return; } appendSelectedKeys(keys, contextData, toAppendTo); } else if (contextData != null){ // otherwise they just want a single key output final Object value = contextData.getValue(key); if (value != null) { StringBuilders.appendValue(toAppendTo, value); } } } }
@Override public void serialize(final ReadOnlyStringMap contextData, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonGenerationException { jgen.writeStartObject(); contextData.forEach(WRITE_STRING_FIELD_INTO, jgen); jgen.writeEndObject(); }
@Test public void testClear() { final MutableLogEvent mutable = new MutableLogEvent(); assertEquals("context data", 0, mutable.getContextData().size()); assertNull("context stack", mutable.getContextStack()); assertFalse("end of batch", mutable.isEndOfBatch()); assertEquals("context map", 0, mutable.getContextData().size()); assertNull("context stack", mutable.getContextStack()); assertSame("level", Level.OFF, mutable.getLevel());
@Override public void serialize(final ReadOnlyStringMap contextData, final JsonGenerator jgen, final SerializerProvider provider) throws IOException, JsonGenerationException { final MapEntry[] pairs = new MapEntry[contextData.size()]; contextData.forEach(new BiConsumer<String, Object>() { int i = 0; @Override public void accept(final String key, final Object value) { pairs[i++] = new MapEntry(key, String.valueOf(value)); } }); jgen.writeObject(pairs); } }
@Override public void putAll(final ReadOnlyStringMap source) { assertNotFrozen(); source.forEach(PUT_ALL, map); sortedKeys = null; }
@Override public void append(LogEvent event) { Event appenderEvent = new Event(event, event.getContextData().toMap()); if (this.manager.async) { this.events.add(appenderEvent); } else { sendEvent(appenderEvent, appenderEvent.getProperties()); } }
assertTrue("Incorrect event", event.getMessage().getFormattedMessage().equals("This is a test message")); assertTrue("Message not delivered via TCP", tcpTestServer.getCount() > 0); assertEquals(expectedUuidStr, event.getContextData().getValue(tcKey)); event = tcpTestServer.getQueue().poll(3, TimeUnit.SECONDS); assertNotNull("No event retrieved", event);
@Override public void putAll(final ReadOnlyStringMap source) { if (source == this || source == null || source.isEmpty()) { // this.putAll(this) does not modify this collection // this.putAll(null) does not modify this collection // this.putAll(empty ReadOnlyStringMap) does not modify this collection return; } assertNotFrozen(); assertNoConcurrentModification(); if (source instanceof SortedArrayStringMap) { if (this.size == 0) { initFrom0((SortedArrayStringMap) source); } else { merge((SortedArrayStringMap) source); } } else if (source != null) { source.forEach(PUT_ALL, this); } }
private static void appendFully(final ReadOnlyStringMap contextData, final StringBuilder toAppendTo) { final StringBuilder sb = getStringBuilder(); sb.append("{"); contextData.forEach(WRITE_KEY_VALUES_INTO, sb); sb.append('}'); toAppendTo.append(sb); trimToMaxSize(sb); }
@Override public void append(LogEvent event) { Event appenderEvent = new Event(event, event.getContextData().toMap()); if (this.manager.async) { this.events.add(appenderEvent); } else { sendEvent(appenderEvent, appenderEvent.getProperties()); } }
/** * Looks up the value from the ThreadContext Map. * @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) { return event.getContextData().getValue(key); } }