Skip to content

Commit e8bbd1d

Browse files
committed
Removed 'patternFlags' @PluginAttribute from RegexFilter @pluginfactory createFilter. (apache#3086) 8d05a7
1 parent be94cd1 commit e8bbd1d

File tree

2 files changed

+63
-38
lines changed

2 files changed

+63
-38
lines changed

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

+53-38
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
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;
2522
import org.apache.logging.log4j.Marker;
2623
import org.apache.logging.log4j.core.Filter;
2724
import org.apache.logging.log4j.core.LogEvent;
2825
import org.apache.logging.log4j.core.Logger;
26+
import org.apache.logging.log4j.core.config.Node;
27+
import org.apache.logging.log4j.core.config.plugins.Plugin;
28+
import org.apache.logging.log4j.core.config.plugins.PluginAttribute;
29+
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
2930
import org.apache.logging.log4j.message.Message;
3031
import org.apache.logging.log4j.message.ParameterizedMessage;
3132
import org.apache.logging.log4j.plugins.Configurable;
@@ -45,7 +46,6 @@
4546
@Plugin
4647
public final class RegexFilter extends AbstractFilter {
4748

48-
private static final int DEFAULT_PATTERN_FLAGS = 0;
4949
private final Pattern pattern;
5050
private final boolean useRawMessage;
5151

@@ -101,10 +101,7 @@ private Result filter(final String msg) {
101101

102102
@Override
103103
public String toString() {
104-
final StringBuilder sb = new StringBuilder();
105-
sb.append("useRaw=").append(useRawMessage);
106-
sb.append(", pattern=").append(pattern.toString());
107-
return sb.toString();
104+
return "useRaw=" + useRawMessage + ", pattern=" + pattern.toString();
108105
}
109106

110107
/**
@@ -114,54 +111,72 @@ public String toString() {
114111
* The regular expression to match.
115112
* @param patternFlags
116113
* An array of Strings where each String is a {@link Pattern#compile(String, int)} compilation flag.
114+
* (no longer used - pattern flags can be embedded in regex-expression.
117115
* @param useRawMsg
118116
* If true, the raw message will be used, otherwise the formatted message will be used.
119117
* @param onMatch
120118
* The action to perform when a match occurs.
121119
* @param onMismatch
122120
* The action to perform when a mismatch occurs.
123121
* @return The RegexFilter.
124-
* @throws IllegalAccessException
125-
* @throws IllegalArgumentException
122+
* @throws IllegalAccessException When there is no access to the definition of the specified member.
123+
* @throws IllegalArgumentException When passed an illegal or inappropriate argument.
124+
* @deprecated use {@link #createFilter(String, Boolean, Result, Result)}
125+
*/
126+
@Deprecated
127+
// TODO Consider refactoring to use AbstractFilter.AbstractFilterBuilder
128+
public static RegexFilter createFilter(
129+
// @formatter:off
130+
@PluginAttribute("regex") final String regex,
131+
final String[] patternFlags,
132+
@PluginAttribute("useRawMsg") final Boolean useRawMsg,
133+
@PluginAttribute("onMatch") final Result match,
134+
@PluginAttribute("onMismatch") final Result mismatch)
135+
// @formatter:on
136+
throws IllegalArgumentException, IllegalAccessException {
137+
138+
// LOG4J-3086 - pattern-flags can be embedded in RegEx expression
139+
140+
return createFilter(regex, useRawMsg, match, mismatch);
141+
}
142+
143+
/**
144+
* Creates a Filter that matches a regular expression.
145+
*
146+
* @param regex
147+
* The regular expression to match.
148+
* @param useRawMsg
149+
* 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.
150+
* @param match
151+
* The action to perform when a match occurs.
152+
* @param mismatch
153+
* The action to perform when a mismatch occurs.
154+
* @return The RegexFilter.
155+
* @throws IllegalAccessException When there is no access to the definition of the specified member.
156+
* @throws IllegalArgumentException When passed an illegal or inappropriate argument.
126157
*/
127158
// TODO Consider refactoring to use AbstractFilter.AbstractFilterBuilder
128159
@PluginFactory
129160
public static RegexFilter createFilter(
130161
// @formatter:off
131-
@PluginAttribute final String regex,
132-
@PluginElement final String[] patternFlags,
133-
@PluginAttribute final Boolean useRawMsg,
134-
@PluginAttribute final Result onMatch,
135-
@PluginAttribute final Result onMismatch)
162+
@PluginAttribute("regex") final String regex,
163+
@PluginAttribute("useRawMsg") final Boolean useRawMsg,
164+
@PluginAttribute("onMatch") final Result match,
165+
@PluginAttribute("onMismatch") final Result mismatch)
136166
// @formatter:on
137167
throws IllegalArgumentException, IllegalAccessException {
168+
boolean raw = Boolean.TRUE.equals(useRawMsg);
138169
if (regex == null) {
139170
LOGGER.error("A regular expression must be provided for RegexFilter");
140171
return null;
141172
}
142-
return new RegexFilter(useRawMsg, Pattern.compile(regex, toPatternFlags(patternFlags)), onMatch, onMismatch);
143-
}
144-
145-
private static int toPatternFlags(final String[] patternFlags)
146-
throws IllegalArgumentException, IllegalAccessException {
147-
if (patternFlags == null || patternFlags.length == 0) {
148-
return DEFAULT_PATTERN_FLAGS;
149-
}
150-
final Field[] fields = Pattern.class.getDeclaredFields();
151-
final Comparator<Field> comparator = (f1, f2) -> f1.getName().compareTo(f2.getName());
152-
Arrays.sort(fields, comparator);
153-
final String[] fieldNames = new String[fields.length];
154-
for (int i = 0; i < fields.length; i++) {
155-
fieldNames[i] = fields[i].getName();
156-
}
157-
int flags = DEFAULT_PATTERN_FLAGS;
158-
for (final String test : patternFlags) {
159-
final int index = Arrays.binarySearch(fieldNames, test);
160-
if (index >= 0) {
161-
final Field field = fields[index];
162-
flags |= field.getInt(Pattern.class);
163-
}
173+
final Pattern pattern;
174+
try {
175+
pattern = Pattern.compile(regex);
176+
} catch (final Exception ex) {
177+
LOGGER.error("Unable to compile regular expression: {}", regex, ex);
178+
return null;
164179
}
165-
return flags;
180+
return new RegexFilter(raw, pattern, match, mismatch);
166181
}
167182
}
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)