Log4j 记录错误时无法发送电子邮件

发布于 2024-11-14 08:06:09 字数 1725 浏览 0 评论 0原文

我在我的应用程序中启用了日志记录,并且我想通过电子邮件(gmail 帐户)发送日志错误。 I:

  1. 设置一个java项目
  2. 添加activation.jar,log4j.java和mail.jar(java邮件)
  3. 我将这些库添加到项目类路径中
  4. 我添加了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:

  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 ???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

稀香 2024-11-21 08:06:09

Log4J 提供的 SMTP Appender 默认情况下没有设置支持 GMail 所需的参数。它不会发出 STARTTLS 命令来启动 SMTP 会话。您可以通过

  • 以下方式纠正此问题: 编写自己的附加程序以支持将消息发送到 GMail SMTP 服务器。
  • 或者,使用Gmail 的 Log4j SMTP Appender。免责声明:我没有使用过这个项目。

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.
玻璃人 2024-11-21 08:06:09

这是一个例子:
(因为我努力了几天,现在它可以工作了)

技巧是: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

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
诺曦 2024-11-21 08:06:09

我在连接 GMail SMTP 服务器时遇到了同样的问题,现在
我已经解决了。下面是我用来发送电子邮件的代码
使用 Log4j.properties 文件设置。

我使用的是 Log4j.1.2.16 版本和 JDK1.6

请在下面找到解决问题的步骤:

第一步初始化 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");
}

其次查看 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

在代码级别以及 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 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","[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.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.

Let me know if someone have a better way to doing this.

且行且努力 2024-11-21 08:06:09

不完全回答您原来的问题,但 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>

来自:第 4 章:Logback 文档的 Appenders

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>[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: Appenders of Logback documentation.

轻许诺言 2024-11-21 08:06:09

您的代码有一些问题:

而不是

System.getProperty("mail.smtp.user","[email protected]");
System.getProperty("mail.smtp.starttls.enable","true");

It 应该去

 System.setProperty("mail.smtp.user","[email protected]");
 System.setProperty("mail.smtp.starttls.enable","true");

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");
昵称有卵用 2024-11-21 08:06:09
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
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文