Max
Max

Reputation: 1

Personalize RegexFilter in log4j2 for an xml file

despite the various posts found with the various solutions I can't solve a problem. I am using Eclipse for a project and I would like only logs containing a certain expression / word to appear in a given log file. For the configuration of log4j2 I am using an xml file appenders files are different

below the xml file

<Properties>
    <Property name="path">./LOGS</Property>
</Properties>



<Appenders>    
    <Console name="Console" target="SYSTEM_OUT">
          <PatternLayout>
            <pattern> %d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n </pattern>
         </PatternLayout>
    </Console>
     
 
    <RollingFile     name="Log"
                     fileName="${path}/log.txt" immediateFlush="false"   append="true"
                     filePattern="${path}/log-%d{yyyy-MM-dd}-%i.txt">
                     fileIndex = min 
        <PatternLayout>
            <pattern> %d{yyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n </pattern>
         </PatternLayout>
                    
        <Policies>
            <OnStartupTriggeringPolicy />
            <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            <SizeBasedTriggeringPolicy size="10 MB" />
        </Policies>
        <DefaultRolloverStrategy max="40" />
    </RollingFile>
    
    
    <RollingFile     name="LogScarti"
                     fileName="${path}/log_scarti.txt" immediateFlush="false"   append="true"
                     filePattern="${path}/log_scarti-%d{yyyy-MM-dd}-%i.txt">
        <PatternLayout>
            <pattern> %d{yyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n </pattern>
         </PatternLayout>
                    
        <Policies>
            <OnStartupTriggeringPolicy />
            <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            <SizeBasedTriggeringPolicy size="10 MB" />
        </Policies>
        <DefaultRolloverStrategy max="40" />
    </RollingFile>
    
    
    <RollingFile     name="Performance"
                     fileName="${path}/Performance.txt" immediateFlush="false"   append="true"
                     filePattern="${path}/Performance-%d{yyyy-MM-dd}-%i.txt">
        <Filters>            
        <RegexFilter regex=".*common.utils.PerformanceMonitor.*" onMatch="DENY" onMismatch="ACCEPT"/>
        </Filters>
        <PatternLayout>
            <pattern> %d{yyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n </pattern>
         </PatternLayout>
                    
        <Policies>
            <OnStartupTriggeringPolicy />
            <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            <SizeBasedTriggeringPolicy size="10 MB" />
        </Policies>
        <DefaultRolloverStrategy max="40" />
    </RollingFile>
         

</Appenders>

<Loggers>
    <Root level="info">
        <AppenderRef ref="Console"/>
        <AppenderRef ref="Log" level="Info"/>
        <AppenderRef ref="LogScarti" level="Error" />
        <AppenderRef ref="Performance" level="Info" /> 
    </Root>
</Loggers>

Nothing is recorded in the Performance file and if I play with the accept / deny parameters, it writes everything to me as in the logs file. I hope I have correctly illustrated the problem thanks to those who will dedicate even just a minute of their time

Upvotes: 0

Views: 630

Answers (1)

Piotr P. Karwasz
Piotr P. Karwasz

Reputation: 16045

The RegexFilter only filters the content of the log message (the %m pattern converter), whereas you are apparently trying to filter by logger name.

To filter by logger name you just need to attach your appender to the correct logger configuration:

<Loggers>
    <Root level="info">
        <AppenderRef ref="Console"/>
        <AppenderRef ref="Log" level="Info"/>
        <AppenderRef ref="LogScarti" level="Error" />
    </Root>
    <Logger name="com.accenture.etl.bollini.common.utils.PerformanceMonitor"
            level=info>
        <AppenderRef ref="Performance" />
    </Logger>
</Loggers>

Upvotes: 1

Related Questions