首页 > 解决方案 > Log4j RegexFilter,如何配置 PatternFlags?

问题描述

我想使用 RegexFilter ( https://logging.apache.org/log4j/2.x/manual/filters.html#RegexFilter ) 来过滤 URL。但为了做到这一点,我需要用Pattern.DOTALL标志配置正则表达式模式。如何配置 RegexFilter 以使用此标志构造他的模式?我试过这个:

<RegexFilter regex=".*https://[-A-Za-z0-9+&amp;@#/%?=~_()|!:,.;]*[-A-Za-z0-9+&amp;@#/%=~_()|].*" onMatch="DENY" onMismatch="ACCEPT" PatternFlags="DOTALL"/> 

但是PatternFlags被忽略了。

查看 RegexFilter 代码,它确实读取了 PatternFlags 属性:

   @PluginFactory
    public static RegexFilter createFilter(@PluginAttribute("regex") String regex, @PluginElement("PatternFlags") String[] patternFlags, @PluginAttribute("useRawMsg") Boolean useRawMsg, @PluginAttribute("onMatch") Result match, @PluginAttribute("onMismatch") Result mismatch) throws IllegalArgumentException, IllegalAccessException {
        if (regex == null) {
            LOGGER.error("A regular expression must be provided for RegexFilter");
            return null;
        } else {
            return new RegexFilter(useRawMsg.booleanValue(), Pattern.compile(regex, toPatternFlags(patternFlags)), match, mismatch);
        }
    }

标签: javaregexlog4jlog4j2

解决方案


推荐阅读