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

The following examples show how to use org.apache.logging.log4j.message.Message#getFormat() . 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: 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 2
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 3
Source File: MaskingRewritePolicy.java    From owasp-security-logging with Apache License 2.0 4 votes vote down vote up
/**
 * 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(LogEvent source) {
	// get the markers for the log event. If no markers, nothing can be
	// tagged confidential and we can return
	Marker sourceMarker = source.getMarker();
	if (sourceMarker == null) {
		return source;
	}

	// get the message. If no message we can return
	final Message msg = source.getMessage();
	if (msg == null || !(msg instanceof ParameterizedMessage)) {
		return source;
	}

	// get the parameters. If no params we can return
	Object[] params = msg.getParameters();
	if (params == null || params.length == 0) {
		return source;
	}

	// check if this event is actually marked as confidential. If not,
	// return
	Log4jMarker eventMarker = new Log4jMarker(sourceMarker);
	if (!eventMarker.contains(SecurityMarkers.CONFIDENTIAL)) {
		return source;
	}

	// we have a message, parameters, a marker, and it is confidential.
	// Process
	for (int i = 0; i < params.length; i++) {
		params[i] = MASKED_PASSWORD;
	}

	// create new message
	Message outMessage = new ParameterizedMessage(msg.getFormat(), params,
			msg.getThrowable());

	// build new log event for output
	LogEvent output = new Log4jLogEvent.Builder(source)
			.setMessage(outMessage).build();

	return output;
}