Search in sources :

Example 11 with ObjectMessage

use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.

the class Category method log.

public void log(final String fqcn, final Priority priority, final Object message, final Throwable t) {
    if (isEnabledFor(priority)) {
        final Message msg = new ObjectMessage(message);
        forcedLog(fqcn, priority, msg, t);
    }
}
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) LocalizedMessage(org.apache.logging.log4j.message.LocalizedMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage)

Example 12 with ObjectMessage

use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.

the class Category method log.

public void log(final Priority priority, final Object message) {
    if (isEnabledFor(priority)) {
        @SuppressWarnings("unchecked") final Message msg = message instanceof Map ? new MapMessage((Map) message) : new ObjectMessage(message);
        forcedLog(FQCN, priority, msg, null);
    }
}
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) LocalizedMessage(org.apache.logging.log4j.message.LocalizedMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) MapMessage(org.apache.logging.log4j.message.MapMessage) RendererMap(org.apache.log4j.or.RendererMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 13 with ObjectMessage

use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.

the class LoggerTest method flowTracingString_SupplierOfObjectMessages.

@Test
public void flowTracingString_SupplierOfObjectMessages() {
    final EntryMessage msg = logger.traceEntry("doFoo(a={}, b={})", new Supplier<Message>() {

        @Override
        public Message get() {
            return new ObjectMessage(1);
        }
    }, new Supplier<Message>() {

        @Override
        public Message get() {
            return new ObjectMessage(2);
        }
    });
    logger.traceExit(msg, 3);
    assertEquals(2, results.size());
    assertThat("Incorrect Entry", results.get(0), startsWith("ENTER[ FLOW ] TRACE Enter"));
    assertThat("Missing entry data", results.get(0), containsString("doFoo(a=1, b=2)"));
    assertThat("Incorrect Exit", results.get(1), startsWith("EXIT[ FLOW ] TRACE Exit"));
    assertThat("Missing exit data", results.get(1), containsString("doFoo(a=1, b=2): 3"));
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) StructuredDataMessage(org.apache.logging.log4j.message.StructuredDataMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) EntryMessage(org.apache.logging.log4j.message.EntryMessage) Message(org.apache.logging.log4j.message.Message) JsonMessage(org.apache.logging.log4j.message.JsonMessage) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) EntryMessage(org.apache.logging.log4j.message.EntryMessage) Test(org.junit.jupiter.api.Test)

Example 14 with ObjectMessage

use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.

the class LoggerSupplierTest method flowTracing_SupplierOfObjectMessage.

@Test
public void flowTracing_SupplierOfObjectMessage() {
    logger.traceEntry(() -> new ObjectMessage(1234567890));
    assertThat(results).hasSize(1);
    String entry = results.get(0);
    assertThat(entry).startsWith("ENTER[ FLOW ] TRACE Enter").contains("(1234567890)").doesNotContain("ObjectMessage");
}
Also used : ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Test(org.junit.jupiter.api.Test)

Example 15 with ObjectMessage

use of org.apache.logging.log4j.message.ObjectMessage in project logging-log4j2 by apache.

the class LevelTest method testLevelLogging.

@Test
public void testLevelLogging(final LoggerContext context) {
    final Marker marker = MarkerManager.getMarker("marker");
    final Message msg = new ObjectMessage("msg");
    final Throwable t = new Throwable("test");
    final Level[] levels = new Level[] { Level.TRACE, Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR, Level.FATAL };
    final String[] names = new String[] { "levelTest", "levelTest.Trace", "levelTest.Debug", "levelTest.Info", "levelTest.Warn", "levelTest.Error", "levelTest.Fatal" };
    for (final Level level : levels) {
        for (final String name : names) {
            final Logger logger = context.getLogger(name);
            // Message
            logger.log(level, msg);
            // Object
            logger.log(level, 123);
            // String
            logger.log(level, name);
            // Marker, Message
            logger.log(level, marker, msg);
            // Marker, Object
            logger.log(level, marker, 123);
            // marker, String
            logger.log(level, marker, name);
            // Message, Throwable
            logger.log(level, msg, t);
            // Object, Throwable
            logger.log(level, 123, t);
            // String, Object...
            logger.log(level, name, "param1", "param2");
            // String, Throwable
            logger.log(level, name, t);
            // Marker, Message, Throwable
            logger.log(level, marker, msg, t);
            // Marker, Object, Throwable
            logger.log(level, marker, 123, t);
            // Marker, String, Object...
            logger.log(level, marker, name, "param1", "param2");
            // Marker, String, Throwable
            logger.log(level, marker, name, t);
        }
    }
    // Logger "levelTest" will not receive same events as "levelTest.Trace"
    int levelCount = names.length - 1;
    final int UNIT = 14;
    final Expected[] expectedResults = new Expected[] { // 
    new Expected(listAll, UNIT * levelCount, "TRACE", "All"), // 
    new Expected(listTrace, UNIT * levelCount--, "TRACE", "Trace"), // 
    new Expected(listDebug, UNIT * levelCount--, "DEBUG", "Debug"), // 
    new Expected(listInfo, UNIT * levelCount--, "INFO", "Info"), // 
    new Expected(listWarn, UNIT * levelCount--, "WARN", "Warn"), // 
    new Expected(listError, UNIT * levelCount--, "ERROR", "Error"), // 
    new Expected(listFatal, UNIT * levelCount--, "FATAL", "Fatal") };
    for (final Expected expected : expectedResults) {
        final String description = expected.description;
        final List<LogEvent> events = expected.appender.getEvents();
        assertNotNull(events, description + ": No events");
        assertThat(events, hasSize(expected.expectedEventCount));
        final LogEvent event = events.get(0);
        assertEquals(event.getLevel().name(), expected.expectedInitialEventLevel, description + ": Expected level " + expected.expectedInitialEventLevel + ", got" + event.getLevel());
    }
}
Also used : ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Message(org.apache.logging.log4j.message.Message) Marker(org.apache.logging.log4j.Marker) Logger(org.apache.logging.log4j.Logger) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) Level(org.apache.logging.log4j.Level) 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