我对springboot和log4j2完全是新手,而且我无法为某个类型的日志发送电子邮件(警告和错误)。我不明白为什么我会收到“ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.SmtpAppender”消息。有人知道吗?
日志4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configuration SYSTEM "log4j2.dtd">
<Configuration status="debug" monitorInterval="30">
<Appenders>
<SMTP name="Mailer" subject="Error Log" to="email@email.com"
from="email2@email.com"
smtpHost="host" smtpUsername="username"
smtpPassword="mdp" smtpProtocol="smtp"
bufferSize="50">
<HtmlLayout />
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n" />
</SMTP>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Mailer" />
</Root>
</Loggers>
</Configuration>
聚合物.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>smtp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>smtp</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Smtp应用程序.java
package com.example.smtp;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SmtpApplication {
private static final Logger logger = LogManager.getLogger(SmtpApplication.class);
public static void main(String[] args) {
SpringApplication.run(SmtpApplication.class, args);
logger.debug("Debugging log");
logger.info("Info log");
logger.warn("Hey, This is a warning!");
logger.error("Oops! We have an Error. OK");
logger.fatal("Damn! Fatal error. Please fix me.");
}
}
日志
2023-01-24 17:30:17,730 main DEBUG AsyncLogger.ThreadNameStrategy=UNCACHED (user specified null, default is UNCACHED)
2023-01-24 17:30:17,731 main DEBUG org.apache.logging.log4j.core.util.SystemClock supports precise timestamps.
2023-01-24 17:31:47,540 Log4j2-TF-4-Scheduled-2 INFO Source 'C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml' was modified on Tue Jan 24 17:31:33 CET 2023 (1674577893351), previous modification was on Tue Jan 24 17:29:57 CET 2023 (1674577797490)
2023-01-24 17:31:47,542 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Reconfiguration started for context 6d5380c2 (org.apache.logging.log4j.core.LoggerContext@7674b62c)
2023-01-24 17:31:47,544 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG PluginManager 'Lookup' found 17 plugins
2023-01-24 17:31:47,546 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Closing FileInputStream java.io.FileInputStream@465ecc51
2023-01-24 17:31:47,550 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Watching configuration 'C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml' for lastModified Tue Jan 24 17:31:33 CET 2023 (1674577893351)
2023-01-24 17:31:47,552 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Apache Log4j Core 2.19.0 initializing configuration XmlConfiguration[location=C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml]
2023-01-24 17:31:47,553 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG PluginManager 'Core' found 131 plugins
2023-01-24 17:31:47,554 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG PluginManager 'Level' found 0 plugins
2023-01-24 17:31:47,554 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG PluginManager 'Lookup' found 17 plugins
2023-01-24 17:31:47,556 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.HtmlLayout].
2023-01-24 17:31:47,557 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG HtmlLayout$Builder(locationInfo="null", title="null", contentType="null", charset="null", fontSize="null", fontName="null", datePattern="null", timezone="null")
2023-01-24 17:31:47,557 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Building Plugin[name=layout, class=org.apache.logging.log4j.core.layout.PatternLayout].
2023-01-24 17:31:47,558 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG PatternLayout$Builder(pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%L - %m%n", PatternSelector=null, Configuration(C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml), Replace=null, charset="null", alwaysWriteExceptions="null", disableAnsi="null", noConsoleNoAnsi="null", header="null", footer="null")
2023-01-24 17:31:47,558 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG PluginManager 'Converter' found 48 plugins
2023-01-24 17:31:47,559 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Building Plugin[name=appender, class=org.apache.logging.log4j.core.appender.SmtpAppender].
2023-01-24 17:31:47,561 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG SmtpAppender$Builder(to="email@email.com", cc="null", bcc="null", from="email2@email.com", replyTo="null", subject="Error Log", smtpProtocol="smtp", smtpHost="host", smtpPort="null", smtpUsername="username", smtpPassword="*****", smtpDebug="null", bufferSize="50", SSL=null, ignoreExceptions="null", HtmlLayout(org.apache.logging.log4j.core.layout.HtmlLayout@42614042), name="Mailer", Configuration(C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml), Filter=null, ={})
2023-01-24 17:31:47,563 Log4j2-TF-3-ConfigurationFileWatcher-3 ERROR appender SMTP has no parameter that matches element PatternLayout
2023-01-24 17:31:47,566 Log4j2-TF-3-ConfigurationFileWatcher-3 ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.SmtpAppender for element SMTP: java.lang.NoClassDefFoundError: javax/mail/MessagingException java.lang.NoClassDefFoundError: javax/mail/MessagingException
at org.apache.logging.log4j.core.appender.SmtpAppender$Builder.lambda$build$0(SmtpAppender.java:288)
at java.base/java.util.Optional.orElseGet(Unknown Source)
at org.apache.logging.log4j.core.appender.SmtpAppender$Builder.build(SmtpAppender.java:288)
at org.apache.logging.log4j.core.appender.SmtpAppender$Builder.build(SmtpAppender.java:92)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:124)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1138)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1063)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1055)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:664)
at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:258)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:304)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:621)
at org.apache.logging.log4j.core.LoggerContext.onChange(LoggerContext.java:757)
at org.apache.logging.log4j.core.util.AbstractWatcher$ReconfigurationRunnable.run(AbstractWatcher.java:93)
at java.base/java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.mail.MessagingException
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(Unknown Source)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(Unknown Source)
at java.base/java.lang.ClassLoader.loadClass(Unknown Source)
... 15 more
2023-01-24 17:31:47,571 Log4j2-TF-3-ConfigurationFileWatcher-3 ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.SmtpAppender for element SMTP: java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.SmtpAppender java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.SmtpAppender
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.findFactoryMethod(PluginBuilder.java:260)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:136)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1138)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1063)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1055)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:664)
at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:258)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:304)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:621)
at org.apache.logging.log4j.core.LoggerContext.onChange(LoggerContext.java:757)
at org.apache.logging.log4j.core.util.AbstractWatcher$ReconfigurationRunnable.run(AbstractWatcher.java:93)
at java.base/java.lang.Thread.run(Unknown Source)
2023-01-24 17:31:47,575 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Building Plugin[name=appenders, class=org.apache.logging.log4j.core.config.AppendersPlugin].
2023-01-24 17:31:47,577 Log4j2-TF-3-ConfigurationFileWatcher-3 ERROR Null object returned for SMTP in Appenders.
2023-01-24 17:31:47,579 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG createAppenders(={})
2023-01-24 17:31:47,580 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Building Plugin[name=AppenderRef, class=org.apache.logging.log4j.core.config.AppenderRef].
2023-01-24 17:31:47,581 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG createAppenderRef(ref="Mailer", level="null", Filter=null)
2023-01-24 17:31:47,581 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Building Plugin[name=root, class=org.apache.logging.log4j.core.config.LoggerConfig$RootLogger].
2023-01-24 17:31:47,582 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG LoggerConfig$RootLogger$Builder(additivity="null", level="INFO", levelAndRefs="null", includeLocation="null", ={Mailer}, ={}, Configuration(C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml), Filter=null)
2023-01-24 17:31:47,583 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Building Plugin[name=loggers, class=org.apache.logging.log4j.core.config.LoggersPlugin].
2023-01-24 17:31:47,584 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG createLoggers(={root})
2023-01-24 17:31:47,585 Log4j2-TF-3-ConfigurationFileWatcher-3 ERROR Unable to locate appender "Mailer" for logger config "root"
2023-01-24 17:31:47,586 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Configuration XmlConfiguration[location=C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml] initialized
2023-01-24 17:31:47,587 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Starting configuration XmlConfiguration[location=C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml]
2023-01-24 17:31:47,588 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Log4j2 ConfigurationScheduler starting 1 threads
2023-01-24 17:31:47,589 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Started configuration XmlConfiguration[location=C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml] OK.
2023-01-24 17:31:47,590 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Log4jBridgeHandler.propertyChange(): java.beans.PropertyChangeEvent[propertyName=config; oldValue=XmlConfiguration[location=C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml]; newValue=XmlConfiguration[location=C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml]; propagationId=null; source=org.apache.logging.log4j.core.LoggerContext@7674b62c]
2023-01-24 17:31:47,591 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Log4jBridgeHandler.propagateLogLevels(): XmlConfiguration[location=C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml]
2023-01-24 17:31:47,593 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Log4j2 ConfigurationScheduler shutting down threads in java.util.concurrent.ScheduledThreadPoolExecutor@6302a2c0[Running, pool size = 1, active threads
= 0, queued tasks = 1, completed tasks = 3]
2023-01-24 17:31:47,595 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Stopped XmlConfiguration[location=C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml] OK
2023-01-24 17:31:47,596 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Log4jBridgeHandler.propertyChange(): java.beans.PropertyChangeEvent[propertyName=config; oldValue=XmlConfiguration[location=C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml]; newValue=XmlConfiguration[location=C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml]; propagationId=null; source=org.apache.logging.log4j.core.LoggerContext@7674b62c]
2023-01-24 17:31:47,599 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Log4jBridgeHandler.propagateLogLevels(): XmlConfiguration[location=C:\Users\theo.lalande\Desktop\smtp\target\classes\log4j2.xml]
2023-01-24 17:31:47,602 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Registering MBean org.apache.logging.log4j2:type=6d5380c2
2023-01-24 17:31:47,603 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Registering MBean org.apache.logging.log4j2:type=6d5380c2,component=StatusLogger
2023-01-24 17:31:47,604 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Registering MBean org.apache.logging.log4j2:type=6d5380c2,component=ContextSelector
2023-01-24 17:31:47,606 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Registering MBean org.apache.logging.log4j2:type=6d5380c2,component=Loggers,name=
2023-01-24 17:31:47,606 Log4j2-TF-3-ConfigurationFileWatcher-3 DEBUG Reconfiguration completed for 6d5380c2 (org.apache.logging.log4j.core.LoggerContext@7674b62c) in 65 milliseconds.
我试过很多方法,但都没办法。有人知道吗?
2条答案
按热度按时间qoefvg9y1#
您正面临Java EE 8和Jakarta EE 9之间差距的副作用之一(这是一个非常具有破坏性的类重命名):Sping Boot 3.x包含Jakarta Mail 2.0的实现,而Log4j2 Core中的SMTP附加器需要Java Mail 1.6的实现。
为了填补差距,我们在
log4j-jakarta-smtp
工件中发布了MailManager
(SMTP附加器背后的引擎)的替代实现。要使用它,您只需要将它添加到运行时类路径中:(the版本由Sping Boot 管理)。
lvjbypge2#
尝试更换
具有以下依赖项