Search in sources :

Example 96 with Message

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

the class AbstractLogger method entryMsg.

protected EntryMessage entryMsg(final String format, final Object... params) {
    final int count = params == null ? 0 : params.length;
    if (count == 0) {
        if (Strings.isEmpty(format)) {
            return flowMessageFactory.newEntryMessage(null);
        }
        return flowMessageFactory.newEntryMessage(new SimpleMessage(format));
    }
    if (format != null) {
        return flowMessageFactory.newEntryMessage(new ParameterizedMessage(format, params));
    }
    final StringBuilder sb = new StringBuilder();
    sb.append("params(");
    for (int i = 0; i < count; i++) {
        if (i > 0) {
            sb.append(", ");
        }
        final Object parm = params[i];
        sb.append(parm instanceof Message ? ((Message) parm).getFormattedMessage() : String.valueOf(parm));
    }
    sb.append(')');
    return flowMessageFactory.newEntryMessage(new SimpleMessage(sb));
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) Message(org.apache.logging.log4j.message.Message) EntryMessage(org.apache.logging.log4j.message.EntryMessage) StringFormattedMessage(org.apache.logging.log4j.message.StringFormattedMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 97 with Message

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

the class RingBufferLogEventTest method testGetMillisReturnsConstructorMillisForNormalMessage.

@Test
public void testGetMillisReturnsConstructorMillisForNormalMessage() {
    final RingBufferLogEvent evt = new RingBufferLogEvent();
    final String loggerName = null;
    final Marker marker = null;
    final String fqcn = null;
    final Level level = null;
    final Message data = null;
    final Throwable t = null;
    final ContextStack contextStack = null;
    final String threadName = null;
    final StackTraceElement location = null;
    evt.setValues(null, loggerName, marker, fqcn, level, data, t, (StringMap) evt.getContextData(), contextStack, -1, threadName, -1, location, new FixedPreciseClock(123, 456), new DummyNanoClock(1));
    assertEquals(123, evt.getTimeMillis());
    assertEquals(456, evt.getInstant().getNanoOfMillisecond());
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) FixedPreciseClock(org.apache.logging.log4j.core.time.internal.FixedPreciseClock) DummyNanoClock(org.apache.logging.log4j.core.time.internal.DummyNanoClock) Level(org.apache.logging.log4j.Level) Marker(org.apache.logging.log4j.Marker) MutableThreadContextStack(org.apache.logging.log4j.spi.MutableThreadContextStack) ContextStack(org.apache.logging.log4j.ThreadContext.ContextStack) Test(org.junit.Test)

Example 98 with Message

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

the class MapRewritePolicy method rewrite.

/**
 * Rewrite the event.
 * @param source a logging event that may be returned or
 * used to create a new logging event.
 * @return The LogEvent after rewriting.
 */
@Override
public LogEvent rewrite(final LogEvent source) {
    final Message msg = source.getMessage();
    if (msg == null || !(msg instanceof MapMessage)) {
        return source;
    }
    @SuppressWarnings("unchecked") final MapMessage<?, Object> mapMsg = (MapMessage<?, Object>) msg;
    final Map<String, Object> newMap = new HashMap<>(mapMsg.getData());
    switch(mode) {
        case Add:
            {
                newMap.putAll(map);
                break;
            }
        default:
            {
                for (final Map.Entry<String, Object> entry : map.entrySet()) {
                    if (newMap.containsKey(entry.getKey())) {
                        newMap.put(entry.getKey(), entry.getValue());
                    }
                }
            }
    }
    final Message message = mapMsg.newInstance(newMap);
    return new Log4jLogEvent.Builder(source).setMessage(message).build();
}
Also used : MapMessage(org.apache.logging.log4j.message.MapMessage) Message(org.apache.logging.log4j.message.Message) HashMap(java.util.HashMap) MapMessage(org.apache.logging.log4j.message.MapMessage)

Example 99 with Message

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

the class Log4jLogger method log.

@Override
public void log(final Marker marker, final String fqcn, final int level, final String message, final Object[] params, Throwable throwable) {
    final Level log4jLevel = getLevel(level);
    final org.apache.logging.log4j.Marker log4jMarker = getMarker(marker);
    if (!logger.isEnabled(log4jLevel, log4jMarker, message, params)) {
        return;
    }
    final Message msg;
    if (params == null) {
        msg = new SimpleMessage(message);
    } else {
        msg = new ParameterizedMessage(message, params, throwable);
        if (throwable != null) {
            throwable = msg.getThrowable();
        }
    }
    logger.logMessage(fqcn, log4jLevel, log4jMarker, msg, throwable);
}
Also used : SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Message(org.apache.logging.log4j.message.Message) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) SimpleMessage(org.apache.logging.log4j.message.SimpleMessage) Level(org.apache.logging.log4j.Level) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage)

Example 100 with Message

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

the class AsyncLoggerEventTranslationExceptionTest method testEventTranslationExceptionDoesNotCauseAsyncEventException.

@Test
void testEventTranslationExceptionDoesNotCauseAsyncEventException() {
    final Logger log = LogManager.getLogger("com.foo.Bar");
    assertTrue(TestExceptionHandler.INSTANTIATED, "TestExceptionHandler was not configured properly");
    final Message exceptionThrowingMessage = new ExceptionThrowingMessage();
    assertThrows(TestMessageException.class, () -> ((AbstractLogger) log).logMessage("com.foo.Bar", Level.INFO, null, exceptionThrowingMessage, null));
    // stop async thread
    CoreLoggerContexts.stopLoggerContext();
    assertFalse(TestExceptionHandler.EVENT_EXCEPTION_ENCOUNTERED, "ExceptionHandler encountered an event exception");
}
Also used : ReusableSimpleMessage(org.apache.logging.log4j.message.ReusableSimpleMessage) Message(org.apache.logging.log4j.message.Message) Logger(org.apache.logging.log4j.Logger) AbstractLogger(org.apache.logging.log4j.spi.AbstractLogger) Test(org.junit.jupiter.api.Test)

Aggregations

Message (org.apache.logging.log4j.message.Message)128 SimpleMessage (org.apache.logging.log4j.message.SimpleMessage)88 LogEvent (org.apache.logging.log4j.core.LogEvent)55 Test (org.junit.jupiter.api.Test)50 Log4jLogEvent (org.apache.logging.log4j.core.impl.Log4jLogEvent)46 Marker (org.apache.logging.log4j.Marker)26 ObjectMessage (org.apache.logging.log4j.message.ObjectMessage)26 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)24 Level (org.apache.logging.log4j.Level)23 Test (org.junit.Test)22 StringMapMessage (org.apache.logging.log4j.message.StringMapMessage)19 StructuredDataMessage (org.apache.logging.log4j.message.StructuredDataMessage)18 ContextStack (org.apache.logging.log4j.ThreadContext.ContextStack)17 MapMessage (org.apache.logging.log4j.message.MapMessage)15 StringMap (org.apache.logging.log4j.util.StringMap)15 DummyNanoClock (org.apache.logging.log4j.core.time.internal.DummyNanoClock)9 EntryMessage (org.apache.logging.log4j.message.EntryMessage)9 Map (java.util.Map)8 StringFormattedMessage (org.apache.logging.log4j.message.StringFormattedMessage)8 IOException (java.io.IOException)7