Search code examples
spring-bootlogginglog4j2

No factory method found for class HttpAppender


I was trying to use log4j2 HttpAppender, but I get this error:

2024-09-24T07:59:58.504101611Z main ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.HttpAppender for element Http: java.lang.NullPointerException: layout java.lang.NullPointerException: layout
    at java.base/java.util.Objects.requireNonNull(Objects.java:259)
    at org.apache.logging.log4j.core.appender.HttpAppender.<init>(HttpAppender.java:170)
    at org.apache.logging.log4j.core.appender.HttpAppender.<init>(HttpAppender.java:40)
    at org.apache.logging.log4j.core.appender.HttpAppender$Builder.build(HttpAppender.java:85)
    at org.apache.logging.log4j.core.appender.HttpAppender$Builder.build(HttpAppender.java:46)
    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:1164)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1085)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1077)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:681)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:264)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:313)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:631)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:713)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:735)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:260)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:154)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:46)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:197)
    at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:146)
    at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:113)
    at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:95)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59)
    at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:203)
    at com.leonardo.test.TestApplication.main(TestApplication.java:10)

2024-09-24T07:59:58.506231729Z main ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.HttpAppender for element Http: java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.HttpAppender java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.HttpAppender
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.findFactoryMethod(PluginBuilder.java:268)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:140)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1164)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1085)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1077)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:681)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:264)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:313)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:631)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:713)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:735)
    at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:260)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:154)
    at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:46)
    at org.apache.logging.log4j.LogManager.getContext(LogManager.java:197)
    at org.apache.commons.logging.LogAdapter$Log4jLog.<clinit>(LogAdapter.java:146)
    at org.apache.commons.logging.LogAdapter$Log4jAdapter.createLog(LogAdapter.java:113)
    at org.apache.commons.logging.LogAdapter.createLog(LogAdapter.java:95)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:67)
    at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:59)
    at org.springframework.boot.SpringApplication.<clinit>(SpringApplication.java:203)
    at com.leonardo.test.TestApplication.main(TestApplication.java:10)

2024-09-24T07:59:58.507533188Z main ERROR Null object returned for Http in Appenders.
2024-09-24T07:59:58.512017995Z main ERROR Unable to locate appender "HttpAppender" for logger config "root"
2024-09-24T07:59:58.668953038Z main ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.HttpAppender for element Http: java.lang.NullPointerException: layout java.lang.NullPointerException: layout
    at java.base/java.util.Objects.requireNonNull(Objects.java:259)
    at org.apache.logging.log4j.core.appender.HttpAppender.<init>(HttpAppender.java:170)
    at org.apache.logging.log4j.core.appender.HttpAppender.<init>(HttpAppender.java:40)
    at org.apache.logging.log4j.core.appender.HttpAppender$Builder.build(HttpAppender.java:85)
    at org.apache.logging.log4j.core.appender.HttpAppender$Builder.build(HttpAppender.java:46)
    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:1164)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1085)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1077)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:681)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:264)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:313)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:631)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:713)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:735)
    at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.reinitialize(Log4J2LoggingSystem.java:311)
    at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:74)
    at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:61)
    at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.initialize(Log4J2LoggingSystem.java:223)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:332)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:298)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:246)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:223)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:185)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:178)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:156)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)
    at org.springframework.boot.context.event.EventPublishingRunListener.multicastInitialEvent(EventPublishingRunListener.java:136)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:81)
    at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:64)
    at java.base/java.lang.Iterable.forEach(Iterable.java:75)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:118)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:112)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:63)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:370)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352)
    at com.leonardo.test.TestApplication.main(TestApplication.java:10)

2024-09-24T07:59:58.669561735Z main ERROR Unable to invoke factory method in class org.apache.logging.log4j.core.appender.HttpAppender for element Http: java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.HttpAppender java.lang.IllegalStateException: No factory method found for class org.apache.logging.log4j.core.appender.HttpAppender
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.findFactoryMethod(PluginBuilder.java:268)
    at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:140)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1164)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1085)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:1077)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:681)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:264)
    at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:313)
    at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:631)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:713)
    at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:735)
    at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.reinitialize(Log4J2LoggingSystem.java:311)
    at org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(AbstractLoggingSystem.java:74)
    at org.springframework.boot.logging.AbstractLoggingSystem.initialize(AbstractLoggingSystem.java:61)
    at org.springframework.boot.logging.log4j2.Log4J2LoggingSystem.initialize(Log4J2LoggingSystem.java:223)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:332)
    at org.springframework.boot.context.logging.LoggingApplicationListener.initialize(LoggingApplicationListener.java:298)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEnvironmentPreparedEvent(LoggingApplicationListener.java:246)
    at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:223)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:185)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:178)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:156)
    at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:138)
    at org.springframework.boot.context.event.EventPublishingRunListener.multicastInitialEvent(EventPublishingRunListener.java:136)
    at org.springframework.boot.context.event.EventPublishingRunListener.environmentPrepared(EventPublishingRunListener.java:81)
    at org.springframework.boot.SpringApplicationRunListeners.lambda$environmentPrepared$2(SpringApplicationRunListeners.java:64)
    at java.base/java.lang.Iterable.forEach(Iterable.java:75)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:118)
    at org.springframework.boot.SpringApplicationRunListeners.doWithListeners(SpringApplicationRunListeners.java:112)
    at org.springframework.boot.SpringApplicationRunListeners.environmentPrepared(SpringApplicationRunListeners.java:63)
    at org.springframework.boot.SpringApplication.prepareEnvironment(SpringApplication.java:370)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:330)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1363)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1352)
    at com.leonardo.test.TestApplication.main(TestApplication.java:10)

2024-09-24T07:59:58.670033903Z main ERROR Null object returned for Http in Appenders.
2024-09-24T07:59:58.670451268Z main ERROR Unable to locate appender "HttpAppender" for logger config "root"

This is my log4j2.xml configuration:

<Configuration>
    <Appenders>
        <Http name="HTTP" url="http://localhost:8055/log" verifyHostname="false" method="POST"/>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="HTTP"/>
        </Root>
    </Loggers>
</Configuration>

And these are the dependencies in my pom.xml:

<properties>
    <java.version>21</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-log4j2</artifactId>
    </dependency>



    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>
                        <groupId>org.projectlombok</groupId>
                        <artifactId>lombok</artifactId>
                    </exclude>
                </excludes>
            </configuration>
        </plugin>
    </plugins>
</build>

I also checked the classes in my target folder and the class public final class HttpAppender extends AbstractAppender is present.

So, is my configuration wrong or what?

I also looked into this question, but my version of log4j2 is well above 2.7.


Solution

  • Looking at the stacktrace there is a NullPointerException on the layout property of the HttpAppender.

    2024-09-24T07:59:58.668953038Z main ERROR Could not create plugin of type class org.apache.logging.log4j.core.appender.HttpAppender for element Http: java.lang.NullPointerException: layout java.lang.NullPointerException: layout
    

    Which indicates that you need to specify the layout to use. Looking at the documentation the layout has a multiplicity of one, which is basically required (else it would have been zero or one).

    Fix your configuration.

    <Configuration>
        <Appenders>
            <Http name="HTTP" url="http://localhost:8055/log" verifyHostname="false" method="POST">
              <PatternLayout pattern="%d %m%n"/>
            </Http>
        </Appenders>
        <Loggers>
            <Root level="INFO">
                <AppenderRef ref="HTTP"/>
            </Root>
        </Loggers>
    </Configuration>
    

    Here, as in the documentation, it adds the JsonTemplateLayout.