Search in sources :

Example 1 with ObjectMessage

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

the class ScriptFilter method filter.

@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final Object msg, final Throwable t) {
    final SimpleBindings bindings = new SimpleBindings();
    bindings.put("logger", logger);
    bindings.put("level", level);
    bindings.put("marker", marker);
    bindings.put("message", msg instanceof String ? new SimpleMessage((String) msg) : new ObjectMessage(msg));
    bindings.put("parameters", null);
    bindings.put("throwable", t);
    bindings.putAll(configuration.getProperties());
    bindings.put("substitutor", configuration.getStrSubstitutor());
    final Object object = configuration.getScriptManager().execute(script.getName(), bindings);
    return object == null || !Boolean.TRUE.equals(object) ? onMismatch : onMatch;
}
Also used : SimpleBindings(javax.script.SimpleBindings) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage)

Example 2 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 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(description + ": No events", events);
        assertThat(events, hasSize(expected.expectedEventCount));
        final LogEvent event = events.get(0);
        assertEquals(description + ": Expected level " + expected.expectedInitialEventLevel + ", got" + event.getLevel(), event.getLevel().name(), expected.expectedInitialEventLevel);
    }
}
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.Test)

Example 3 with ObjectMessage

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

the class Category method maybeLog.

void maybeLog(final String fqcn, final org.apache.logging.log4j.Level level, final Object message, final Throwable throwable) {
    if (logger.isEnabled(level)) {
        final Message msg;
        if (message instanceof String) {
            msg = new SimpleMessage((String) message);
        } else // SimpleMessage treats String and CharSequence differently, hence this else-if block.
        if (message instanceof CharSequence) {
            msg = new SimpleMessage((CharSequence) message);
        } else if (message instanceof Map) {
            @SuppressWarnings("unchecked") final Map<String, ?> map = (Map<String, ?>) message;
            msg = new MapMessage<>(map);
        } else {
            msg = new ObjectMessage(message);
        }
        if (logger instanceof ExtendedLogger) {
            ((ExtendedLogger) logger).logMessage(fqcn, level, null, msg, throwable);
        } else {
            logger.log(level, msg, throwable);
        }
    }
}
Also used : ExtendedLogger(org.apache.logging.log4j.spi.ExtendedLogger) 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) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) RendererMap(org.apache.log4j.or.RendererMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 4 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, final Throwable t) {
    if (isEnabledFor(priority)) {
        @SuppressWarnings("unchecked") final Message msg = message instanceof Map ? new MapMessage((Map) message) : 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) MapMessage(org.apache.logging.log4j.message.MapMessage) RendererMap(org.apache.log4j.or.RendererMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 5 with ObjectMessage

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

the class MessageResolverTest method test_ObjectMessage.

@Test
void test_ObjectMessage() {
    // Create the log event.
    final int id = 0xDEADBEEF;
    final String name = "name-" + id;
    final Object attachment = new LinkedHashMap<String, Object>() {

        {
            put("id", id);
            put("name", name);
        }
    };
    final ObjectMessage message = new ObjectMessage(attachment);
    final LogEvent logEvent = Log4jLogEvent.newBuilder().setMessage(message).build();
    // Create the event template.
    final String eventTemplate = writeJson(asMap("message", asMap("$resolver", "message")));
    // Create the layout.
    JsonTemplateLayout layout = JsonTemplateLayout.newBuilder().setConfiguration(CONFIGURATION).setStackTraceEnabled(true).setEventTemplate(eventTemplate).build();
    // Check the serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        assertThat(accessor.getInteger(new String[] { "message", "id" })).isEqualTo(id);
        assertThat(accessor.getString(new String[] { "message", "name" })).isEqualTo(name);
    });
}
Also used : LogEvent(org.apache.logging.log4j.core.LogEvent) Log4jLogEvent(org.apache.logging.log4j.core.impl.Log4jLogEvent) ObjectMessage(org.apache.logging.log4j.message.ObjectMessage) JsonTemplateLayout(org.apache.logging.log4j.layout.template.json.JsonTemplateLayout) LinkedHashMap(java.util.LinkedHashMap) 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