Problems creating custom appender using JPMS service in 3.0.0-beta3

PavelTurk <[email protected]> Thu, 28 Nov 2024 16:47:59 +0200
Newsgroups gmane.comp.jakarta.log4j.user
Message-ID <[email protected]>
Hello everyone.

I am trying to create my own appender using JPMS service in 3.0.0-beta3. This is my code:

import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.Layout;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.plugins.Configurable;
import org.apache.logging.log4j.plugins.Plugin;

@Plugin("Foo")
@Configurable(elementType = Appender.ELEMENT_TYPE, printObject = true)
public class FooAppender extends AbstractAppender {

     public FooAppender(String name, Filter filter, Layout layout, boolean ignoreExceptions, Property[] properties) {
         super(name, filter, layout, ignoreExceptions, properties);
     }

     @Override
     public void append(LogEvent event) {
         System.out.println("Event:" + event.getMessage());
     }
}

import org.apache.logging.log4j.plugins.model.PluginEntry;
import org.apache.logging.log4j.plugins.model.PluginService;

public class PluginProvider extends PluginService {

     private static PluginEntry[] entries = new PluginEntry[] {
         PluginEntry.builder()
             .setKey("foo")
             .setClassName("com.foo.FooAppender")
             .setName("Foo")
             .setNamespace("Core")//I tried to use my namespace - it didn't help
             .setElementType("appender")
             .setPrintable(true)
             .get(),
     };

     @Override
     public PluginEntry[] getEntries() {
         System.out.println("ENTRIES OK"); // I didn't get this point
         return entries;
     }
}

module-info:

opens com.foo;
provides org.apache.logging.log4j.plugins.model.PluginService with com.foo.PluginProvider;

Config:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error" shutdownHook="disable">
     <Appenders>
          <Foo name="Foo"/>
      </Appenders>
     <Loggers>
         <Root level="debug">
             <AppenderRef ref="Foo"/>
         </Root>
    </Loggers>
</Configuration>

Result:
Exception in thread "main" org.apache.logging.log4j.plugins.di.spi.ReflectionException
     at [email protected]/org.apache.logging.log4j.plugins.di.spi.ReflectionAgent.invokeMethod(ReflectionAgent.java:84)
     at [email protected]/org.apache.logging.log4j.plugins.di.DefaultInstanceFactory.lambda$registerBundleMethod$13(DefaultInstanceFactory.java:306)
     at [email protected]/org.apache.logging.log4j.util.LazyUtil$SafeLazy.value(LazyUtil.java:113)
     at [email protected]/org.apache.logging.log4j.util.Lazy.get(Lazy.java:39)
     at [email protected]/org.apache.logging.log4j.plugins.di.InstanceFactory.getInstance(InstanceFactory.java:126)
     at [email protected]/org.apache.logging.log4j.plugins.di.DefaultInstanceFactory.lambda$getArgumentFactory$9(DefaultInstanceFactory.java:223)
     at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:212)
     at java.base/java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1709)
     at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:556)
     at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:546)
     at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:622)
     at java.base/java.util.stream.AbstractPipeline.evaluateToArrayNode(AbstractPipeline.java:291)
     at java.base/java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:631)
     at java.base/java.util.stream.ReferencePipeline.toArray(ReferencePipeline.java:637)
     at [email protected]/org.apache.logging.log4j.plugins.di.DefaultInstanceFactory.lambda$registerBundleMethod$13(DefaultInstanceFactory.java:305)
     at [email protected]/org.apache.logging.log4j.util.LazyUtil$SafeLazy.value(LazyUtil.java:113)
     at [email protected]/org.apache.logging.log4j.util.Lazy.get(Lazy.java:39)
     at [email protected]/org.apache.logging.log4j.plugins.di.InstanceFactory.getInstance(InstanceFactory.java:115)
     at [email protected]/org.apache.logging.log4j.core.LoggerContext.<init>(LoggerContext.java:149)
     at [email protected]/org.apache.logging.log4j.core.LoggerContext$Builder.build(LoggerContext.java:996)
     at [email protected]/org.apache.logging.log4j.core.selector.AbstractContextSelector.createContext(AbstractContextSelector.java:42)
     at [email protected]/org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.locateContext(ClassLoaderContextSelector.java:231)
     at [email protected]/org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:149)
     at [email protected]/org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:132)
     at [email protected]/org.apache.logging.log4j.core.selector.ClassLoaderContextSelector.getContext(ClassLoaderContextSelector.java:126)
     at [email protected]/org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:151)
     at [email protected]/org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:49)
     at [email protected]/org.apache.logging.log4j.LogManager.getContext(LogManager.java:138)
     at [email protected]/org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:136)
     at [email protected]/org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:58)
     at [email protected]/org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
     at [email protected]/org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:32)
     at [email protected]/org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:426)
     at [email protected]/org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:451)

So, java dies in main class where I initializer my logger.
Could anyone say: 1) how to fix it 2) How to use custom Namespace for my appender in configuration?
For example, if I do PluginEntry.builder().setNamespace("FooNS")?

Best regards, Pavel