Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  1. Set up a java project
  2. add activation.jar , log4j.java and mail.jar (java mail)
  3. I added those libraries to the project class path
  4. 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
[email protected] 
[email protected]
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 ???

like image 393
bmscomp Avatar asked Jun 05 '11 12:06

bmscomp


2 Answers

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.
like image 79
Vineet Reynolds Avatar answered Sep 27 '22 17:09

Vineet Reynolds


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         
> [email protected]
> 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
> [email protected]
> [email protected]
> 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
like image 29
LanaLang Avatar answered Sep 27 '22 18:09

LanaLang