Log4j2 (2.1) custom plugin not detected by packages attribute
Asked Answered
S

1

6

I have packaged my log4j2 custom plugin into a separate jar (contains only plugin classes) and have put it in application classpath. But it does not get detected.

I googled found that it's a bug - "packages" parameter is no longer used. Also some links suggested some alternatives where maven pom.xml and a log4j2 plugin dat file comes in context. The problem is that I am not familiar with maven and have no idea on how dat file is generated. I just know that it is included in log4j-2.1-core.jar where existing log4j2 plugins are defined in pom.xml.

Can some-one suggest me how can I make my custom plugin work ?

I went through this - Log4j2 custom plugins - annotation processing with Maven Assembly Plugin

But its not clear. I am following the solution but not sure how plugin dat file created for custom plugin or where exactly I need to make the changes..

Supernal answered 20/4, 2015 at 10:56 Comment(1)
As specified in below answer even javac CustomPlugin.java -cp log4j-core.jar will generate the Log4j2Plugins.dat file. No need for a Maven build or pom.xmlSupernal
D
8

There are two ways to make log4j2 find your custom plugin: through the packages configuration attribute and through a javac-generated plugin dat file.

Option 1: the packages attribute

There was an old version of log4j2 where the packages attribute no longer worked, but this was fixed in 2.0.1. This should not be an issue any more.

To use this option, put the package name of your plugin class in the packages attribute. For example, if the fully qualified class name of your plugin is com.mycompany.myproduct.MyPlugin, then start your log4j2.xml configuration file with

<Configuration status="trace" packages="com.mycompany.myproduct">
  ...

The status="trace" attribute will show internal log4j2 debug statements being shown on the console. This may help troubleshoot any issues, for example if your plugin is not found.

Option 2: the plugin dat file

If you compile with the log4j-core jar in the classpath, javac will generate a log4j2 plugin dat file. Maven will automatically include this in your jar, but if you don't use maven you can include this file into your jar manually. Again, use status="trace" to troubleshoot if necessary.

Configuration

After you have done either of the above, log4j2 can find your plugin. The next step is configuring your plugin correctly. It is possible that this is causing the problem.

Let's assume your plugin is a custom lookup and looks like this:

package com.mycompany.myproduct;

@Plugin(name = "FabLookup", category = StrLookup.CATEGORY)
public class BetterLookup extends AbstractLookup {
    @Override
    public String lookup(final LogEvent event, final String key) {
        return com.mycompany.SomeClass.getValue(key);
    }
}

Now, you declared the name of your plugin FabLookup, so this is what you need to use in the configuration. Not the class name (although it is okay for them to be the same).

An example configuration using your plugin would look like this:

<Configuration status="trace" packages="com.mycompany.myproduct">
...
<Appenders>
<RollingFile name="RollingFile" fileName="logs/app.log"
             filePattern="logs/$${date:yyyy-MM}/app-%d{MM-dd-yyyy}.log.gz">

  <!-- use custom lookups to access arbitrary internal system info -->
  <PatternLayout header="${FabLookup:key1} ${FabLookup:key2}">
    <Pattern>%d %m%n</Pattern>
  </PatternLayout>
  <Policies>
    <TimeBasedTriggeringPolicy />
  </Policies>
</RollingFile>
...

If the above is not sufficient to solve the problem, please post a bit more detail like how your plugin is declared in your java code and how it is configured in your log4j2.xml.

Dockyard answered 21/4, 2015 at 1:34 Comment(5)
The custom plugin seems to work now.. Used maven and pom.xml as defined here - github.com/mushkevych/log4j2plugin/blob/…Supernal
Now I guess there are two log4jPlugin.dat files, one in core & other in custom jar hope this won't cause any issues in future. For now seems to work fine. @Remko Popma Thanks for a descriptive answerSupernal
Plugin dat files in separate jar files will be processes separately. This will not cause issues unless you declare a plugin with the same name as a built-in plugin.Dockyard
Option 1 worked for me- very easy. If you have multiple packages you can separate by comma ("com.acme.logger,com.my.logger")Bodice
Note if you're using newer versions of Gradle, you have to add this to your dependencies to generate the dat file annotationProcessor('org.apache.logging.log4j:log4j-core:2.13.3')Addictive

© 2022 - 2024 — McMap. All rights reserved.