Log4j 记录错误时无法发送电子邮件
我在我的应用程序中启用了日志记录,并且我想通过电子邮件(gmail 帐户)发送日志错误。 I:
- 设置一个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
配置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
没有发生任何事情,没有发送电子邮件,也没有显示错误,我不明白为什么,请对这个主题有任何想法吗???
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
[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 ???
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Log4J 提供的 SMTP Appender 默认情况下没有设置支持 GMail 所需的参数。它不会发出 STARTTLS 命令来启动 SMTP 会话。您可以通过
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.appender.gmail.SMTPProtocol=smtps
Here is a example :
(because I tried very hard for a few days,now its works)
the trick is : log4j.appender.gmail.SMTPProtocol=smtps
我在连接 GMail SMTP 服务器时遇到了同样的问题,现在
我已经解决了。下面是我用来发送电子邮件的代码
使用 Log4j.properties 文件设置。
我使用的是 Log4j.1.2.16 版本和 JDK1.6
请在下面找到解决问题的步骤:
第一步初始化
System.getProperties(key,value)
。要连接到 GMail,您必须进行以下代码更改其次查看 log4j.properties 文件设置:
在代码级别以及 log4j.properties 设置完成所有更改后,我开始收到电子邮件。
让我知道是否有人有更好的方法来做到这一点。
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.
不完全回答您原来的问题,但 Logback 似乎支持 GMail 开箱即用:
来自:第 4 章:Logback 文档的 Appenders。
Not entirely answering your original question, but Logback seems to support GMail out of the box:
From: Chapter 4: Appenders of Logback documentation.
您的代码有一些问题:
而不是
It 应该去
Your code have some problems:
instead of
It should go