Apache Log4j自定义电子邮件appender没有邮件,应用程序启动时出现错误通知

时间:2015-10-07 19:00:31

标签: java email logging log4j log4j2

我有一个应用程序,它将记录器配置为在设置标记时向所有应用程序管理员发送电子邮件。我们编写了一个自定义电子邮件附加程序,因为现有的SMTPAppender对我们的需求来说太不灵活了。

本周早些时候,我们注意到我们的QA服务器没有收到记录电子邮件,这在以前是经常发生的。我们的配置中没有任何内容突然出现在我身上,我很难过。

这是IDE中应用程序启动的输出:

2015-10-07 14:26:11,344 ERROR Error processing element EMAIL: CLASS_NOT_FOUND
2015-10-07 14:26:11,435 ERROR Unable to locate appender Email for logger 
14:26:11.709 [main] INFO  c.q.app.services.server.data.h2.H2DbLifecycleManager - Starting DB on
connection jdbc:h2:tcp://localhost/C:/Users/jason/AppData/Local/appserver/store/data/db
2015-10-07 14:26:13,401 ERROR Error processing element EMAIL: CLASS_NOT_FOUND
2015-10-07 14:26:13,416 ERROR Unable to locate appender Email for logger 
Oct 07, 2015 2:26:14 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 4.3.1.Final

电子邮件附加程序在EMAIL块中的以下配置中定义。 log4j.xml中的配置是

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <EMAIL name="Email">
            <PatternLayout pattern="%d{MMM dd, yyyy - HH:mm:ss}%n%msg%n"/>
            <MarkerFilter marker="NOTIFICATION_EMAIL" onMatch="ACCEPT" onMismatch="DENY"/>
        </EMAIL>
    </Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="Email"/>
        </Root>
        <Logger name="com.ning.http.client" level="warn">
            <AppenderRef ref="Console" />
        </Logger>
    </Loggers>
</Configuration>

,自定义appender定义为

/**
 * An appender used to send an email notification of a log event to all application Administrators.
 */
@Plugin(name = "EMAIL", category = "Core", elementType = "appender", printObject = true)
public class EmailAppender extends AbstractAppender
{

    /**
     * @param name - the name of the Appender.  Cannot be null
     * @param filter - The Filter or null (defaults to ThresholdFilter, level of ERROR).
     * @param layout - The layout to use - if not included it uses the default PatternLayout.
     * @return an appender for sending emails on logging events
     */
    @PluginFactory
    public static EmailAppender createAppender(@PluginAttribute("name") final String name, 
                                                @PluginElement("Filter") Filter filter, @PluginElement("Layout") Layout<? extends Serializable> layout)
    {
        if (name == null)
        {
            LOGGER.error("No name provided for NotificationAppender");
            return null;
        }

        if (filter == null)
            filter = ThresholdFilter.createFilter(null, null, null);

        if (layout == null)
            layout = PatternLayout.createDefaultLayout();

        return new EmailAppender(name, filter, layout);
    }

    protected EmailAppender(String name, Filter filter, Layout<? extends Serializable> layout)
    {
        super(name, filter, layout);
    }

    ...

}

似乎所有内容都按预期配置。但是,由于未知原因,EmailAppender根本没有编译或包含在构建中。有什么想法解决这个问题吗?

1 个答案:

答案 0 :(得分:1)

如果有人遇到类似情况,请发布此答案。

由于未知原因,尽管类定义上有@Plugin(name = ...)注释,但Log4J注释扫描程序无法获取自定义appender类。我没有经验或工具来深入了解原因,但遇到了解决方案。

packages标记添加Configuration限定符可以解决问题,如下所示:

<Configuration status="WARN" packages="com.company.web.common.logging">