7

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?

robob
  • 1,739
  • 4
  • 26
  • 44
  • I found the problem...it wasn't a problem :-) The trigger of the entire buffer of error is the FIRST error send. So for default you'll not see any messages in your mail until there'll be an error. – robob Apr 08 '11 at 09:02

1 Answers1

17

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>
epsilon1024
  • 186
  • 2
  • 5
  • This was a great help. The same configuration works for Gmail too if [Log4j 1.2.16 or above is used](http://geekswithblogs.net/scarpenter/archive/2010/11/26/gmail-logging-with-log4j-1.2.16.aspx). No need for a customized appender for Gmail. – Ziska Jan 31 '13 at 23:59
  • Remember to include `` tag in right order, just to avoid log4j warnings: `The content of element type "appender" must match "errorHandler?,param*,rollingPolicy?,triggeringPolicy?,connectionSource?,layout?,filter*,appender-ref*)".` – metyl Sep 02 '14 at 15:19