How to filter a specific exception from Log4j?
Asked Answered
T

3

9

I'm implementing some new features of Java 8 on my desktop app and Hibernate 3.6 seems to doesn't like it.

I added to an Interface a "default method", since then Hibernate it throwing:

2014-10-02 14:01:25,538 WARN entity.PojoEntityTuplizer  - could not create proxy factory for:modelo.ChequeTercero
org.hibernate.HibernateException: Javassist Enhancement failed: modelo.ChequeTercero
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.getProxyFactory(JavassistLazyInitializer.java:169)
at org.hibernate.proxy.pojo.javassist.JavassistProxyFactory.postInstantiate(JavassistProxyFactory.java:65)
Caused by: java.lang.VerifyError: (class: modelo/ChequeTercero_$$_javassist_45, method: _d21getNumeroValor signature: ()Ljava/lang/String;) Illegal use of nonvirtual function call

for EACH class that implement the interface multiplied for EACH default method in the interface.

As this log is a Level.WARN, it is generating biiiiiiiiiig log file on the users every time they open the application.

I tried to make some logger filters but is not working:

<filter class="org.hibernate.proxy.pojo.BasicLazyInitializer">
<param name="LevelMin" value="FATAL" />
<param name="LevelMax" value="FATAL" />
    </filter>
<filter class="org.apache.log4j.filter.ExpressionFilter">
        <param name="expression" value="EXCEPTION ~= org.hibernate.proxy.pojo.BasicLazyInitializer"/>
        <param name="acceptOnMatch" value="false"/>
    </filter>
    <filter class="org.apache.log4j.varia.StringMatchFilter">
        <param name="StringToMatch" value="org.hibernate.HibernateException: Javassist Enhancement failed"/>
        <param name="AcceptOnMatch" value="false" />
    </filter>
    <!--<filter class="org.apache.log4j.varia.DenyAllFilter"/> -->

What am I doing wrong? Also if I uncomment DenyAllFilter no log appears anymore.

Trichomoniasis answered 2/10, 2014 at 17:32 Comment(0)
T
10

I have found the solution - RegexFilter.

Here is the related Log4j 2.x configuration, that allows me to exclude specific messages both from log file and console appenders.

<Appenders>
    <Console name="console" target="System.out">
        <RegexFilter regex=".*(Javassist Enhancement failed|could not create proxy factory for).*" onMatch="DENY" onMismatch="ACCEPT"/>
        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-2p %c{1.} %x - %m%n"/> 
    </Console>
    <File name="logfile" filename="./app.log" append="true">
        <!--Evita cargar en el log el error que tira hibernate por Java 8 (default method en Interfaces)-->
        <RegexFilter regex=".*(Javassist Enhancement failed|could not create proxy factory for).*" onMatch="DENY" onMismatch="ACCEPT"/>
        <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p %c{-1} %X{userName} - %m%n"/> 
    </File>
</Appenders>
Trichomoniasis answered 27/2, 2018 at 10:59 Comment(0)
L
9

Using log4j 1.2 I am able to filter out specific exceptions, using an ExpressionFilter in the Appender.

<appender name="LOGFILE" class="com.me.RollingFileAppender">
    <param name="File" value="..." />
    ...
    <layout .../>
    <!-- Exclude expected exceptions -->
    <filter class="org.apache.log4j.filter.ExpressionFilter">
        <param name="Expression" value="EXCEPTION ~= 'String found in the exception message'"/>
        <param name="AcceptOnMatch" value="false"/>
    </filter>
</appender>

See http://blog.trifork.com/2011/08/23/filtering-specific-exceptions-when-using-log4j/

Letty answered 27/11, 2014 at 13:12 Comment(0)
W
0

For YAML ,please see/read below:

The accepted answer is using RegexFilter and is an elegant solution for managing log filtering in Log4j2.

While implementing, encountered a scenario where it was needed to exclude a specific Hibernate error from being logged. As in our projects we are working with a YAML configuration, thus found it valuable to share the approach here, especially since most available examples on the internet tend to focus on XML configuration.

Below is an example of how to employ the RegexFilter within a YAML Log4j2 configuration:

  Loggers:
    logger:
      - name: org.hibernate.orm.jdbc.batch
        level: ERROR
        Filters:
          # Ignore log events with 'org.hibernate.StaleStateException' exception
          - RegexFilter:
              regex: ".*org.hibernate.StaleStateException.*"
              onMatch: DENY
              onMismatch: NEUTRAL

In the above example, the intent is to filter out logs related to org.hibernate.StaleStateException, preventing them from being recorded. This is achieved while allowing all other error logs from the org.hibernate.orm.jdbc.batch logger to be captured.

Feel free to customize it further according to your preference.

Wheeled answered 15/8, 2023 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.