Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter log by matching pattern - log4j

I have the following layout patter in my log4j xml file:

"%d{ISO8601} %c %p [%t] [%x] 9.5.4.RC12 %m%n"

What I want is that when ever I get a log containing message process [proc#] completed, it should be skipped. I mean every log except the one containing this message should be printed. [proc#] will contain the process number of max lenght 4. What can I desing a filter with this function in my xml config file. If so, then how?

like image 619
Tariq Avatar asked Jan 23 '14 09:01

Tariq


1 Answers

ExpressionFilter can do that.

In a filter definition inside an appender definition, use an expression similar to (note LIKE is the regex match operator):

"MSG LIKE 'process \[.*\] completed'"

See ExpressionFilter javadoc here:

http://logging.apache.org/log4j/companions/apidocs/org/apache/log4j/filter/ExpressionFilter.html

Example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
    <appender name="CONSOLE">
        <param name="Target" value="System.out"/>
        <layout>
            <param name="ConversionPattern" value="%d %p [%c] - %m%n"/>
        </layout>
        <filter class="org.apache.log4j.filter.ExpressionFilter">
            <param name="expression" value="EXCEPTION ~= com.company.BackendNotAvailableException" />
            <param name="acceptOnMatch" value="false"/>
        </filter>
    </appender>
    <root>
        <priority value ="INFO" />
        <appender-ref ref="CONSOLE"/>
    </root>
</log4j:configuration>

Example taken from here: http://blog.trifork.com/2011/08/23/filtering-specific-exceptions-when-using-log4j/

like image 108
Scott Avatar answered Oct 26 '22 07:10

Scott