Log4j2

Log4J Configuration

The following is true at least for MuleSoft Anypoint Java envrionment or at least to version of Log4j used by MuleSoft.

MuleRuntime: 4.2.1

Log4J <Filters> element representing “CompositeFilter” class in source code and frequently referenced as “Composite Filter” does not work as documented under references.

For example, it is documented to be usable under a specific appender like “RollingFile” appender, but it does not actually do so. For example, if I list something like this

<Appenders>
    <RollingFile name="RollingFile" fileName="logs/app.log">
        <Filters>
            <RegexFilter regex="match_1" onMatch="DENY" onMismatch="ACCEPT" />
            <RegexFilter regex="match_2" onMatch="DENY" onMismatch="ACCEPT" />
        </Filters>
    </RollingFile>
</Appenders>

Only line”RegexFilter” line #1 will work and line #2 will always be skipped. It is easy to prove if one swaps line #1 and #2.

Thus, there is no point for using “” The only way to make it work is to match both expressions in one statement:

    <Appenders>
        <RollingFile name="RollingFile" fileName="logs/app.log">
            <RegexFilter regex="match_1|match_2" onMatch="DENY" onMismatch="ACCEPT" />
        </RollingFile>
    </Appenders>

Obviously, there are limits to this approach.

“NEUTRAL” option is not very well documented. Perhaps, the following solution would work with multi-line (not tested). The assumption is that NEUTRAL meaning is a pass through to the next.

<Appenders>
    <RollingFile name="RollingFile" fileName="logs/app.log">
        <Filters>
            <RegexFilter regex="match_1" onMatch="DENY" onMismatch="ACCEPT" />
            <RegexFilter regex="match_2" onMatch="DENY" onMismatch="ACCEPT" />
        </Filters>
    </RollingFile>
</Appenders>

Filters in Global Scope

Don’t work quite as expected either as these filters seem to replace there rest of the configuration:

<Configuration status="warn" name="MyApp" packages="">
    <Filters>
        <RegexFilter regex="match_1" onMatch="DENY" onMismatch="ACCEPT" />
        <RegexFilter regex="match_2" onMatch="DENY" onMismatch="ACCEPT" />
    </Filters>
</Configuration>

The effect is such that’s if only WARN and above was permitted to log at root level, then this limit will be not taken into the effect and instead filters will be applied to everything.