Nicolas
Nicolas

Reputation: 23

RegexFilter log4j2

i am using log4j2. This is my xml:

 <Appenders>
    <Console name="STDOUT" target="SYSTEM_OUT"> 
        <RegexFilter regex=".*\\binsert\\b.*|.*\\bupdate\\b.*|.*\\bdelete\\b.*" onMatch="ACCEPT" onMismatch="DENY" />  
        <PatternLayout pattern="%mdc{usuario} - %m%n" />
    </Console>

   <RollingFile name="file-log" fileName="c:/logs/SIGE.log"
        filePattern="c:/logs/SIGE-%d{yyyy-MM-dd}.log">
        <PatternLayout>
            <pattern>[%-5level] [%mdc{usuario}] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t]  %c{1} - %m%n</pattern>
        </PatternLayout>
        <RegexFilter regex=".*\\binsert\\b.*|.*\\bupdate\\b.*|.*\\bdelete\\b.*" onMatch="ACCEPT" onMismatch="DENY" />  
        <Policies>
            <SizeBasedTriggeringPolicy />
        </Policies>
    </RollingFile>

</Appenders>
<Loggers>
    <logger name="org.hibernate.SQL" level="DEBUG" additivity="false">
        <AppenderRef ref="STDOUT" />
        <AppenderRef ref="file-log" />
    </logger>
    <logger name="org.hibernate.type" level="TRACE" additivity="false">
        <AppenderRef ref="STDOUT" />
        <AppenderRef ref="file-log" />
    </logger>       
</Loggers>

My idea is to filter to log only the update , insert and delete statements of hibernate. But with this it's not logging anything, if i comment the regexfilter line it logs perfectly. I am using the last hibernate release. Thanks in advance! Nicolás.

Upvotes: 2

Views: 5644

Answers (1)

Paul Vargas
Paul Vargas

Reputation: 42060

Since the regular expression is the value of an attribute of an XML element, you do not need to escape the \ character as in strings literals in the Java language, but only if the character is single quote (') or double quote ("). In which case, you should use &apos; and &quot;, respectively. 1

Therefore, your regular expression can be:

<RegexFilter regex=".*\b(insert|update|delete)\b.*" 
             onMatch="ACCEPT" 
             onMismatch="DENY"/>

Notes

  1. http://www.w3.org/TR/2008/REC-xml-20081126/#syntax

    To allow attribute values to contain both single and double quotes, the apostrophe or single-quote character (') may be represented as " &apos; ", and the double-quote character (") as " &quot; ".

Upvotes: 4

Related Questions