Recently I automated a java application that runs everyday at certain times. The java application was running as Docker container on AWS ECS.
Since the java application was communicating with 3party service through REST API, there was a chance that an error might happen. I needed to be always up to date and receive emails on my Gmail once something fails to execute.
Since I was using logback, it was fairly simple to implement email alerts using SMTPAppender. However, it did not work out as it was described in the documentation.
In this post I am going to share with you how easy to setup logback in order to receive automatic emails using your Gmail account.
What I’m trying to accomplish
- Setup: Integrating sending mail functionality with LogBack
- Make it work
- Extra security step
1. Setup
To achieve this, there are 2 ways :
- Use the email sending code at every place wherever i have used logger.error.
- Use the SMTPAppender of logback which sends email by itself whenever logger.error will be called.
Since I have many places where logger.error
is used obviously the second way is better.
Add the following appender to your logback file.
Note: Don’t forget to add the appender to the root element.
2. Make it work
According to the documentation, this is supposed to work. Well not entirely true. since Google are tight on their security, the email will never reach your inbox.
Since I am using a Gmail account to send emails, Google flag it as non secure App. Therefore my application was not able to send any email.
The answer to solve this issue is by simply changing your account parameters. according to this documentation.
Note: if you are using your own email serve, most likely you will not run into this issue(depending on your settings).
3. Security
Well I don’t feel comfortable putting my email’s password in the xml configuration file without any encryption.
That’s why I have used git-crypt tool to encrypt my logback.xml file using gpg key.
You can refer to git-crypt project to read more about how to use it.
In order to crypt your logback.xml
file which contains secerts. add the following line to .gitattributes
file. create the file in case does not exists.
Run the following commands to encrypt your file, before committing your changes.
Comments