Is there a way to configure the SMTPAppender in LogBack to meet the following criteria?
- Group all the exceptions into one message
- Only send the daily log report if exceptions occurred
- Send the report only once, grouped in one email, at a particular time of the day.
My current implementation is far from doing the above, but currently it sends 3 emails when an exception occurs - the exception message, the stacktrace, and a flush of the buffer.
<!-- Filter duplicate Log Messages - Very important for Email Reports -->
<turboFilter class="ch.qos.logback.classic.turbo.DuplicateMessageFilter">
<AllowedRepetitions>1</AllowedRepetitions>
<CacheSize>1000</CacheSize>
</turboFilter>
<!--
############################################################
BASIC APPENDER
############################################################
-->
<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>%d{HH:mm:ss.SSS} %-55(%X{user} %level [%thread] %logger{20}) - %msg%n</pattern>
</encoder>
</appender>
<!--
############################################################
EMAIL APPENDER
############################################################
-->
<statusListener class="ch.qos.logback.core.status.OnConsoleStatusListener" />
<appender name="Email" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>SERVER</smtpHost>
<smtpPort>PORT</smtpPort>
<asynchronousSending>false</asynchronousSending>
<from>SENDER</from>
<to>RECIPIENT</to>
<subject>SUBJECT</subject>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{HH:mm:ss.SSS} %-55(%X{user} %level [%thread] %logger{20}) - %msg%n</pattern>
</layout>
</appender>
<!--
############################################################
OTHER
############################################################
-->
<root level="INFO">
<appender-ref ref="Console"/>
<appender-ref ref="RollingFile"/>
<appender-ref ref="Email"/>
</root>
One simple solution is to log those errors to a file and have a script on your server/machine that reads the file once a day and sends an email.
If you want to use an appender, it seems to me that you would need to roll your own as I don't think the standard
SMTPAppenderenables you to send emails once a day:SMTPAppendersendBuffermethod that is inSMTPAppenderBaseso that it simply adds the log message to a collectionScheduledExecutorServiceto your appender that runs asendEmailmethod once a daysendEmailmethod would synchronize onthisfor thread safety, check if the collection is empty, send an email with all the errors and clear the collectionA basic implementation could look like the class below (I have not tested it - I'm using Java 8 syntax but you can replace it by anonymous classes if required). Note that I only keep the event that caused the exception, you may also want to keep the content of the CyclicBuffer in the sendBuffer method and/or add some error separators between errors in the sendEmail method. This can become quite complex and is to be fine-tuned depending on your requirements.
Your logback configuration file then looks like:
To go further, you could add parameters, such as the time of the day when this is sent, the number of emails per day etc.