Re: Problems creating custom appender using JPMS service in 3.0.0-beta3

"Piotr P. Karwasz" <[email protected]> Thu, 28 Nov 2024 18:45:09 +0100
Newsgroups gmane.comp.jakarta.log4j.user
Message-ID <[email protected]>
Hi Pavel,

On 28.11.2024 15:47, PavelTurk wrote:
> Hello everyone.
>
> I am trying to create my own appender using JPMS service in 
> 3.0.0-beta3. This is my code: 

Apparently two small bugs made their way to `3.0.0-beta3`([1] and [2]), 
which make compilation under JPMS challenging, but not impossible.

You can find a complete example of a custom appender on my repo:

https://github.com/copernik-eu/bug-reproducibility/tree/main/log4j-core-3.0.0-beta3-jpms

[1] https://github.com/apache/logging-log4j2/issues/3250
[2] https://github.com/apache/logging-log4j2/issues/3251
> import org.apache.logging.log4j.plugins.model.PluginEntry;
> import org.apache.logging.log4j.plugins.model.PluginService;
>
> public class PluginProvider extends PluginService {
>
This is usually not needed, you just need to have 
`log4j-plugin-processor` on the annotation processor path. If you 
disabled the automatic annotation processor detection, you also need to 
add `org.apache.logging.log4j.plugin.processor.PluginProcessor` to the 
list of annotation processors.

The plugin processor generates a class named 
`<pluginPackage>.plugins.Log4jPlugins`, where `<pluginPackage>`:

* Can be provided explicitly using `-ApluginPackage=...`,

* If it is not provided explicitly, it is the common prefix of all your 
Log4j Core plugins.

If you just have `com.example.FooAppender`, the generated 
`PluginService` will be: `com.example.plugins.Log4jPlugins`.

[1] 
https://logging.apache.org/log4j/3.x/components.html#log4j-plugin-processor

> 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)
>
Do you have a message and a cause for this exception? If this is not 
issue #3250 I mentioned before, please share the entire stack trace.


> 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")?

The purpose of namespaces is to distinguish between plugins that have 
the same name, but have different functions. For example the `<Replace>` 
configuration element is in the "Core" namespace, while the `%replace` 
pattern is in the `Converter` space. In general you shouldn't be 
concerned by namespaces:

* The plugins that correspond to XML elements in the configuration file 
MUST be in the "Core" namespace.

Piotr