简体   繁体   English

Log4j SmtpAppender-主题中的例外

[英]Log4j SmtpAppender - exception in subject

I use log4j for sending emails with exception. 我使用log4j例外发送电子邮件。

My log4j configuration: 我的log4j配置:

log4j.rootLogger=info, stdout, errmail

log4j.appender.errmail=cz.toby.utils.log.ErrSmtpAppender
log4j.appender.errmail.to=my_email@email.cz
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: 我的appender看起来像这样:

    public class ErrSmtpAppender extends SMTPAppender {

    @Override
    public void activateOptions() {
        setSMTPHost("xxxxx");
        setFrom("from_email@email.cz");
        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. 我试图在互联网上找到它,但是我只发现了相似的主题,但没有找到我真正想要的主题。

@Toby Ok I misunderstood what you are trying to achieve. @Toby好的,我误解了您要实现的目标。 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: 我认为为此,您将需要访问Appender的CyclicBuffer cb,例如:

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

Or you may need to do an ObjectRenderer as well 否则您可能还需要做一个ObjectRenderer

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

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

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