Skip to content

Commit 33373f9

Browse files
committed
Improve getFormat() usage in RegexFilter
1 parent b3636b2 commit 33373f9

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

log4j-core/src/main/java/org/apache/logging/log4j/core/filter/RegexFilter.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@
3232
import org.apache.logging.log4j.core.config.plugins.PluginElement;
3333
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
3434
import org.apache.logging.log4j.message.Message;
35+
import org.apache.logging.log4j.message.MessageFormatMessage;
3536
import org.apache.logging.log4j.message.ParameterizedMessage;
37+
import org.apache.logging.log4j.message.StringFormattedMessage;
38+
import org.apache.logging.log4j.message.StructuredDataMessage;
3639

3740
/**
38-
* This filter returns the onMatch result if the message matches the regular expression.
39-
*
40-
* The "useRawMsg" attribute can be used to indicate whether the regular expression should be applied to the result of
41-
* calling Message.getMessageFormat (true) or Message.getFormattedMessage() (false). The default is false.
42-
*
41+
* A filter that matches the given regular expression pattern against messages.
4342
*/
4443
@Plugin(name = "RegexFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
4544
public final class RegexFilter extends AbstractFilter {
@@ -78,18 +77,29 @@ public Result filter(
7877
if (msg == null) {
7978
return onMismatch;
8079
}
81-
final String text = useRawMessage ? msg.getFormat() : msg.getFormattedMessage();
80+
final String text = targetMessageTest(msg);
8281
return filter(text);
8382
}
8483

8584
@Override
8685
public Result filter(final LogEvent event) {
87-
final String text = useRawMessage
88-
? event.getMessage().getFormat()
89-
: event.getMessage().getFormattedMessage();
86+
final String text = targetMessageTest(event.getMessage());
9087
return filter(text);
9188
}
9289

90+
// While `Message#getFormat()` is broken in general, it still makes sense for certain types.
91+
// Hence, suppress the deprecation warning.
92+
@SuppressWarnings("deprecation")
93+
private String targetMessageTest(final Message message) {
94+
return useRawMessage
95+
&& (message instanceof ParameterizedMessage
96+
|| message instanceof StringFormattedMessage
97+
|| message instanceof MessageFormatMessage
98+
|| message instanceof StructuredDataMessage)
99+
? message.getFormat()
100+
: message.getFormattedMessage();
101+
}
102+
93103
private Result filter(final String msg) {
94104
if (msg == null) {
95105
return onMismatch;
@@ -114,7 +124,7 @@ public String toString() {
114124
* @param patternFlags
115125
* An array of Strings where each String is a {@link Pattern#compile(String, int)} compilation flag.
116126
* @param useRawMsg
117-
* If true, the raw message will be used, otherwise the formatted message will be used.
127+
* If {@code true}, for {@link ParameterizedMessage}, {@link StringFormattedMessage}, and {@link MessageFormatMessage}, the message format pattern; for {@link StructuredDataMessage}, the message field will be used as the match target.
118128
* @param match
119129
* The action to perform when a match occurs.
120130
* @param mismatch

src/site/antora/modules/ROOT/pages/manual/filters.adoc

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -746,19 +746,16 @@ include::partial$manual/log-event.adoc[]
746746
[#RegexFilter]
747747
==== `RegexFilter`
748748
749-
The `RegexFilter` matches a regular expression against either the result of
750-
link:../javadoc/log4j-api/org/apache/logging/log4j/message/Message.html#getFormat()[`Message.getFormat()`]
751-
or
752-
link:../javadoc/log4j-api/org/apache/logging/log4j/message/Message.html#getFormat()[`Message.getFormattedMessage()`].
753-
It can be used with all kinds of `Message` implementations.
754-
755-
Besides the <<common-configuration-attributes,common configuration attributes>>,
756-
the `RegexFilter` supports the following parameters:
749+
The `RegexFilter` matches a regular expression against messages.
750+
Besides the <<common-configuration-attributes,common configuration attributes>>, the `RegexFilter` supports the following parameters:
757751
758-
.`RegexFilter` -- configuration attributes
759-
[cols="1m,1,1,4"]
752+
.`RegexFilter` configuration attributes
753+
[%header,cols="1m,1,1,4"]
760754
|===
761-
|Attribute | Type | Default value | Description
755+
|Attribute
756+
| Type
757+
| Default value
758+
| Description
762759
763760
| regex
764761
| https://docs.oracle.com/javase/{java-target-version}/docs/api/java/util/regex/Pattern.html[`Pattern`]
@@ -770,13 +767,7 @@ the `RegexFilter` supports the following parameters:
770767
| useRawMsg
771768
| `boolean`
772769
| `false`
773-
| If `true` the result of
774-
link:../javadoc/log4j-api/org/apache/logging/log4j/message/Message.html#getFormat()[`Message.getFormat()`]
775-
will be used.
776-
Otherwise,
777-
link:../javadoc/log4j-api/org/apache/logging/log4j/message/Message.html#getFormat()[`Message.getFormattedMessage()`]
778-
is used.
779-
770+
| If `true`, for xref:manual/messages.adoc#ParameterizedMessage[`ParameterizedMessage`], xref:manual/messages.adoc#StringFormattedMessage[`StringFormattedMessage`], and xref:manual/messages.adoc#MessageFormatMessage[`MessageFormatMessage`], the message format pattern; for xref:manual/messages.adoc#StructuredDataMessage[`StructuredDataMessage`], the message field will be used as the match target.
780771
|===
781772
782773
[WARNING]

0 commit comments

Comments
 (0)