Search in sources :

Example 16 with ObjectMessage

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();
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) MapMessage(org.apache.logging.log4j.message.MapMessage) Message(org.apache.logging.log4j.message.Message) LogEvent(org.apache.logging.log4j.core.LogEvent) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Test(org.junit.Test)

Example 17 with ObjectMessage

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);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) StringMapMessage(org.apache.logging.log4j.message.StringMapMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Message(org.apache.logging.log4j.message.Message) LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Test(org.junit.jupiter.api.Test)

Aggregations

ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)17 Message (org.apache.logging.log4j.message.Message)11 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)11 MapMessage (org.apache.logging.log4j.message.MapMessage)6 Test (org.junit.jupiter.api.Test)6 LocalizedMessage (org.apache.logging.log4j.message.LocalizedMessage)5 Map (java.util.Map)4 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 RendererMap (org.apache.log4j.or.RendererMap)4 LogEvent (org.apache.logging.log4j.core.LogEvent)4 Marker (org.apache.logging.log4j.Marker)3 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)3 Level (org.apache.logging.log4j.Level)2 Logger (org.apache.logging.log4j.Logger)2 ExtendedLogger (org.apache.logging.log4j.spi.ExtendedLogger)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 LinkedHashMap (java.util.LinkedHashMap)1 SimpleBindings (javax.script.SimpleBindings)1 ObjectRenderer (org.apache.log4j.or.ObjectRenderer)1