使用 log4j 通过 SMTPAppender 发送电子邮件报告 [英] Using log4j to send email reports via the SMTPAppender
问题描述
我正在尝试使用 log4j 发送包含来自后台进程的日志记录语句的电子邮件报告.我希望为每个进程运行发送一封电子邮件,而不是为每个日志语句发送一封电子邮件.我查看了 SMTPAppender
,但没有看到在流程完成时手动发送报告的方法.我相信 TriggeringEventEvaluator
可能是关键,但我遇到的一个问题是如何获得 TriggeringEventEvaluator
实例的句柄.我一直在使用 log4j 1.2.14,而 SMTPAppender.getEvaluator()
方法是在 1.2.15 中引入的.有什么想法吗?我是否在正确的轨道上?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?
我希望能够做到这一点:
I want to be able to do this:
log.info(message1);
log.info(message2);
log.info(message3);
log.sendMail();
在考虑更多之后,我想我需要澄清我希望完成的任务.我正在尝试通过运行石英作业捕获日志记录并将结果日志作为电子邮件发送.石英作业将一堆服务方法调用到各种服务中.我希望包括这些服务方法执行的任何日志记录以及石英作业本身的日志记录.我想我可以执行以下操作来捕获所有日志记录,但它不起作用.
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屋!