Log4j2 (2.1) custom plugin not detected by packages attribute

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.

Leave a Comment