Log4j gives SMTPAppender for sending emails on logging. This SMTP Appender works good in most of the cases, except TLS/SSL SMTP setups. Gmail works on the same SSL/TLS setup, so any one who wants to configure the default log4j SMTPAppender with Gmail will be troubled with strange Java Mail errors.
Though this problem can be easily solved by writing a Log4j Custom SMTP Appender.
“GmailSMTPAppender“ - Custom SMTPAppender for GMAIL
“GmailSMTPAppender” is the solution to the default log4j SMTPAppender SSL/TLS issue. GmailSMTPAppender is basically an extension to existing log4j SMTPAppender. It just overrides two methods from SMTPAppender i.e.
- protected Session createSession()
- protected void sendBuffer()
So by this inheritance GmailSMTPAppender utilizes the log4j’s SMTPAppender for most of the logic and specializes few methods for Gmail specific handling.
How to Install GmailSmtpAppender in my project ?
GmailSMTPAppender is open source, here is the project landing page [link]. Following are the steps to install this appender to your project.
- Checkout the project source from here. This source is checking of a full Eclipse “Java project”.
- Copy GmailSMTPAppender.java to your source folder.
- Only dependencies are log4j-1.2.16.jar and java mail.jar. You can replace them with latest version if required or use your existing ones.
- Copy and replace log4j.properties available here as required. You have to change the usual SMTPAppender settings for "log4j.appender.EMAIL". This is explained in detail below.
- Thats it, all done !!. Use Logger.error() to send email notifications.
What about Google APPS Account ?
GmailSmtpAppender works fine with both normal Gmail accounts and Google apps account. The same SMTP Host will go for Google apps accounts. So just update the SMTPUser/Pass with From/To addresses and you are done.
Source Code & DEPENDENCIES ?
The complete code + dependencies is hosted on GoogleCode. You can checkout the code from here.
Setting up log4j.properties
Here is the sample log4j.properties. You can change or copy stuff like appenders from it as required. GmailSMTPAppender has not exposed any new appender settings. So just use your usual SMTPAppender settings and they will work fine with GmailSMTPAppender too.
#@author abhinav@tgerm.com #Google Code Project http://code.google.com/p/log4j-gmail-smtp-appender/ #My Blog : http://www.tgerm.com log4j.rootLogger=ERROR, Console,R log4j.logger.com.tgerm=DEBUG, Console, R, EMAIL log4j.additivity.com.tgerm=false log4j.appender.Console=org.apache.log4j.ConsoleAppender log4j.appender.Console.layout=org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=appender.log log4j.appender.R.Append=false log4j.appender.R.MaxFileSize=100KB log4j.appender.R.MaxBackupIndex=1 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n log4j.appender.EMAIL=com.tgerm.log4j.appender.GmailSMTPAppender log4j.appender.EMAIL.SMTPHost=smtp.gmail.com # Turn off debugging if not required log4j.appender.EMAIL.SMTPDebug=true log4j.appender.EMAIL.From=from@gmail.com log4j.appender.EMAIL.To=to@tgerm.com log4j.appender.EMAIL.SMTPUsername=smtpuser@gmail.com log4j.appender.EMAIL.SMTPPassword=somepass log4j.appender.EMAIL.Subject=Email Notification from Gmail SMTP Appender log4j.appender.EMAIL.cc=cc@gmail.com log4j.appender.EMAIL.layout=org.apache.log4j.PatternLayout log4j.appender.EMAIL.layout.ConversionPattern=%p %t %c - %m%n log4j.appender.EMAIL.BufferSize=1
Sample ERROR Emails !


Feedback
Let me know in case of issues.