且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

包属性未检测到Log4j2(2.1)自定义插件

更新时间:2021-11-14 23:08:23

有两种方法可以让log4j2找到你的自定义插件:通过packages配置属性和javac生成的插件dat文件。

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

选项1: packages属性

有一个旧版本的log4j2,其中的packages属性不再有效,但是这个已在2.0.1中修复。这不应该是一个问题。

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.

要使用此选项,请将插件类的包名称放在包$ c中$ c>属性。例如,如果插件的完全限定类名是 com.mycompany.myproduct.MyPlugin ,则使用

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

status =trace属性将显示控制台上显示的内部log4j2调试语句。这可能有助于解决任何问题,例如,如果找不到您的插件。

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.

选项2:插件数据文件

如果使用类路径中的log4j-core jar进行编译,javac将生成一个log4j2插件dat文件。 Maven将自动将其包含在您的jar中,但如果您不使用maven,则可以手动将此文件包含在jar中。同样,如有必要,请使用status =trace进行故障排除。

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.

配置

完成上述任一操作后,log4j2可以找到您的插件。下一步是正确配置插件。这可能导致问题。

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);
    }
}

现在,您声明了插件的名称 FabLookup ,因此这是您需要在配置中使用的内容。不是类名(虽然它们可以相同)。

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

如果上述内容不足以解决问题,请发布一个更多详细信息,例如您的java代码中如何声明插件以及如何在log4j2.xml中配置插件。

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.