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));
}
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());
}
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();
}
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);
}
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");
}
Aggregations