with SMTPAppender I receive only ERROR and not INF

2019-02-03 17:43发布

I've configured an SMTPAppender into my Java app.

<appender name="AdministratorEmail" class="org.apache.log4j.net.SMTPAppender">
  <param name="Threshold" value="info" />
  <param name="BufferSize" value="512" />
  <param name="SMTPHost" value="smtp.sss.intranet" />
  <param name="From" value="adminEbookMaker@sss.intranet" />
  <param name="To" value="user@sss.it" />
  <param name="Subject" value="errors" />       
  <layout class="org.apache.log4j.PatternLayout">
    <param name="ConversionPattern" value="[%d{ISO8601}]%n%n%-5p%n%n%c%n%n%m%n%n" />
  </layout>
  <filter class="org.apache.log4j.varia.LevelRangeFilter">
    <param name="LevelMin" value="info" />
    <param name="LevelMax" value="fatal" />
  </filter>
</appender>

I receive only the ERROR log type and not the INFO type! I changed the LevelMin but nothing, I inserted the Thresold, but nothing!

Does someone have any kind of suggestion?

1条回答
狗以群分
2楼-- · 2019-02-03 18:20

The SMTPAppender by design only logs ERROR and above messages. This level cannot be affected by properties. The documentation for the appended states:

By default, an email message will be sent when an ERROR or higher severity message is appended. The triggering criteria can be modified by setting the evaluatorClass property with the name of a class implementing TriggeringEventEvaluator, setting the evaluator property with an instance of TriggeringEventEvaluator or nesting a triggeringPolicy element where the specified class implements TriggeringEventEvaluator

See: Class SMTPAppender

The fact that you are seeing INFO messages only after the first ERROR is due to the bufferSize property which shows the 'n' most recent log lines before the error to give context to the error.

Further research on this shows that there is an implementation of the required interface for TriggerEventEvaluator in the 'extras companion'

This can be downloaded from: Apache Download Mirrors

If you include this in your project you can then add the following to your SMTPAppender definition in log4j.xml (note the properties format is not supported!)

  <appender name="SMTP" class="org.apache.log4j.net.SMTPAppender">
...
    <triggeringPolicy  class="org.apache.log4j.rolling.FilterBasedTriggeringPolicy">
      <filter class="org.apache.log4j.filter.LevelRangeFilter">
        <param name="levelMin" value="INFO" />
      </filter>
    </triggeringPolicy>
...
  </appender>
查看更多
登录 后发表回答