

Integrating sending mail functionality with LogBack in Scala
source link: https://blog.knoldus.com/integrating-sending-mail-functionality-with-logback-in-scala/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Integrating sending mail functionality with LogBack in Scala
Reading Time: < 1 minute
Few days ago, I had the requirement that whenever there will be an exception in the application, we should get an email for this. Earlier, I was just logging the error log using LogBack :
xxxxxxxxxx
val logger: Logger = LoggerFactory.getLogger(this.getClass())
logger.error("................")
To do this, there are 2 ways :
1. Use the email sending code at every place wherever i have used logger.error.
2. Use the SMTPAppender of logback which sends email by itself whenever logger.error will be called.
So I tried with 2nd solution.
SMTPAppender :
We have to provide following settings for this in logback.xml :
xxxxxxxxxx
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>smtp.gmail.com</smtpHost>
<smtpPort>587</smtpPort>
<STARTTLS>true</STARTTLS>
<username>USERNAME</username>
<password>PASSWORD</password>
<asynchronousSending>false</asynchronousSending>
<to>EMAIL-IDS</to>
<from>NAME</from>
<subject>ERROR: %logger{20} - %m</subject>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date %-5level %logger - %message%n</pattern>
</layout>
</appender>
<root level="info">
<appender-ref ref="EMAIL" />
</root>
There is one important thing to be remember, email would work only for logger.error(). Other than this ,it would not be work
If you want to append one more logger then do as follows :
xxxxxxxxxx
<logger name="com.example.foo" level="debug">
<appender-ref ref="CONSOLE" /
</logger>
Code for this sample is present on the Knoldus Github
For more details, click here
Recommend
-
13
Sending mail without having to see your inbox For the longest time, the way I handled e-mail was with the "mail" program which came with the system. It was simple, fast (it never did "hourglass" or "beachball" garbage on me), and...
-
6
Sending mail functionality for Google App Engine Reading Time: < 1 minuteWhen we search...
-
10
Adding an Email sending functionality in Play using Scala Reading Time: < 1 minuteTo provide an Email Sending functionality in your Play App that is being built with Play 2.2.1, follow these steps ( this post...
-
5
Logging with Logback in Scala Written by ...
-
7
Knolx : Logging with Logback in Scala Reading Time: < 1 minuteIn this presentation, I have explained brief introduction about logback and its components. I also explained about configuration of logback.xml f...
-
14
CRUD operations with PostgreSQL using SLICK with LOGBACK EMAIL functionality. Reading Time: < 1 minuteThis article is a...
-
11
A Non-blocking "Email sending" functionality in Scala Reading Time: < 1 minuteIn our last blog “Adding a...
-
11
Failure send...
-
34
Sending Mail via Gmail using OAuth2 (2022 Edition) (Shallow Thoughts) Akkana's Musings on Open Source Computing and Technology, Science, and Nature.
-
9
go-mail - Easy to use, yet comprehensive library for sending mails with Go
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK