I'm completely new to springboot and log4j2, and I cannot manage to send e-mail for a certain type of log (Warn&Error). I dont' understand why i get "ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.SmtpAppender" message. Does anyone have a clue?
log4j2.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 protected]"
from="[email protected]"
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>
pom.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>
SmtpApplication.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.");
}
}
LOG
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 protected]", cc="null", bcc="null", from="[email protected]", 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.
I've tried plenty of things, but cannot mannage du make it work. Does anyone avec a clue?
CodePudding user response:
Your are facing one of the side-effects of the gap between Java EE 8 and Jakarta EE 9 (it's a very disruptive renaming of classes): Spring Boot 3.x contains an implementation of Jakarta Mail 2.0, while the SMTP appender in Log4j2 Core requires and implementation of Java Mail 1.6.
To fill the gap, we published an alternative implementation of MailManager
(the engine behind the SMTP appender) in the log4j-jakarta-smtp
artifact. To use it, you just need to add it to your runtime classpath:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jakarta-smtp</artifactId>
<scope>runtime</scope>
<exclusions>
<exclusion>
<groupId>com.sun.activation</groupId>
<artifactId>jakarta-activation</artifactId>
</exclusion>
<exclusion>
<groupId>com.sun.mail</groupId>
<artifactId>smtp</artifactId>
</exclusion>
</exclusions>
</dependency>
(the version is managed by Spring Boot).
CodePudding user response:
Try replacing
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
with the following the dependencies
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
<scope>runtime</scope>
</dependency>