Log4j failing to send an email when logging an error
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.Em开发者_开发知识库ail.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 ???
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:
- Either writing your own appender to support sending of messages to the GMail SMTP server.
- Or, by using the Log4j SMTP Appender for Gmail. Disclaimer: I haven't used this project.
Here is a example : (because I tried very hard for a few days,now its works)
the trick is : log4j.appender.gmail.SMTPProtocol=smtps
> log4j.rootLogger= ERROR,gmail
> log4j.appender.gmail=org.apache.log4j.net.SMTPAppender
> log4j.appender.gmail.SMTPProtocol=smtps
> log4j.appender.gmail.SMTPUsername=dummy@gmail.com
> log4j.appender.gmail.SMTPPassword=Your gmail password
> log4j.appender.gmail.threshold=error
> log4j.appender.gmail.SMTPHost=smtp.gmail.com
> log4j.appender.gmail.SMTPPort=465
> log4j.appender.gmail.smtp.starttls.enable=true
> log4j.appender.gmail.Subject=Logging Message via Gmail
> log4j.appender.gmail.To=dummy@example.com
> log4j.appender.gmail.From=dummy@gmail.com
> log4j.appender.gmail.layout=org.apache.log4j.PatternLayout
> log4j.appender.gmail.layout.ConversionPattern=%d{MM/dd/yyyy HH:mm:ss}[%M] %-5p %C - %m%n
>log4j.appender.gmail.BufferSize=5
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 changes
static
{
System.setProperty("mail.smtp.auth", "true");
System.setProperty("mail.smtp.socketFactory.port", "465");
System.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
System.setProperty("mail.smtp.socketFactory.fallback", "false");
System.setProperty("mail.smtp.user","xxx@gmail.com");
System.setProperty("mail.smtp.starttls.enable","true");
System.setProperty("mail.transport.protocol", "smtp");
System.setProperty("mail.smtp.starttls.enable", "true");
System.setProperty("mail.smtp.host", "smtp.gmail.com");
System.setProperty("mail.smtp.port", "465");
System.setProperty("mail.smtp.quitwait", "false");
}
Second see the log4j.properties file setting:
log4j.rootLogger=DEBUG, FILE, email
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.maxFileSize=8192KB
log4j.appender.FILE.maxBackupIndex=5
log4j.appender.FILE.File=xxx.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} [%p] %t %c:%L - %m%n
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.SMTPHost=smtp.gmail.com
log4j.appender.email.SMTPPort=465
log4j.appender.email.SMTPUsername=xyz@gmail.com
log4j.appender.email.SMTPPassword=xyz1234
log4j.appender.email.From=abcd@gmail.com
log4j.appender.email.to=blaha@gmail.com
log4j.appender.email.Subject=Error Alert
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=%d [%t] %-5p %c %x - %m%n
log4j.appender.email.BufferSize=10
log4j.appender.email.Threshold=ERROR
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.
Not entirely answering your original question, but Logback seems to support GMail out of the box:
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>smtp.gmail.com</smtpHost>
<smtpPort>465</smtpPort>
<ssl>true</ssl>
<username>YOUR_USERNAME@gmail.com</username>
<password>YOUR_GMAIL_PASSWORD</password>
<to>EMAIL-DESTINATION</to>
<to>ANOTHER_EMAIL_DESTINATION</to> <!-- additional destinations are possible -->
<from>YOUR_USERNAME@gmail.com</from>
<subject>TESTING: %logger{20} - %m</subject>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%date %-5level %logger{35} - %message%n</pattern>
</layout>
</appender>
From: Chapter 4: Appenders of Logback documentation.
Your code have some problems:
instead of
System.getProperty("mail.smtp.user","xxx@gmail.com");
System.getProperty("mail.smtp.starttls.enable","true");
It should go
System.setProperty("mail.smtp.user","xxx@gmail.com");
System.setProperty("mail.smtp.starttls.enable","true");
I am using the below log4j configuration . But There is nothing that happened no email sent and no error shown , and I do not understand why, Please any idea about that topic ???
I am using jdk 1.8 and log4j-1.2.17.jar , activation.jar, java-mail-1.4.jar,mail-api-1-3-1.jar,slf4j-api-1.7.1.jar...
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.SMTPProtocol=smtps
log4j.appender.MAIL.SMTPUsername=username@gmail.com
log4j.appender.MAIL.SMTPPassword=password
log4j.appender.MAIL.threshold=error
log4j.appender.MAIL.SMTPHost=smtp.gmail.com
log4j.appender.MAIL.SMTPPort=465
log4j.appender.MAIL.smtp.starttls.enable=true
log4j.appender.MAIL.Subject=Error Alert on server
log4j.appender.MAIL.to=username@gmail.com
log4j.appender.MAIL.From=no-reply@gmail.com
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=[%d{ISO8601}]%n%n%-5p%n%n%c%n%n%m%n%n`enter code here`
log4j.appender.MAIL.BufferSize=5
log4j.appender.MAIL.LevelRangeFilter.LevelMin=error
log4j.appender.MAIL.LevelRangeFilter.LevelMax=fatal
log4j.appender.MAIL.smtp.auth=true
精彩评论