I enabled logging in my application, and I want to send logs error by email (gmail account). I:
- Set up a java project
- add activation.jar , log4j.java and mail.jar (java mail)
- I added those libraries to the project class path
- I added log4j.properties and I configured it like this :
log4j.rootLogger= mainlogger, Email, dest log4j.appender.mainlogger=org.apache.log4j.ConsoleAppender log4j.appender.mainlogger.target=System.out log4j.appender.mainlogger.layout=org.apache.log4j.PatternLayout log4j.appender.mainlogger.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %p %-4r [%t] %-5p %c %x - %m%n log4j.appender.dest=org.apache.log4j.FileAppender log4j.appender.dest.File=log.log log4j.appender.dest.layout=org.apache.log4j.PatternLayout log4j.appender.dest.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %p %-4r [%t] %-5p %c %x - %m%n
Configuring the SMTP appender
log4j.appender.Email=org.apache.log4j.net.SMTPAppender
log4j.appender.Email.BufferSize=512
log4j.appender.Email.Threshold=ERROR
log4j.appender.Email.SMTPHost=smtp.gmail.com
log4j.appender.Email.SMTPUsername=myusername
log4j.appender.Email.SMTPPassword=mypassword
log4j.appender.Email.From=myemail@gmail.com
log4j.appender.Email.To=myotheremail@gmail.com
log4j.appender.Email.Subject=Error Report
log4j.appender.Email.layout=org.apache.log4j.PatternLayout
log4j.appender.Email.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
There is nothing that happened no email sent and no error shown , and I do not understand why, Please any idea about that topic ???
Not entirely answering your original question, but Logback seems to support GMail out of the box:
From: Chapter 4: Appenders of Logback documentation.
Here is a example : (because I tried very hard for a few days,now its works)
the trick is : log4j.appender.gmail.SMTPProtocol=smtps
The SMTP Appender provided by Log4J does not setup the necessary parameters to support GMail by default. It does not issue the STARTTLS command to initiate a SMTP session. You could rectify this by:
I faced the same problem with connecting to GMail SMTP server and now I have resolved it. The following piece of Code I used to send email with Log4j.properties file setting.
I am using the Log4j.1.2.16 version with JDK1.6
Please find below the steps to resolve the problem:
First step initialize the
System.getProperties(key,value)
. To connect to GMail, you have to make the following code changesSecond see the log4j.properties file setting:
After doing all the changes at code level as well as at log4j.properties settings, I started getting emails.
Let me know if someone have a better way to doing this.
Your code have some problems:
instead of
It should go