Log4j SmtpAppender - 主题异常

发布于 2025-01-01 10:33:10 字数 1233 浏览 0 评论 0原文

我使用 log4j 发送电子邮件,但有例外。

我的 log4j 配置:

log4j.rootLogger=info, stdout, errmail

log4j.appender.errmail=cz.toby.utils.log.ErrSmtpAppender
[email protected]
log4j.appender.errmail.subject=Error - 
log4j.appender.errmail.layout=org.apache.log4j.HTMLLayout
log4j.appender.errmail.threshold=debug

它工作得很好,但我想要的是动态更改带有异常名称的主题内容。

我的附加程序如下所示:

    public class ErrSmtpAppender extends SMTPAppender {

    @Override
    public void activateOptions() {
        setSMTPHost("xxxxx");
        setFrom("[email protected]");
        setBufferSize(50);
        super.activateOptions();
        try {
            msg.setSubject(msg.getSubject() + ", node: " + InetAddress.getLocalHost().getHostName());
        } catch (Exception e) {
            // do nothing, only cannot set host to subject
        }
    }

}

如何以编程方式设置带有异常名称的主题?我试图在互联网上找到它,但我只找到了类似的主题,但没有找到我真正想要的。

I use log4j for sending emails with exception.

My log4j configuration:

log4j.rootLogger=info, stdout, errmail

log4j.appender.errmail=cz.toby.utils.log.ErrSmtpAppender
[email protected]
log4j.appender.errmail.subject=Error - 
log4j.appender.errmail.layout=org.apache.log4j.HTMLLayout
log4j.appender.errmail.threshold=debug

It works perfectly, but what I want is dynamicaly change the content of subject with names of exceptions.

my appender looks like this:

    public class ErrSmtpAppender extends SMTPAppender {

    @Override
    public void activateOptions() {
        setSMTPHost("xxxxx");
        setFrom("[email protected]");
        setBufferSize(50);
        super.activateOptions();
        try {
            msg.setSubject(msg.getSubject() + ", node: " + InetAddress.getLocalHost().getHostName());
        } catch (Exception e) {
            // do nothing, only cannot set host to subject
        }
    }

}

How can I set subject with a name of exception programmatically? I was trying to find it on the internet, but I found only similar themes but not what I exactly want.

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

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

发布评论

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

评论(1

故人爱我别走 2025-01-08 10:33:10

@托比好吧,我误解了你想要实现的目标。您想要电子邮件主题中的日志消息吗?

我认为为此,您需要访问 Appender 的 CyclicBuffer cb,例如:

msg.setSubject("Logged Message String" + cb.get(cb.length()).getMessage());

或者您可能还需要执行 ObjectRenderer

ThreadGroupRenderer stackTraceRenderer = new ThreadGroupRenderer();
msg.setSubject("First 100 Chars of Stack Trace: " + stackTraceRenderer(cb.get(cb.length()-1).getMessage()).substring(0,99));

@Toby Ok I misunderstood what you are trying to achieve. You are wanting the log message in the e-mail subject?

I think for that you will need to access the CyclicBuffer cb of the Appender something like:

msg.setSubject("Logged Message String" + cb.get(cb.length()).getMessage());

Or you may need to do an ObjectRenderer as well

ThreadGroupRenderer stackTraceRenderer = new ThreadGroupRenderer();
msg.setSubject("First 100 Chars of Stack Trace: " + stackTraceRenderer(cb.get(cb.length()-1).getMessage()).substring(0,99));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文