java Log4j 在记录错误时无法发送电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6242838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Log4j failing to send an email when logging an error
提问by bmscomp
I enabled logging in my application, and I want to send logs error by email (gmail account). I:
我在我的应用程序中启用了登录,我想通过电子邮件(gmail 帐户)发送日志错误。一世:
- 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 :
- 建立一个java项目
- 添加 activation.jar 、 log4j.java 和 mail.jar (java邮件)
- 我将这些库添加到项目类路径中
- 我添加了 log4j.properties 并像这样配置它:
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
配置 SMTP 附加程序
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 ???
没有发生任何事情,没有发送电子邮件,也没有显示错误,我不明白为什么,请对这个话题有任何想法???
回答by Vineet Reynolds
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:
Log4J 提供的 SMTP Appender 默认没有设置必要的参数来支持 GMail。它不会发出 STARTTLS 命令来启动 SMTP 会话。您可以通过以下方式纠正此问题:
- 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.
- 编写您自己的 appender 以支持将消息发送到 GMail SMTP 服务器。
- 或者,使用Log4j SMTP Appender for Gmail。免责声明:我没有使用过这个项目。
回答by LanaLang
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.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
回答by Bharat
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.
我在连接到 GMail SMTP 服务器时遇到了同样的问题,现在我已经解决了。下面的一段代码我用来发送带有 Log4j.properties 文件设置的电子邮件。
I am using the Log4j.1.2.16 version with JDK1.6
我使用的是带有 JDK1.6 的 Log4j.1.2.16 版本
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
第一步初始化System.getProperties(key,value)
. 要连接到 GMail,您必须进行以下代码更改
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","[email protected]");
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.properties 文件设置:
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
[email protected]
log4j.appender.email.SMTPPassword=xyz1234
[email protected]
[email protected]
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.
在代码级别以及 log4j.properties 设置中进行所有更改后,我开始收到电子邮件。
Let me know if someone have a better way to doing this.
如果有人有更好的方法来做到这一点,请告诉我。
回答by Fernando Wermus
Your code have some problems:
您的代码有一些问题:
instead of
代替
System.getProperty("mail.smtp.user","[email protected]");
System.getProperty("mail.smtp.starttls.enable","true");
It should go
应该去
System.setProperty("mail.smtp.user","[email protected]");
System.setProperty("mail.smtp.starttls.enable","true");
回答by Tomasz Nurkiewicz
Not entirely answering your original question, but Logbackseems to support GMail out of the box:
不能完全回答您的原始问题,但Logback似乎支持开箱即用的GMail :
<appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender">
<smtpHost>smtp.gmail.com</smtpHost>
<smtpPort>465</smtpPort>
<ssl>true</ssl>
<username>[email protected]</username>
<password>YOUR_GMAIL_PASSWORD</password>
<to>EMAIL-DESTINATION</to>
<to>ANOTHER_EMAIL_DESTINATION</to> <!-- additional destinations are possible -->
<from>[email protected]</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: Appendersof Logback documentation.
回答by user9478789
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
[email protected]
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
[email protected]
[email protected]
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