尝试使用 SMTP Appender 时出现 Log4J2 工厂错误

Log4J2 Factory Error when attempting to use SMTP Appender

下午好,

当我尝试使用 SMTP Appender 时,我在控制台中收到一个奇怪的错误。加载 XML 文件时似乎会发生错误,因为它不会通过除标准输出之外的任何输出流记录。 appender的内容如下(而且我已经确认错误在XML这一块)。我已经删除了我们的服务器信息。

<SMTP name="Mailer">
    <Subject>[ERROR] (software name) on ${hostName} has thrown a fatal error</Subject>
    <To>(a valid email in the form of a@a.com)</To>
    <From>(a valid email in the form of a@a.com)</From>
    <SMTPHost>(The Internal IP address of a server in the form of 192.168.0.1)</SMTPHost>
    <SMTPPort>587</SMTPPort>
    <BufferSize>512</BufferSize>
</SMTP>

无论它是否在记录器中被引用,我在 运行 程序和 运行 从日志管理器获取 getLogger 时立即收到以下错误。我删除了几个文件名并将它们替换为文件当时正在做什么的粗略描述。

2015-05-19 19:08:18,812 ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.SmtpAppender for element SMTP. java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:137) at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:766) at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:706) at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:698) at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:358) at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:161) at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:361) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:426) at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:442) at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:138) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:147) at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:41) at org.apache.logging.log4j.LogManager.getContext(LogManager.java:175) at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:426) at (Our Log Interface Class, which essentially just returns the log passed by the Apache log manager) at (The global variable definition file, the first method to use getLogger) at (The main method for our software, where the globals are loaded) Caused by: java.lang.NoSuchMethodError: javax.mail.Session.setProtocolForAddress(Ljava/lang/String;Ljava/lang/String;)V at org.apache.logging.log4j.core.net.SmtpManager$SMTPManagerFactory.createManager(SmtpManager.java:325) at org.apache.logging.log4j.core.net.SmtpManager$SMTPManagerFactory.createManager(SmtpManager.java:299) at org.apache.logging.log4j.core.appender.AbstractManager.getManager(AbstractManager.java:71) at org.apache.logging.log4j.core.net.SmtpManager.getSMTPManager(SmtpManager.java:124) at org.apache.logging.log4j.core.appender.SmtpAppender.createAppender(SmtpAppender.java:142) ... 21 more

2015-05-19 19:08:18,814 ERROR Null object returned for SMTP in Appenders. 2015-05-19 19:08:18,819 ERROR Unable to locate appender Mailer for logger fatalerror

配置的详细信息是正确的(我知道它是一个有效的 IP 等)- 它们在 log4j 1 中工作。错误日志几乎没有告诉我任何关于错误的信息,所以我希望有人以前听说过这个.谢谢大家!

这是您的堆栈跟踪中的重要行:

Caused by: java.lang.NoSuchMethodError: javax.mail.Session.setProtocolForAddress(Ljava/lang/String;Ljava/lang/String;)V

setProtocolForAddress method 从 1.4 版开始就是 JavaMail 的一部分,所以这向我暗示您使用的是该 JAR 的旧版本。如果是,请尝试升级到更高版本。

请注意,log4j2 有很多查找,而不仅仅是系统属性。因此您需要将 ${hostName} 转换为 ${sys:hostName},如果您正在使用 IP 地址查找,这可能是问题的原因。 (尝试对所有查找值进行硬编码以排除这种可能性。)