包属性未检测到Log4j2(2.1)自定义插件 [英] Log4j2 (2.1) custom plugin not detected by packages attribute

查看:499
本文介绍了包属性未检测到Log4j2(2.1)自定义插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的log4j2自定义插件打包到一个单独的jar(仅包含插件类)中,并将其放在应用程序类路径中。但它没有被发现。

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.

我用谷歌搜索发现它是一个错误 - 不再使用packages参数。还有一些链接提供了一些替代方案,其中maven pom.xml和log4j2插件dat文件在上下文中。问题是我不熟悉maven并且不知道如何生成dat文件。我只知道它包含在log4j-2.1-core.jar中,其中现有的log4j2插件在pom.xml中定义。

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 ?

我经历了这个 - Log4j2自定义插件 - 使用Maven Assembly Plugin进行注释处理

但尚不清楚。我正在关注解决方案但不确定如何为自定义插件创建插件dat文件或者我需要在哪里进行更改..

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

推荐答案

有两种方法可以让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.

要使用此选项,请将插件类的包名称放在属性。例如,如果插件的完全限定类名是 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.

这篇关于包属性未检测到Log4j2(2.1)自定义插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆