use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class ReadOnlyStringMapResolverTest method test_mdc_pattern.
@Test
void test_mdc_pattern() {
// Create the log event.
final SimpleMessage message = new SimpleMessage("Hello, World!");
final StringMap contextData = new SortedArrayStringMap();
final String mdcPatternMatchedKey = "mdcKey1";
final String mdcPatternMatchedValue = "mdcValue1";
contextData.putValue(mdcPatternMatchedKey, mdcPatternMatchedValue);
final String mdcPatternMismatchedKey = "mdcKey2";
final String mdcPatternMismatchedValue = "mdcValue2";
contextData.putValue(mdcPatternMismatchedKey, mdcPatternMismatchedValue);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).setContextData(contextData).build();
// Check the serialized event.
testReadOnlyStringMapPattern(mdcPatternMatchedKey, mdcPatternMatchedValue, mdcPatternMismatchedKey, logEvent, "mdc");
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class JndiRestrictedLookupTest method testBadSerializableLookup.
@Test
public void testBadSerializableLookup() throws Exception {
int port = embeddedLdapRule.embeddedServerPort();
Context context = embeddedLdapRule.context();
context.bind("cn=" + TEST_MESSAGE + "," + DOMAIN_DSN, new SimpleMessage("Test Message"));
final StrLookup lookup = new JndiLookup();
String result = lookup.lookup(LDAP_URL + port + "/" + "cn=" + TEST_MESSAGE + "," + DOMAIN_DSN);
if (result != null) {
fail("Lookup returned an object");
}
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class PropertyRewritePolicy method rewrite.
/**
* {@inheritDoc}
*/
public LoggingEvent rewrite(final LoggingEvent source) {
if (!properties.isEmpty()) {
Map<String, String> rewriteProps = source.getProperties() != null ? new HashMap<>(source.getProperties()) : new HashMap<>();
for (Map.Entry<String, String> entry : properties.entrySet()) {
if (!rewriteProps.containsKey(entry.getKey())) {
rewriteProps.put(entry.getKey(), entry.getValue());
}
}
LogEvent event;
if (source instanceof LogEventAdapter) {
event = new Log4jLogEvent.Builder(((LogEventAdapter) source).getEvent()).setContextData(new SortedArrayStringMap(rewriteProps)).build();
} else {
LocationInfo info = source.getLocationInformation();
StackTraceElement element = new StackTraceElement(info.getClassName(), info.getMethodName(), info.getFileName(), Integer.parseInt(info.getLineNumber()));
Thread thread = getThread(source.getThreadName());
long threadId = thread != null ? thread.getId() : 0;
int threadPriority = thread != null ? thread.getPriority() : 0;
event = Log4jLogEvent.newBuilder().setContextData(new SortedArrayStringMap(rewriteProps)).setLevel(OptionConverter.convertLevel(source.getLevel())).setLoggerFqcn(source.getFQNOfLoggerClass()).setMarker(null).setMessage(new SimpleMessage(source.getRenderedMessage())).setSource(element).setLoggerName(source.getLoggerName()).setThreadName(source.getThreadName()).setThreadId(threadId).setThreadPriority(threadPriority).setThrown(source.getThrowableInformation().getThrowable()).setTimeMillis(source.getTimeStamp()).setNanoTime(0).setThrownProxy(null).build();
}
return new LogEventAdapter(event);
}
return source;
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class Log4jLogEventTest method testBuilderCorrectlyCopiesAllEventAttributes.
@Test
public void testBuilderCorrectlyCopiesAllEventAttributes() {
final StringMap contextData = ContextDataFactory.createContextData();
contextData.putValue("A", "B");
final ContextStack contextStack = ThreadContext.getImmutableStack();
final Exception exception = new Exception("test");
final Marker marker = MarkerManager.getMarker("EVENTTEST");
final Message message = new SimpleMessage("foo");
final StackTraceElement stackTraceElement = new StackTraceElement("A", "B", "file", 123);
final String fqcn = "qualified";
final String name = "Ceci n'est pas une pipe";
final String threadName = "threadName";
final Log4jLogEvent event = //
Log4jLogEvent.newBuilder().setContextData(//
contextData).setContextStack(//
contextStack).setEndOfBatch(//
true).setIncludeLocation(//
true).setLevel(//
Level.FATAL).setLoggerFqcn(//
fqcn).setLoggerName(//
name).setMarker(//
marker).setMessage(//
message).setNanoTime(//
1234567890L).setSource(//
stackTraceElement).setThreadName(//
threadName).setThrown(//
exception).setTimeMillis(987654321L).build();
assertEquals(contextData, event.getContextData());
assertSame(contextStack, event.getContextStack());
assertTrue(event.isEndOfBatch());
assertTrue(event.isIncludeLocation());
assertSame(Level.FATAL, event.getLevel());
assertSame(fqcn, event.getLoggerFqcn());
assertSame(name, event.getLoggerName());
assertSame(marker, event.getMarker());
assertSame(message, event.getMessage());
assertEquals(1234567890L, event.getNanoTime());
assertSame(stackTraceElement, event.getSource());
assertSame(threadName, event.getThreadName());
assertSame(exception, event.getThrown());
assertEquals(987654321L, event.getTimeMillis());
final LogEvent event2 = new Log4jLogEvent.Builder(event).build();
assertEquals(event2, event, "copy constructor builder");
assertEquals(event2.hashCode(), event.hashCode(), "same hashCode");
}
use of org.apache.logging.log4j.message.SimpleMessage in project logging-log4j2 by apache.
the class Log4jLogEventTest method testJavaIoSerializableWithThrown.
@Test
public void testJavaIoSerializableWithThrown() throws Exception {
final Error thrown = new InternalError("test error");
final Log4jLogEvent evt = //
Log4jLogEvent.newBuilder().setLoggerName(//
"some.test").setLoggerFqcn(//
Strings.EMPTY).setLevel(//
Level.INFO).setMessage(//
new SimpleMessage("abc")).setThrown(//
thrown).build();
final byte[] binary = serialize(evt);
final Log4jLogEvent evt2 = deserialize(binary);
assertEquals(evt.getTimeMillis(), evt2.getTimeMillis());
assertEquals(evt.getLoggerFqcn(), evt2.getLoggerFqcn());
assertEquals(evt.getLevel(), evt2.getLevel());
assertEquals(evt.getLoggerName(), evt2.getLoggerName());
assertEquals(evt.getMarker(), evt2.getMarker());
assertEquals(evt.getContextData(), evt2.getContextData());
assertEquals(evt.getContextStack(), evt2.getContextStack());
assertEquals(evt.getMessage(), evt2.getMessage());
assertEquals(evt.getSource(), evt2.getSource());
assertEquals(evt.getThreadName(), evt2.getThreadName());
assertNull(evt2.getThrown());
assertNotNull(evt2.getThrownProxy());
assertEquals(evt.getThrownProxy(), evt2.getThrownProxy());
assertEquals(evt.isEndOfBatch(), evt2.isEndOfBatch());
assertEquals(evt.isIncludeLocation(), evt2.isIncludeLocation());
}
Aggregations