Java Code Examples for org.apache.logging.log4j.message.Message#getFormattedMessage()

The following examples show how to use org.apache.logging.log4j.message.Message#getFormattedMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IbisMaskingLayout.java    From iaf with Apache License 2.0 6 votes vote down vote up
@Override
public final String toSerializable(LogEvent logEvent) {
	MutableLogEvent event = convertToMutableLog4jEvent(logEvent);
	Message msg = event.getMessage();
	String message = msg.getFormattedMessage();

	if (StringUtils.isNotEmpty(message)) {
		message = Misc.hideAll(message, globalReplace);
		message = Misc.hideAll(message, threadLocalReplace.get());

		int length = message.length();
		if (maxLength > 0 && length > maxLength) {
			int diff = length - maxLength;
			//We trim the message because it may end with a newline or whitespace character.
			message = message.substring(0, maxLength).trim() + " " + String.format(moreMessage, diff) + "\r\n";
		}
	}

	event.setMessage(new LogMessage(message, msg.getThrowable()));

	return serializeEvent(event);
}
 
Example 2
Source File: Log4J2DialogAppender.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
private static IdeaLoggingEvent extractLoggingEvent(@Nonnull Message message, @Nonnull Throwable throwable) {
  //noinspection ThrowableResultOfMethodCallIgnored
  Throwable rootCause = ExceptionUtil.getRootCause(throwable);
  if (rootCause instanceof LogEventException) {
    return ((LogEventException)rootCause).getLogMessage();
  }

  String strMessage = message.getFormattedMessage();
  ExceptionWithAttachments withAttachments = ExceptionUtil.findCause(throwable, ExceptionWithAttachments.class);
  if (withAttachments != null) {
    return LogMessageEx.createEvent(strMessage, ExceptionUtil.getThrowableText(throwable), withAttachments.getAttachments());
  }

  return new IdeaLoggingEvent(strMessage, throwable);
}
 
Example 3
Source File: NoGcMessagePatternConverter.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void format(final LogEvent event, final StringBuilder toAppendTo) {
    final Message msg = event.getMessage();
    if (msg != null) {
        String result;
        if (msg instanceof NoGcMessage) {
            toAppendTo.append(((NoGcMessage) msg).get());
            return;
        }
        if (msg instanceof MultiformatMessage) {
            result = ((MultiformatMessage) msg).getFormattedMessage(formats);
        } else {
            result = msg.getFormattedMessage();
        }
        if (result != null) {
            toAppendTo.append(config != null && result.contains("${") ?
                    config.getStrSubstitutor().replace(event, result) : result);
        } else {
            toAppendTo.append("null");
        }
    }
}
 
Example 4
Source File: MessageResolver.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private static void resolveString(
        final String fallbackKey,
        final Message message,
        final JsonWriter jsonWriter) {
    if (fallbackKey != null) {
        jsonWriter.writeObjectStart();
        jsonWriter.writeObjectKey(fallbackKey);
    }
    if (message instanceof StringBuilderFormattable) {
        final StringBuilderFormattable formattable =
                (StringBuilderFormattable) message;
        jsonWriter.writeString(formattable);
    } else {
        final String formattedMessage = message.getFormattedMessage();
        jsonWriter.writeString(formattedMessage);
    }
    if (fallbackKey != null) {
        jsonWriter.writeObjectEnd();
    }
}
 
Example 5
Source File: Rfc5424Layout.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
private void appendMessage(final StringBuilder buffer, final LogEvent event) {
    final Message message = event.getMessage();
    // This layout formats StructuredDataMessages instead of delegating to the Message itself.
    final String text = (message instanceof StructuredDataMessage || message instanceof MessageCollectionMessage)
            ? message.getFormat() : message.getFormattedMessage();

    if (text != null && text.length() > 0) {
        buffer.append(' ').append(escapeNewlines(text, escapeNewLine));
    }

    if (exceptionFormatters != null && event.getThrown() != null) {
        final StringBuilder exception = new StringBuilder(LF);
        for (final PatternFormatter formatter : exceptionFormatters) {
            formatter.format(event, exception);
        }
        buffer.append(escapeNewlines(exception.toString(), escapeNewLine));
    }
    if (includeNewLine) {
        buffer.append(LF);
    }
}
 
Example 6
Source File: PrivacyProtectionLogger.java    From Javacord with Apache License 2.0 5 votes vote down vote up
@Override
public void logMessage(String fqcn, Level level, Marker marker, Message message, Throwable t) {
    String formattedMessage = message.getFormattedMessage();
    if (privateDataSet.stream().anyMatch(formattedMessage::contains)) {
        delegate.log(level, marker, privateDataSet.stream().reduce(
                formattedMessage, (s, privateData) -> s.replace(privateData, PRIVATE_DATA_REPLACEMENT)), t);
    } else {
        delegate.log(level, marker, message, t);
    }
}
 
Example 7
Source File: PrivacyProtectionLogger.java    From Javacord with Apache License 2.0 5 votes vote down vote up
@Override
public void logMessage(String fqcn, Level level, Marker marker, Message message, Throwable t) {
    String formattedMessage = message.getFormattedMessage();
    if (privateDataSet.stream().anyMatch(formattedMessage::contains)) {
        delegate.log(level, marker, privateDataSet.stream().reduce(
                formattedMessage, (s, privateData) -> s.replace(privateData, PRIVATE_DATA_REPLACEMENT)), t);
    } else {
        delegate.log(level, marker, message, t);
    }
}
 
Example 8
Source File: AbstractLoggerTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public void logMessage(final String fqcn, final Level level, final Marker marker, final Message message, final Throwable t) {
    if(expectingThrowables) {
        assertNotNull("Expected a Throwable but received null!", t);
    } else {
        assertNull("Expected null but received a Throwable! "+t, t);
    }
    if (message != null) {
        message.getFormattedMessage();
    }
}
 
Example 9
Source File: MessageAttributeConverter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public String convertToDatabaseColumn(final Message message) {
    if (message == null) {
        return null;
    }

    return message.getFormattedMessage();
}
 
Example 10
Source File: RegexFilter.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Override
public Result filter(final Logger logger, final Level level, final Marker marker, final Message msg,
        final Throwable t) {
    if (msg == null) {
        return onMismatch;
    }
    final String text = useRawMessage ? msg.getFormat() : msg.getFormattedMessage();
    return filter(text);
}
 
Example 11
Source File: TestLogger.java    From strimzi-kafka-operator with Apache License 2.0 4 votes vote down vote up
LoggedMessage(Level level, Marker marker, Message message, Throwable throwable) {
    this.level = level;
    this.marker = marker;
    this.formattedMessage = message.getFormattedMessage();
    this.throwable = throwable;
}
 
Example 12
Source File: MessagePatternConverter.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public void format(final LogEvent event, final StringBuilder toAppendTo) {
    final Message msg = event.getMessage();
    if (msg instanceof StringBuilderFormattable) {

        final boolean doRender = textRenderer != null;
        final StringBuilder workingBuilder = doRender ? new StringBuilder(80) : toAppendTo;

        final int offset = workingBuilder.length();
        if (msg instanceof MultiFormatStringBuilderFormattable) {
            ((MultiFormatStringBuilderFormattable) msg).formatTo(formats, workingBuilder);
        } else {
            ((StringBuilderFormattable) msg).formatTo(workingBuilder);
        }

        // TODO can we optimize this?
        if (config != null && !noLookups) {
            for (int i = offset; i < workingBuilder.length() - 1; i++) {
                if (workingBuilder.charAt(i) == '$' && workingBuilder.charAt(i + 1) == '{') {
                    final String value = workingBuilder.substring(offset, workingBuilder.length());
                    workingBuilder.setLength(offset);
                    workingBuilder.append(config.getStrSubstitutor().replace(event, value));
                }
            }
        }
        if (doRender) {
            textRenderer.render(workingBuilder, toAppendTo);
        }
        return;
    }
    if (msg != null) {
        String result;
        if (msg instanceof MultiformatMessage) {
            result = ((MultiformatMessage) msg).getFormattedMessage(formats);
        } else {
            result = msg.getFormattedMessage();
        }
        if (result != null) {
            toAppendTo.append(config != null && result.contains("${")
                    ? config.getStrSubstitutor().replace(event, result) : result);
        } else {
            toAppendTo.append("null");
        }
    }
}
 
Example 13
Source File: InternalAsyncUtil.java    From logging-log4j2 with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the specified message, with its content frozen unless system property
 * {@code log4j.format.msg.async} is true or the message class is annotated with
 * {@link AsynchronouslyFormattable}.
 *
 * @param msg the message object to inspect, modify and return
 * @return Returns the specified message, with its content frozen
 */
public static Message makeMessageImmutable(final Message msg) {
    // if the Message instance is reused, there is no point in freezing its message here
    if (msg != null && !canFormatMessageInBackground(msg)) {
        msg.getFormattedMessage(); // LOG4J2-763: ask message to makeMessageImmutable parameters
    }
    return msg;
}