9

Here's a snippet containing my Logback SMTPAppender:

<appender name="logManager-smtpAppender" class="ch.qos.logback.classic.net.SMTPAppender">
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>WARN</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>NEUTRAL</onMismatch>
    </filter>
    <filter class="ch.qos.logback.classic.filter.LevelFilter">
        <level>ERROR</level>
        <onMatch>ACCEPT</onMatch>
        <onMismatch>DENY</onMismatch>
    </filter>

    <asynchronousSending>false</asynchronousSending>

    <smtpHost>my.smtp.host</smtpHost>
    <to>john.smith@example.com</to>
    <from>no-reply@example.com</from>
    <username>my_smtp_user</username>
    <password>my_smtp_password</password>
    <subject>%logger{20} - %m</subject>
    <layout class="ch.qos.logback.classic.html.HTMLLayout"/>
    <cyclicBufferTracker class="ch.qos.logback.core.spi.CyclicBufferTracker">
        <bufferSize>1</bufferSize>
    </cyclicBufferTracker>
</appender>

When the following Java executes:

logger.warn("This is a warning.");
logger.error("This is an error.");

I only get 1 email. By setting bufferSize to 1 I would have expected to get 2 different emails with 1 single log message in each of them. What's going on?

3 Answers3

8

As Ceki has already mentioned that SMTPAPpender trigges emails on events of level ERROR. To get all your logs in one mail, You can increase the buffersize

10

Here by increasing bufferSize to 10, You will get last 10 messages logged by your error.

Check below link : https://github.com/abdulwaheed18/Slf4jTutorial/blob/master/src/com/waheed/tutorial8/Application8.java https://github.com/abdulwaheed18/Slf4jTutorial/blob/master/sample8.xml

Waheed
  • 1,835
  • 15
  • 21
  • Thanks @Waheed (and +1 for the code samples). One thing I'm still not getting though is: what happens if I increase my bufferSize to 10, and then log, say, 12 ERROR messages? Will I get 1 email with 10 log messages? If so, what happens to the last 2 messages (10 + 2 = 12)? Thanks again! –  May 30 '13 at 10:16
  • 1
    Yes, If you set bufferSize value as 10, you will only get last 10 messages. Mostly we use SMTPAppender to notify admin/Ops that something has gone wrong in your Application. So as soon as it encountered ERROR log, it will send you a mail with numbers of logs you have set. Per ERROR log you will get one mail. – Waheed May 30 '13 at 11:33
  • Thanks again (and +1!) I'm sorry though, not trying to be a pest here, but I'm still not understanding what would happen to the last 2 log messages. Say I have code that fires 12 `logger.error("...")` messages in a row, and `bufferSize` is set to 10. Like you said, I would get 1 email with 10 ERROR messages in it. **But what happens to the last 2 ERROR logs?** Thanks again for all your help so far! –  May 30 '13 at 12:03
  • I guess you are confused here. As I said per ERROR message you will get one mail. So for 12 errors, You will get 12 mails in your inbox. As soon as first logger.error encountered it will send one mail. On second error log you will get another mail. bufferSize set to 10 means the 10 messages get logged before error message. – Waheed May 30 '13 at 12:27
5

Triggering of outgoing email is computed by an "evaluator". By default SMTPAppender comes with OnErrorEvaluator which triggers emails on events of level ERROR or higher. Thus, by default SMTPAppender will send out an email on the second message (of level ERROR) and not the first (WARN). To trigger outgoing messages on WARN, write your own evaluator. Here is the code:

public class OnWarnEvaluator extends EventEvaluatorBase<ILoggingEvent> {

 public boolean evaluate(ILoggingEvent event) throws NullPointerException,
           EvaluationException {
   return event.getLevel().levelInt >= Level.WARN_INT;
 }
}
Ceki
  • 26,753
  • 7
  • 62
  • 71
  • Is it possible to describe/configure such an evaluator or sg. equivalent declaratively in `log4j.xml` (for Log4J 2.1)? I would like to get one e-mail per INFO and ERROR log message each. – Drux Nov 29 '14 at 19:30
1

To trigger outgoing messages at WARN without adding new source files, you could add the Janino dependency and its evaluator:

<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">

    <evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
        <expression>return level >= WARN;</expression>
    </evaluator>
    ...
</appender>
dave thompson
  • 133
  • 3
  • 9