Log4j2 (2.1) custom plugin not detected by package attribute

I packed my own log4j2 plugin in a separate jar (contains only the plugin classes) and put it in the application class path. But he does not show up.

I googled found this to be an error - the packages option is no longer used. Also some links suggested some alternatives where maven pom.xml and the log4j2 plugin data file go into context. The problem is that I am not familiar with maven and have no idea how the dat file is created. I just know that it is included in log4j-2.1-core.jar, where existing log4j2 plugins are defined in pom.xml.

Can someone suggest me how can I make my custom plugin?

I went through this - Log4j2 Custom Plugins - Annotation Processing with Maven Build Plugin

But this is not clear. I follow the solution, but I don’t know how the plug-in file was created for the custom plug-in or where exactly I need to make changes.

+5
source share
1 answer

There are two ways to get log4j2 to find your custom plugin: through the package configuration attribute and through the plugins data file created by javac.

Option 1: Package Attribute

There was an old version of log4j2 where the package attribute no longer worked, but this one was fixed in version 2.0.1. This should no longer be a problem.

To use this parameter, 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 , run the log4j2.xml configuration file using

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

The status="trace" attribute will display the log4j2 internal debugging commands displayed on the console. This can help fix any problems, for example, if your plugin is not found.

Option 2: plugin file

If you compile the log4j-core kernel in the classpath, javac will generate the log4j2 plugin data file. Maven will automatically include this in your jar, but if you are not using maven, you can manually include this file in your jar. Again, use status = "trace" to troubleshoot if necessary.

Configuration

After you have completed one of the above actions, log4j2 can find your plugin. The next step is to configure the plugin correctly. Perhaps this is causing the problem.

Suppose your plugin is a regular search 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 have declared the name of your FabLookup plugin, so this is what you need to use in the configuration. Not the name of the class (although everything is the same for them).

An example configuration using your plugin will 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 enough to solve the problem, write in a little more detail, for example, how your plugin is declared in your java code and how it is configured in your log4j2.xml.

+4
source

Source: https://habr.com/ru/post/973187/


All Articles