I am using the latest version of spring boot.
I have the below code in various classes.
LOGGER.info(" send email : " + EMAIL); // EMAIL="send_email"
I need to send emails whenever a log is generated with the keyword "send_email" I have below conditions to send an email
How can I achieve this using Spring boot?
Spring Boot uses logback as default. SLF4J is just a facade for several logging frameworks.
If you are using logback, you can customize the SMTPAppender to pass evaluator parameter to decide if the log message should be processed by SMTPAppender or not.
logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="ERROR_MAILER" class="ch.qos.logback.classic.net.SMTPAppender">
<evaluator class="app.Evaluator"> </evaluator>
<layout class="ch.qos.logback.classic.html.HTMLLayout"/>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
</appender>
<root level="INFO">
<appender-ref ref="ERROR_MAILER"/>
</root>
</configuration>
gt.app.Evaluator
package app;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.boolex.EventEvaluatorBase;
public class Evaluator extends EventEvaluatorBase<ILoggingEvent> {
@Override
public boolean evaluate(ILoggingEvent event) throws NullPointerException {
return event.getMessage().contains("THE_KEYWORD_HERE");
}
}
Reference: Source code of ch.qos.logback.core.net.SMTPAppenderBase
See also: ch.qos.logback.core.net.SMTPAppenderBase#checkEntryConditions