简体   繁体   English

为log4j SMTPAppender中的邮件设置自定义主题

[英]Set custom subject for mail in log4j SMTPAppender

I want that every time a message is logged for being sent as mail, different subject should be set. 我希望每次记录要通过邮件发送的消息时,都应该设置不同的主题。 Below is log4j config for my smtpappender 以下是我的smtpappender的log4j配置

log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.SMTPHost=localhost
log4j.appender.email.SMTPPort=25
log4j.appender.email.from="user@mydomain.com"
log4j.appender.email.to="user@mydomain.com"
log4j.appender.email.subject="test"
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} :: %-5p :: %c{1}:%L :: Message :: %m%n

I know that SMTPAppender should be extended, but I can't find any satisfactory info about how I can write a class that will accept subject when a log event is triggered. 我知道应该扩展SMTPAppender,但是我找不到关于如何编写一个可以在触发日志事件时接受主题的类的令人满意的信息。 for example 例如

logger.error("Sample message");

For this log message subject should be accepted. 对于此日志消息,应接受主题。 How to achieve this? 如何实现呢?

You're right, you will face to the reload properties issue. 没错,您将面对重载属性问题。 Moreover I doubt that it would work on a concurrent system (2 errors raised at same time). 此外,我怀疑它是否可以在并发系统上运行(同时引发2个错误)。 Instead I would create a dedicated service to send the emails without using log4j. 相反,我将创建一个专用服务来发送电子邮件,而无需使用log4j。 By this way, you should gain on the robustness aspect. 通过这种方式,您应该在鲁棒性方面有所收获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM