I am trying to configure Logback to send emails whenever an exception (Logging level: ERROR) occurs. I have not been able to make it work so far so I would like to ask for y
You have to enable Marker based triggering of email.
include an evaluator in your logback.xml under SMTP appender refer below link
http://logback.qos.ch/manual/appenders.html#OnMarkerEvaluator
then add below logic in your Exception catch block,pass Marker object to logger method if you want to send email like below -
Marker notifyAdmin = MarkerFactory.getMarker("NOTIFY_ADMIN");
logger.error(notifyAdmin,
"This is a serious an error requiring the admin's attention",
new Except
ion("Just testing"));
its possible that in some scenario you dont want to send email and only want to log the exception then just dont pass Marker object to logger method.
logger.error("This is a serious an error requiring the admin's attention",
new Except
ion("Just testing"));
Pls include following (from my working project) on pom.xml & logback.xml in order to get error mail :-
pom.xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jcl-over-slf4j</artifactId>
<version>1.6.4</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="errMailer" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>xxx</smtpHost>
<smtpPort>25</smtpPort>
<from>me@gmail.com</from>
<to>you@gmail.com</to>
<subjectStr>App Err Mail</subjectStr>
<layout class="ch.qos.logback.classic.html.HTMLLayout"/>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>ERROR</level>
</filter>
</appender>
<root level="ERROR">
<appender-ref ref="errMailer" />
</root>
</configuration>
Default entry for port in smtpappender is 25, gmail is using authentication for smtp services, and you need use ports 587 for TLS and 465 for SSL.
I would say this might be your problem.
Try to setup mail server on your local machine first without authentication, and try is your appended works. then try to find how to configure it for gmail.
From page http://logback.qos.ch/manual/appenders.html#gmailSTARTTLS
<configuration>
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>smtp.gmail.com</smtpHost>
<smtpPort>587</smtpPort>
<STARTTLS>true</STARTTLS>
<username>YOUR_USERNAME@gmail.com</username>
<password>YOUR_GMAIL_xPASSWORD</password>
<to>EMAIL-DESTINATION</to>
<to>ANOTHER_EMAIL_DESTINATION</to> <!-- additional destinations are possible -->
<from>YOUR_USERNAME@gmail.com</from>
<subject>TESTING: %logger{20} - %m</subject>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date %-5level %logger - %message%n</pattern>
</layout>
</appender>
<root level="DEBUG">
<appender-ref ref="EMAIL" />
</root>
</configuration>