简体   繁体   中英

log4j smtp appender overriding gives unexpected behavior?

I have overridden log4j smtp appender for some purpose. The problem is if i keep <param name="Threshold" value="INFO"/> Then it would send mails properly. But if i keep <param name="Threshold" value="ERROR"/> Then no mails are sent. What is the problem? Please find my code.

public class MyAppender extends SMTPAppender{
  @Override
    protected void sendBuffer() {
          //Some code to format email body

    }

}

log4j.xml

<!-- Appenders -->



<appender name="mail" class="com.service.MyAppender">
    <param name="BufferSize" value="2"/>
    <param name="BufferedIO" value="true"/>
    <param name="Threshold" value="ERROR"/>
    <param name="SMTPHost" value="myhost"/>
    <param name="From" value="abc@co.com"/>
    <param name="To" value="def@ld.com"/>
    <param name="Subject" value="Testing Testing"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern"
               value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{2}:%L - %m%n"/>
    </layout>
</appender>


<appender name="ASYNC" class="org.apache.log4j.AsyncAppender">
    <param name="BufferSize" value="500"/>
    <appender-ref ref="mail"/>
</appender>


<root>
    <level value="ERROR" />
    <appender-ref ref="ASYNC" />
</root>

I looked into SMTPAppender source code and i believe below lines are the problem.

if(evaluator.isTriggeringEvent(event)) {
      sendBuffer();
    }

According to log4j documentation , all log events with value lower than Treshold will be filtered out (ie not sent).

It's possible that changing Treshold to more restrictive filters out all messages that are logged in your case. Make sure that you actually have events with log level ERROR (or higher - FATAL).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM