Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

log4j2 - Unable to invoke factory method in class org.apache.logging.log4j.core.config.AppendersPlugin for element Appenders: java.lang.NullPointerException

I'm converting this working Log4j2 XML based example How to Create a Custom Appender in log4j2? to .properties file version. However, I'm getting below error:

Error log:

Unable to invoke factory method in class org.apache.logging.log4j.core.config.AppendersPlugin for element Appenders: java.lang.NullPointerException
java.lang.NullPointerException
at org.apache.logging.log4j.core.config.plugins.visitors.PluginElementVisitor.visit(PluginElementVisitor.java:52)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.generateParameters(PluginBuilder.java:258)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:135)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:1002)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:942)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:552)
at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:241)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:288)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:622)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:695)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:712)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:267)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:245)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:47)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:174)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:664)
at com.log.TestCustomAppender.<clinit>(TestCustomAppender.java:7)

Log4j2.properties file:

name = configuration
packages = com.yourcompany.yourcustomappenderpackage
status = error

# Define stdout appender, which sends log events to stdout
appender.console.type = Console
appender.console.name = STDOUT

#appender.abc=com.yourcompany.yourcustomappenderpackage.MyCustomAppenderImpl
appender.abc.type=appender
appender.abc.name=MyCustomAppender

rootLogger.level = warn
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.abc.ref = MyCustomAppender

logger.TestCustomAppender1.level = info
logger.TestCustomAppender1.name = com.log.TestCustomAppender
logger.TestCustomAppender1.appenderRef.stdout.ref = STDOUT
logger.TestCustomAppender1.appenderRef.abc.ref = MyCustomAppender

TestCustomAppender.java

package com.log;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class TestCustomAppender {
    static final Logger logger = LogManager.getLogger(TestCustomAppender.class.getName());

    public static void main(String[] args) {
        
        logger.info("Hello World!!!"); 
        logger.debug("Hello World!!!");
        logger.error("Hello World!!!");
        System.out.println("Rohit");
    }
}

MyCustomAppenderImpl.java

package com.yourcompany.yourcustomappenderpackage;

import java.io.Serializable;
import java.util.concurrent.locks.*;
import org.apache.logging.log4j.core.*;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.appender.AppenderLoggingException;
import org.apache.logging.log4j.core.config.plugins.*;
import org.apache.logging.log4j.core.layout.PatternLayout;

// note: class name need not match the @Plugin name.
@Plugin(name = "MyCustomAppender", category = "Core", elementType = "appender", printObject = true)
public final class MyCustomAppenderImpl extends AbstractAppender {
    
    protected MyCustomAppenderImpl(String name, Filter filter, Layout<? extends Serializable> layout,
            final boolean ignoreExceptions) {
        super(name, filter, layout, ignoreExceptions);
        System.out.println("Rohit 2");

    }

    // The append method is where the appender does the work.
    // Given a log event, you are free to do with it what you want.
    // This example demonstrates:
    // 1. Concurrency: this method may be called by multiple threads concurrently
    // 2. How to use layouts
    // 3. Error handling
    @Override
    public void append(LogEvent event) {

        System.out.println("Rohit 1");
    }

    // Your custom appender needs to declare a factory method
    // annotated with `@PluginFactory`. Log4j will parse the configuration
    // and call this factory method to construct an appender instance with
    // the configured attributes.
    @PluginFactory
    public static MyCustomAppenderImpl createAppender(@PluginAttribute("name") String name,
            @PluginElement("Layout") Layout<? extends Serializable> layout,
            @PluginElement("Filter") final Filter filter, @PluginAttribute("otherAttribute") String otherAttribute) {
        System.out.println("Rohit 3");

        if (name == null) {
            LOGGER.error("No name provided for MyCustomAppenderImpl");
            return null;
        }
        if (layout == null) {
            layout = PatternLayout.createDefaultLayout();
        }
        return new MyCustomAppenderImpl(name, filter, layout, true);
    }
}
question from:https://stackoverflow.com/questions/65831073/unable-to-invoke-factory-method-in-class-org-apache-logging-log4j-core-config-ap

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The issue was with Type value in appender.abc.type=appender
I thought it should be "appender" from elementType = "appender". However, Type value must be value of @Plugin (name="MyCustomAppender")

name = configuration
packages = com.yourcompany.yourcustomappenderpackage
status = error

# Define stdout appender, which sends log events to stdout
appender.console.type = Console
appender.console.name = STDOUT

appender.abc.name=MyCustomAppender
appender.abc.type=MyCustomAppender

#rootLogger.level = warn
rootLogger.appenderRef.stdout.ref = STDOUT
rootLogger.appenderRef.abc.ref = abcd

#logger.TestCustomAppender1.level = info
logger.TestCustomAppender1.name = com.log.TestCustomAppender
logger.TestCustomAppender1.appenderRef.stdout.ref = STDOUT
logger.TestCustomAppender1.appenderRef.abc.ref = MyCustomAppender

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...