Log4j2 (2.1) custom plugin not detected by package

2019-01-15 15:14发布

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..

1条回答
你好瞎i
2楼-- · 2019-01-15 15:26

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.

查看更多
登录 后发表回答