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.