Skip to content

Commit f0ec0b8

Browse files
committed
Removed 'patternFlags' @PluginAttribute from RegexFilter @pluginfactory createFilter. (apache#3086)
1 parent c59fdd4 commit f0ec0b8

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

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

+43-34
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
*/
1717
package org.apache.logging.log4j.core.filter;
1818

19-
import java.lang.reflect.Field;
20-
import java.util.Arrays;
21-
import java.util.Comparator;
2219
import java.util.regex.Matcher;
2320
import java.util.regex.Pattern;
2421
import org.apache.logging.log4j.Level;
@@ -29,7 +26,6 @@
2926
import org.apache.logging.log4j.core.config.Node;
3027
import org.apache.logging.log4j.core.config.plugins.Plugin;
3128
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
32-
import org.apache.logging.log4j.core.config.plugins.PluginElement;
3329
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
3430
import org.apache.logging.log4j.message.Message;
3531
import org.apache.logging.log4j.message.MessageFormatMessage;
@@ -43,7 +39,6 @@
4339
@Plugin(name = "RegexFilter", category = Node.CATEGORY, elementType = Filter.ELEMENT_TYPE, printObject = true)
4440
public final class RegexFilter extends AbstractFilter {
4541

46-
private static final int DEFAULT_PATTERN_FLAGS = 0;
4742
private final Pattern pattern;
4843
private final boolean useRawMessage;
4944

@@ -110,10 +105,7 @@ private Result filter(final String msg) {
110105

111106
@Override
112107
public String toString() {
113-
final StringBuilder sb = new StringBuilder();
114-
sb.append("useRaw=").append(useRawMessage);
115-
sb.append(", pattern=").append(pattern.toString());
116-
return sb.toString();
108+
return "useRaw=" + useRawMessage + ", pattern=" + pattern.toString();
117109
}
118110

119111
/**
@@ -123,6 +115,40 @@ public String toString() {
123115
* The regular expression to match.
124116
* @param patternFlags
125117
* An array of Strings where each String is a {@link Pattern#compile(String, int)} compilation flag.
118+
* (no longer used - pattern flags can be embedded in regex-expression.
119+
* @param useRawMsg
120+
* 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.
121+
* @param match
122+
* The action to perform when a match occurs.
123+
* @param mismatch
124+
* The action to perform when a mismatch occurs.
125+
* @return The RegexFilter.
126+
* @throws IllegalAccessException When there is no access to the definition of the specified member.
127+
* @throws IllegalArgumentException When passed an illegal or inappropriate argument.
128+
* @deprecated use {@link #createFilter(String, Boolean, Result, Result)}
129+
*/
130+
@Deprecated
131+
// TODO Consider refactoring to use AbstractFilter.AbstractFilterBuilder
132+
public static RegexFilter createFilter(
133+
// @formatter:off
134+
@PluginAttribute("regex") final String regex,
135+
final String[] patternFlags,
136+
@PluginAttribute("useRawMsg") final Boolean useRawMsg,
137+
@PluginAttribute("onMatch") final Result match,
138+
@PluginAttribute("onMismatch") final Result mismatch)
139+
// @formatter:on
140+
throws IllegalArgumentException, IllegalAccessException {
141+
142+
// LOG4J-3086 - pattern-flags can be embedded in RegEx expression
143+
144+
return createFilter(regex, useRawMsg, match, mismatch);
145+
}
146+
147+
/**
148+
* Creates a Filter that matches a regular expression.
149+
*
150+
* @param regex
151+
* The regular expression to match.
126152
* @param useRawMsg
127153
* 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.
128154
* @param match
@@ -138,40 +164,23 @@ public String toString() {
138164
public static RegexFilter createFilter(
139165
// @formatter:off
140166
@PluginAttribute("regex") final String regex,
141-
@PluginElement("PatternFlags") final String[] patternFlags,
142167
@PluginAttribute("useRawMsg") final Boolean useRawMsg,
143168
@PluginAttribute("onMatch") final Result match,
144169
@PluginAttribute("onMismatch") final Result mismatch)
145170
// @formatter:on
146171
throws IllegalArgumentException, IllegalAccessException {
172+
boolean raw = Boolean.TRUE.equals(useRawMsg);
147173
if (regex == null) {
148174
LOGGER.error("A regular expression must be provided for RegexFilter");
149175
return null;
150176
}
151-
return new RegexFilter(
152-
Boolean.TRUE.equals(useRawMsg), Pattern.compile(regex, toPatternFlags(patternFlags)), match, mismatch);
153-
}
154-
155-
private static int toPatternFlags(final String[] patternFlags)
156-
throws IllegalArgumentException, IllegalAccessException {
157-
if (patternFlags == null || patternFlags.length == 0) {
158-
return DEFAULT_PATTERN_FLAGS;
159-
}
160-
final Field[] fields = Pattern.class.getDeclaredFields();
161-
final Comparator<Field> comparator = (f1, f2) -> f1.getName().compareTo(f2.getName());
162-
Arrays.sort(fields, comparator);
163-
final String[] fieldNames = new String[fields.length];
164-
for (int i = 0; i < fields.length; i++) {
165-
fieldNames[i] = fields[i].getName();
166-
}
167-
int flags = DEFAULT_PATTERN_FLAGS;
168-
for (final String test : patternFlags) {
169-
final int index = Arrays.binarySearch(fieldNames, test);
170-
if (index >= 0) {
171-
final Field field = fields[index];
172-
flags |= field.getInt(Pattern.class);
173-
}
177+
final Pattern pattern;
178+
try {
179+
pattern = Pattern.compile(regex);
180+
} catch (final Exception ex) {
181+
LOGGER.error("Unable to compile regular expression: {}", regex, ex);
182+
return null;
174183
}
175-
return flags;
184+
return new RegexFilter(raw, pattern, match, mismatch);
176185
}
177186
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="fixed">
6+
<issue id="3086" link="https://github.com/apache/logging-log4j2/issues/3086"/>
7+
<description format="asciidoc">
8+
Removed 'patternFlags' @PluginAttribute from RegexFilter @PluginFactory createFilter.
9+
</description>
10+
</entry>

0 commit comments

Comments
 (0)