使用log4j通过SMTPAppender发送电子邮件报告 [英] Using log4j to send email reports via the SMTPAppender
问题描述
我正在尝试使用log4j发送包含来自后台进程的日志记录语句的可通过电子邮件发送的报告。我想为每个进程运行发送一封电子邮件,而不是每封邮件语句都有一封电子邮件。我查看了 SMTPAppender
,但是在进程完成时没有看到手动发送报告的方法。我相信 TriggeringEventEvaluator
可能是关键,但我遇到的一个问题是如何获得 TriggeringEventEvaluator $ c $的句柄c>实例。我坚持使用log4j 1.2.14并且在1.2.15中引入了
SMTPAppender.getEvaluator()
方法。有什么想法吗?我是否走在正确的轨道上? SMTPAppender.close()
方法是否可以在这里发挥作用?
I'm trying to use log4j to send emailable reports that contain the logging statements from a background process. I want one email sent for each process run, not one email for each logging statement. I've looked at the SMTPAppender
, but don't see a way to manually send the report when the process completes. I believe the TriggeringEventEvaluator
may be the key, but one issue I'm running into is how to get a handle to the TriggeringEventEvaluator
instance. I'm stuck using log4j 1.2.14 and the SMTPAppender.getEvaluator()
method was introduced in 1.2.15. Any thoughts? Am I even on the right track? Does the SMTPAppender.close()
method come into play here?
我希望能够这样做:
log.info(message1);
log.info(message2);
log.info(message3);
log.sendMail();
在考虑了这个之后,我想我需要澄清一下我希望完成的事情。我正在尝试捕获运行石英作业的日志记录,并将生成的日志作为电子邮件发送。 quartz工作将一堆服务方法调用到各种服务中。我想要包括那些服务方法执行的任何日志记录以及石英作业本身的日志记录。我以为我可以执行类似以下操作来捕获所有日志记录,但它无法正常工作。
After thinking about this some more, I think I need to clarify what I'm hoping to accomplish. I'm trying to capture the logging from running a quartz job and send the resulting log as an email. The quartz job makes a bunch of service method calls into various services. I want the to include any logging those service methods perform as well as the logging of the quartz jobs itself. I was thinking I could do something like the following for capturing all the logging, but it isn't working.
// at the beginning of quartz job
Logger logger = Logger.getRootLogger();
StringWriter sw = new StringWriter();
WriterAppender wa = new WriterAppender(new SimpleLayout(), sw);
logger.addAppender(wa);
// at the end of the quartz job
String report = sw.toString();
推荐答案
你不应该使用任何log4j的方法,你应该正确配置它。
You shouldn't use any of log4j's methods, you should configure it properly instead.
首先,在您的 log4j.properties
中正确定义您的appender:
First of all, define in your log4j.properties
file your appender properly:
#CONFIGURE SMTP
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.SMTPHost=mail.mydomain.com
log4j.appender.email.SMTPUsername=myuser@mydomain.com
log4j.appender.email.SMTPPassword=mypw
log4j.appender.email.From=myuser@mydomain.com
log4j.appender.email.To=myuser@mydomain.com
log4j.appender.email.Subject=Log of messages
log4j.appender.email.BufferSize=1
log4j.appender.email.EvaluatorClass=TriggerLogEvent
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=%m
注意:代码取自这篇文章。更多信息可以在 SMTPAppender API 。
Note: code taken from this post. More information can be obtained in SMTPAppender API.
接下来,制作一个专门用于发送电子邮件的课程。示例:
Next, make a special class that will be used just for sending email. Example:
package com.foo.mailer;
import org.apache.log4j.Logger;
public class Mailer {
private static final Logger logger = Logger.getLogger(Mailer.class);
public void logMail(String mailString) {
logger.info(mailString);
}
}
接下来,输入 log4j .properties
此类的配置:
Next, put in log4j.properties
configuration for this class:
# INFO level will be logged
log4j.logger.com.foo.mailer = INFO, email
# turn off additivity
log4j.additivity.com.foo.mailer = false
现在,无论何时您想使用log4j发送电子邮件,请将其放入您的代码中:
Now, whenever you want to send an email using log4j, put this in your code:
new Mailer().logMail("This mail should be sent");
免责声明:我尚未测试任何此类代码。
Disclaimer: I haven't tested any of this code.
这篇关于使用log4j通过SMTPAppender发送电子邮件报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!