Press ESC to close

Log4j Gmail SmtpAppender, send log emails via Gmail on Errors !

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.

  1. protected Session createSession()

  2. 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 !

log4j error email in gmail inbox
Log4j error email body

Feedback

Let me know in case of issues.

Comments (9)

  • Anonymoussays:

    June 15, 2011 at 11:59 am

    Hello Abhinav,I want to use your appender in a Google App Engine application, but I want to use a Windows Live email ID to send/receive the log messages.Can I do this using your appender? Or are some changes required?Regards,Arvind.

  • Anonymoussays:

    June 15, 2011 at 6:21 pm

    Hey Arvind,I believe, you should give a first shot with default SMTP Appender, if it doesn't works, then try creating a clone of GMailSMTPAppender to address windows live specific issues.THanks,Abhinav

  • Anonymoussays:

    July 27, 2011 at 2:57 pm

    Hello Abhinav,I am trying to integrate your appender + log4j into a java web app.I copied the Gmail.java file into correct folder, and also added the dependencies for log4j and mail JAR files.However in Netbeans I am getting the following errors in Gmal.java–(1) For class GmailSMTPAppender– Duplicate class.(2) In code segment– “return new PasswordAuthentication(getSMTPUsername(),getSMTPPassword());” I am getting the following errors- Cannot find symbol:method getSMTPUsername and another error – Cannot find symbol:method getSMTPPasswordAre these errors occurring because I am using these in a web application? Or is it some other reason?Regards,Arvind.

  • Anonymoussays:

    July 27, 2011 at 6:03 pm

    These errors are coming because mail.jar is not included in class path PasswordAuthentication.java is part of mail.jar. Also, you have included/copied GmailSMTPAppender twice, so that might be reason for Duplicate class error.Hope this helps- Abhinav

  • Anonymoussays:

    October 11, 2011 at 7:01 am

    Hello when i used GmailSMTPAppender i am getting following error. log4j:ERROR Error occured while sending e-mail notification.javax.mail.MessagingException: Exception reading response; nested exception is: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target at com.sun.mail.smtp.SMTPTransport.readServerResponse(SMTPTransport.java:1611) at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1369) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:412) at javax.mail.Service.connect(Service.java:288) at javax.mail.Service.connect(Service.java:169) at com.dwp.cq.log4j.GmailSMTPAppender.send(GmailSMTPAppender.java:148) at com.dwp.cq.log4j.GmailSMTPAppender.sendBuffer(GmailSMTPAppender.java:131) at org.apache.log4j.net.SMTPAppender.append(SMTPAppender.java:256)Please help me on this

  • Anonymoussays:

    October 13, 2011 at 1:17 pm

    Hi Abhinav GuptaIs there some sort of port for this appender for log4php that you know of. I really want to use gmail with it and it doesn't have an SMTPAppender like log4j have. I hope you can give me ideas where to start.

Leave a Reply

%d bloggers like this: