Search code examples
javalogginglog4jappender

Filter logger from one appender but not another


I have two appenders in my logging configuration. One of them sends E-Mail on ERROR Events.

A class, that I have no control over, spams ERROR messages. So I still want to have that messages but not in both appenders.

This is about my file (reduced to the stuff relevant here, afaics):

<appender name="Logfile">...</appender>
<appender name="sendMailOnError">...</appender>

<logger name="spammingClass">
    <level value="info"/>
</logger>

<root>
   <level value="debug"/>
   <appender-ref ref="Logfile"/>
   <appender-ref ref="sendMailOnError"/>
</root>

So, my guess is that I can somehow exclude spammingClass in sendMailOnError but I don't know how.

Btw. I use Java, but I would like to not write an own Filter class for this.


Solution

  • Yes, by specifying appenders for spammingClass and setting additivity to false:

    <logger name="spammingClass" additivity="false">
        <level value="info"/>
        <appender-ref ref="Logfile"/>
    </logger>