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 your help with the configuration.
I have a Spring boot application where some processing is done:
private void foo() {
try {
// do something
} catch (Exception e) {
log.error("Logging my exception");
}
}
I would like to trigger an email whenever this error is logged. I followed some tutorials and I added logback.xml and smtp-appender.xml to my resources
directory:
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml" />
<include resource="smtp-appender.xml" />
<logger name="com.mycompany" level="DEBUG">
<appender-ref ref="SMTP" />
</logger>
</configuration>
smtp-appender.xml
<?xml version="1.0" encoding="UTF-8"?>
<included>
<appender name="SMTP" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>xxx</smtpHost>
<username>user</username>
<password>password</password>
<to>[email protected]</to>
<from>[email protected]</from>
<subject>testError</subject>
<layout class="ch.qos.logback.classic.html.HTMLLayout" />
</appender>
</included>
I have setup local smtpserver on localhost via sendmail
. I have also tried with AWS smtp server and Gmail. None of these services worked for me so far.
The problem is that I do not receive any email after the exception and there is literally no output corresponding to the logback (besides the log.error()
output, of course) which makes me think that the application does not even know about these config files...
These are my maven dependencies in pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<relativePath />
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20141113</version>
</dependency>
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
Any help would be highly appreciated.
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>[email protected]</username>
<password>YOUR_GMAIL_xPASSWORD</password>
<to>EMAIL-DESTINATION</to>
<to>ANOTHER_EMAIL_DESTINATION</to> <!-- additional destinations are possible -->
<from>[email protected]</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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With