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

查看:46
本文介绍了包属性未检测到 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 程序集插件

但不清楚.我正在遵循解决方案,但不确定如何为自定义插件创建插件 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 找到您的自定义插件:通过包配置属性和通过 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:包属性

旧版本的 log4j2 的包属性不再起作用,但是这个 是在 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.

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

如果用classpath中的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天全站免登陆