with SMTPAppender I receive only ERROR and not INFO type of log items

亡梦爱人 提交于 2019-11-30 05:21:41

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!