0
votes

I want to log the message containing "Here is DEBUG" my log4j2.xml is like this:

<Appenders>
    <!-- Console Appender -->
<Console name="console" target="SYSTEM_OUT">
    <PatternLayout pattern="%-5p %c{1} - %m%n" />
</Console>
    <!--RollingFile Appender-->
<RollingFile name="rollingFile"  fileName="${sys:catalina.base}/logs/${project.name}.log" filePattern="${sys:catalina.base}/logs/${project.name}-%i.log">
        <PatternLayout>
            <Pattern>%p %d{dd-MMMMMMMMM-yyyy HH:mm:ss:SSS} %m %n%n</Pattern>
        </PatternLayout>
        <Policies>
            <SizeBasedTriggeringPolicy size="500kb" />
        </Policies>
        <DefaultRolloverStrategy max="5" />
</RollingFile>
<filter class="org.apache.log4j.varia.StringMatchFilter">
        <param name="StringToMatch" value="Here is DEBUG" />
        <param name="AcceptOnMatch" value="true" />
</filter>
</Appenders>
<Loggers>
    <Root level="debug">
        <!--AppenderRef ref="console" /-->
        <AppenderRef ref="rollingFile" />
    </Root>
</Loggers>

and my java code is : log.info("Here is DEBUG"); log.info("XXXXXXXXXXXX"); log.warn("this is a warning"); log.error("this is an error"); But I still got everything in the log.

1
Maybe because I am using log4j2 and the configuration I have now is log4j 1? - user3707157

1 Answers

0
votes

OK I figured it out. log4j 1 and 2 are very different on the configuration. in 2, the filter should look like

<RegexFilter regex="DEBUG .*" onMatch="ACCEPT" onMismatch="DENY"/>