use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.
the class CategoryTest method testForcedLog.
/**
* Tests Category.forcedLog.
*/
@Test
@SuppressWarnings("deprecation")
public void testForcedLog() {
final MockCategory category = new MockCategory("org.example.foo");
category.setAdditivity(false);
((org.apache.logging.log4j.core.Logger) category.getLogger()).addAppender(appender);
category.info("Hello, World");
final List<LogEvent> list = appender.getEvents();
int events = list.size();
assertTrue("Number of events should be 1, was " + events, events == 1);
LogEvent event = list.get(0);
Message msg = event.getMessage();
assertNotNull("No message", msg);
assertTrue("Incorrect Message type", msg instanceof ObjectMessage);
Object[] objects = msg.getParameters();
assertTrue("Incorrect Object type", objects[0] instanceof String);
appender.clear();
category.log(Priority.INFO, "Hello, World");
events = list.size();
assertTrue("Number of events should be 1, was " + events, events == 1);
event = list.get(0);
msg = event.getMessage();
assertNotNull("No message", msg);
assertTrue("Incorrect Message type", msg instanceof ObjectMessage);
objects = msg.getParameters();
assertTrue("Incorrect Object type", objects[0] instanceof String);
appender.clear();
}
use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.
the class JsonTemplateLayoutTest method test_recursive_collections.
@Test
void test_recursive_collections() {
// Create the event template.
final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message")));
// Create the layout.
final JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setEventTemplate(eventTemplate).build();
// Create a recursive collection.
final Object[] recursiveCollection = new Object[1];
recursiveCollection[0] = recursiveCollection;
// Create the log event.
final Message message = new ObjectMessage(recursiveCollection);
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName(LOGGER_NAME).setMessage(message).build();
// Check the serialized event.
Assertions.assertThatThrownBy(() -> layout.toSerializable(logEvent)).isInstanceOf(StackOverflowError.class);
}
Aggregations