Log4j2自定义插件 - 与Maven Assembly插件注释处理 [英] Log4j2 custom plugins - annotation processing with Maven Assembly Plugin

查看:1628
本文介绍了Log4j2自定义插件 - 与Maven Assembly插件注释处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是很熟悉Maven的,我开始使用它只是在昨天,但我喜欢它。在项目中,我使用 Log4j2 库记录和因先进的插件insufficiecy(比如追加​​程序,转换器)的我需要使用自定义插件即可。 的log4j的API log4j的核心(也有一帮其他库)被添加为依赖关系的 pom.xml的的与我的项目有关。其实我使用的Log4j的2.0版本。

Log4j的使用注释处理标记为 @Plugin pre-负载类。据我所知,在log4j的旧版本,额外的插件进入了中指定的的pom.xml 的触发插件处理,或自定义插件包必须输入到<$ C在配置文件中($ C>包属性的https://logging.apache.org/log4j/2.x/manual/configuration.html#ConfigurationSyntax).但是,这是不是因为2.0-RC2的支持。

在2.0这应该是自动完成的,只要 log4j的核心可用于建筑物的引擎。有一个文件的 Log4j2Plugins.dat 的中的的myproject-0.0.1-SNAPSHOT.jar / META-INF /组织/阿帕奇/日志/ log4j的/核心/配置/插件/ 的包含我的自定义插件的映射 - 这是确定

有关使用Maven构建我也用的Maven Assembly插件。它的目标被绑定到阶段。包装项目后我自然有在目标一个额外的jar 的目录 - 的myproject-0.0.1-SNAPSHOT-JAR与 - dependencies.jar 的。然而, Log4j2Plugins.dat 的这个jar文件包含原始插件映射,相同的文件作为log4j的核心库。这就是问题所在,因为它不抱到我的自定义插件的任何引用。看来,从文件中的的myproject-0.0.1-SNAPSHOT.jar 的被覆盖从log4j的库中的原始文件,但我不知道有什么情况。

所以,当我运行的的myproject-0.0.1-SNAPSHOT-JAR与 - dependencies.jar 的是,log4j无法从我的项目中找到插件类。我认为的的myproject-0.0.1-SNAPSHOT.jar 的会跑好了,但没有依赖关系,我不能运行它。

在配置属性应该重新启用在2.0.1版本中,但如果我不希望等待释放,我不得不使用注释处理方法。

你有一个想法如何解决它?


我试图与发布运行它的的log4j,其中配置元素的属性是可用的2.0-RC1 的。其结果是: log4j的成功加载的类我的自定义插件即可。然而,有这么多其他错误(在这个特定版本相比发生),使程序更加无法使用。

这是一个很好的点,确保我,如果属性将在下一版本2.0.1中启用,我的插件的工作。它应该根据这个问题跟踪复职: https://issues.apache.org/jira /浏览/ LOG4J2-741


加了我的的pom.xml

 &LT;项目的xmlns =htt​​p://maven.apache.org/POM/4.0.0的xmlns:XSI =htt​​p://www.w3.org/2001 / XML模式实例
  XSI:的schemaLocation =htt​​p://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">
  &LT; modelVersion&GT; 4.0.0&LT; / modelVersion&GT;  &LT;&的groupId GT; com.jjurm&LT; /的groupId&GT;
  &LT;&的artifactId GT; twbot&LT; / artifactId的&GT;
  &LT;&版GT; 0.0.1-SNAPSHOT&LT; /版本&GT;
  &LT;包装和GT;&罐子LT; /包装&GT;  &LT;名称&gt;&twbot LT; /名称&gt;
  &LT; URL&GT; HTTP://maven.apache.org< / URL&GT;  &LT;性状&gt;
    &LT; project.build.sourceEncoding&GT; UTF-8&LT; /project.build.sourceEncoding>
  &LT; /性状&gt;  &LT;建立&GT;
    &LT; pluginManagement&GT;
      &LT;&插件GT;
        &LT;&插件GT;
          &LT;&的groupId GT; org.apache.maven.plugins&LT; /的groupId&GT;
          &LT;&的artifactId GT; Maven的编译器插件&LT; / artifactId的&GT;
          &LT;&版GT; 3.1&LT; /版本&GT;
          &LT;结构&gt;
            &lt;信源&GT; 1.7 LT; /源&GT;
            &lt;目标&GT; 1.7 LT; /目标与GT;
          &LT; /结构&gt;
        &LT; /插件&GT;
      &LT; /插件&GT;
    &LT; / pluginManagement&GT;
    &LT;&插件GT;
      &LT;&插件GT;
        &LT;&的artifactId GT; Maven的组装插件&LT; / artifactId的&GT;
        &LT;&版GT; 2.4&LT; /版本&GT;
        &LT;结构&gt;
          &LT; descriptorRefs&GT;
            &LT; descriptorRef&GT; JAR-具有依赖性和LT; / descriptorRef&GT;
          &LT; / descriptorRefs&GT;
          &LT;归档和GT;
            &LT;&舱单GT;
              &LT; mainClass&GT; com.jjurm.twbot.system.Run&LT; / mainClass&GT;
            &LT; /清单&GT;
          &LT; /存档&GT;
        &LT; /结构&gt;
        &LT;&执行GT;
          &LT;执行与GT;
            &LT;&ID GT;使组装和LT; / ID&GT; &LT;! - 这是用于继承合并 - &GT;
            &LT;阶段&gt;包装及LT; /阶段&gt; &LT;! - 绑定到包装阶段 - &GT;
            &LT;目标&GT;
              &LT;&目标GT;单&LT; /目标&GT;
            &LT; /目标&GT;
            &LT;结构&gt;
              &LT; descriptorRefs&GT;
                &LT; descriptorRef&GT; JAR-具有依赖性和LT; / descriptorRef&GT;
              &LT; / descriptorRefs&GT;
              &LT;归档和GT;
                &LT;&舱单GT;
                  &LT; mainClass&GT; com.jjurm.twbot.system.Run&LT; / mainClass&GT;
                &LT; /清单&GT;
              &LT; /存档&GT;
            &LT; /结构&gt;
          &LT; /执行&GT;
        &LT; /处决&GT;
      &LT; /插件&GT;
    &LT; /插件&GT;
  &LT; /构建&GT;  &LT;依赖和GT;
    &LT;&依赖性GT;
      &LT;&的groupId GT;的JUnit&LT; /的groupId&GT;
      &LT;&的artifactId GT;的JUnit&LT; / artifactId的&GT;
      &LT;&版GT; 3.8.1&LT; /版本&GT;
      &LT;&范围GT;试验&LT; /&范围GT;
    &LT; /依赖性&GT;
    &LT;&依赖性GT;
        &LT;&的groupId GT; org.mariadb.jdbc&LT; /的groupId&GT;
        &LT;&的artifactId GT; MariaDB的-Java的客户端&LT; / artifactId的&GT;
        &LT;&版GT; 1.1.7&LT; /版本&GT;
    &LT; /依赖性&GT;
    &LT;&依赖性GT;
        &LT;&的groupId GT;乔达时间&LT; /的groupId&GT;
        &LT;&的artifactId GT;乔达时间&LT; / artifactId的&GT;
        &LT;&版GT; 2.3&LT; /版本&GT;
    &LT; /依赖性&GT;
    &LT;&依赖性GT;
        &LT;&的groupId GT;公共资源配置和LT; /的groupId&GT;
        &LT;&的artifactId GT;公共资源配置和LT; / artifactId的&GT;
        &LT;&版GT; 1.10; /版本&GT;
    &LT; /依赖性&GT;
    &LT;&依赖性GT;
        &LT;&的groupId GT; net.snaq&LT; /的groupId&GT;
        &LT;&的artifactId GT; DBPool对&LT; / artifactId的&GT;
        &LT;&版GT; 6.0&LT; /版本&GT;
    &LT; /依赖性&GT;
    &LT;&依赖性GT;
        &LT;&的groupId GT; org.fusesource.jansi&LT; /的groupId&GT;
        &LT;&的artifactId GT; jansi&LT; / artifactId的&GT;
        &LT;&版GT; 1.11 LT; /版本&GT;
    &LT; /依赖性&GT;
    &LT;&依赖性GT;
        &LT;&的groupId GT; org.apache.logging.log4j&LT; /的groupId&GT;
        &LT;&的artifactId GT; log4j的核心&LT; / artifactId的&GT;
        &LT;&版GT; 2.0&LT; /版本&GT;
    &LT; /依赖性&GT;
    &LT;&依赖性GT;
        &LT;&的groupId GT; org.apache.logging.log4j&LT; /的groupId&GT;
        &LT;&的artifactId GT;的log4j的API&LT; / artifactId的&GT;
        &LT;&版GT; 2.0&LT; /版本&GT;
    &LT; /依赖性&GT;
    &LT;&依赖性GT;
        &LT;&的groupId GT; org.slf4j&LT; /的groupId&GT;
        &LT;&的artifactId GT; SLF4J的API&LT; / artifactId的&GT;
        &LT;&版GT; 1.7.7&L​​T; /版本&GT;
    &LT; /依赖性&GT;
    &LT;&依赖性GT;
        &LT;&的groupId GT; org.apache.logging.log4j&LT; /的groupId&GT;
        &LT;&的artifactId GT;的log4j-SLF4J-implement执行&LT; / artifactId的&GT;
        &LT;&版GT; 2.0&LT; /版本&GT;
    &LT; /依赖性&GT;
    &LT;&依赖性GT;
        &LT;&的groupId GT;公地 - 的JXPath LT; /的groupId&GT;
        &LT;&的artifactId GT;公地 - 的JXPath LT; / artifactId的&GT;
        &LT;&版GT; 1.3&LT; /版本&GT;
    &LT; /依赖性&GT;
    &LT;&依赖性GT;
        &LT;&的groupId GT; net.sourceforge.htmlunit&LT; /的groupId&GT;
        &LT;&的artifactId GT;&的HtmlUnit LT; / artifactId的&GT;
        &LT;&版GT; 2.15&LT; /版本&GT;
    &LT; /依赖性&GT;
  &LT; /依赖和GT;
&LT; /项目&GT;


解决方案

我想从包装的依赖复制到罐子的问题造成的。做一个快速潜入code,它看起来像插件处理器改写为每套它处理插件的插件dat文件。我的猜测是,在包装过程中,您的自定义插件进行处理并写入dat文件,然后覆盖,当你的log4j依赖性列入包处理。有可能是更好的解决方案,但是从我的头顶,我会建议你做下列之一:


  1. 不要打包依赖到你的罐子。只是打包项目,然后包括你的classpath的依赖,当你执行。即使你想在一个单一的便携式罐包装的一切,这样做将允许你,至少可以确认,如果你的插件被覆盖,或者还有别的东西错了。


  2. 从您的主项目分别创建自定义插件单独的项目,将其打包,然后包括导致罐子作为一个依赖。与选项1,请确保您不包括在这个包log4j的罐子。一旦你创建你的自定义插件罐子,你可以将它与其他依赖于你的主要Jar包它,它应该能正常运行,因为您的自定义插件的罐子都会有自己的插件dat文件。


祝你好运!

I am not very familiar with Maven, I started using it just yesterday, but I like it. In my project I use Log4j2 library for logging and because of insufficiecy of advanced plugins (like appenders, converters) I need to use custom plugins. log4j-api and log4j-core (also with a bunch of other libraries) are added as dependencies in pom.xml associated with my project. Actually I am using version 2.0 of Log4j.

Log4j uses annotation processing to pre-load classes marked as @Plugin. As far as I know, in older releases of log4j, additional plugin entry had to be specified in pom.xml to trigger plugin processing, or the packages with custom plugins had to be typed into the packages attribute in the configuration file (https://logging.apache.org/log4j/2.x/manual/configuration.html#ConfigurationSyntax). But this is not supported since 2.0-rc2.

In v2.0 this should be done automatically, as long as log4j-core is available to the building engine. There is a file Log4j2Plugins.dat in myproject-0.0.1-SNAPSHOT.jar/META-INF/org/apache/logging/log4j/core/config/plugins/ that contains mappings of my custom plugins - that's OK.

For building with Maven I use also Maven Assembly Plugin. Its goal single is binded to package phase. After packaging the project I naturally have one additional jar in the target directory - myproject-0.0.1-SNAPSHOT-jar-with-dependencies.jar. However, Log4j2Plugins.dat file in this jar contains mappings of original plugins, the same file as in the log4j-core library. And that's the problem, since it doesn't hold any references to my custom plugins. It seems that the file from myproject-0.0.1-SNAPSHOT.jar is being overwritten with the original file from the log4j library, but I am not sure what's the case.

So when I run myproject-0.0.1-SNAPSHOT-jar-with-dependencies.jar, log4j can't find the plugin classes from my project. I think that myproject-0.0.1-SNAPSHOT.jar would run ok, but I can't run it without the dependencies.

The packages attribute in configuration should be re-enabled in 2.0.1 release, but if I don't want to wait for the release, I have to use the annotation processing method.

Do you have an idea how to fix it?


I tried to run it with release 2.0-rc1 of log4j, where the packages attribute of the configuration element was usable. The result is: log4j successfully loaded the class of my custom plugin. However, there were so many other errors (that arised in this specific release) that make the program even more unusable.

This is one good point, that ensures me that if the packages attribute will be enabled in next release 2.0.1, my plugin will work. It should be reinstated according to this issue tracking: https://issues.apache.org/jira/browse/LOG4J2-741


Added my pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.jjurm</groupId>
  <artifactId>twbot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>twbot</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.1</version>
          <configuration>
            <source>1.7</source>
            <target>1.7</target>
          </configuration>
        </plugin>
      </plugins>
    </pluginManagement>
    <plugins>
      <plugin>
        <artifactId>maven-assembly-plugin</artifactId>
        <version>2.4</version>
        <configuration>
          <descriptorRefs>
            <descriptorRef>jar-with-dependencies</descriptorRef>
          </descriptorRefs>
          <archive>
            <manifest>
              <mainClass>com.jjurm.twbot.system.Run</mainClass>
            </manifest>
          </archive>
        </configuration>
        <executions>
          <execution>
            <id>make-assembly</id> <!-- this is used for inheritance merges -->
            <phase>package</phase> <!-- bind to the packaging phase -->
            <goals>
              <goal>single</goal>
            </goals>
            <configuration>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              <archive>
                <manifest>
                  <mainClass>com.jjurm.twbot.system.Run</mainClass>
                </manifest>
              </archive>
            </configuration>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
        <version>1.1.7</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.3</version>
    </dependency>
    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.10</version>
    </dependency>
    <dependency>
        <groupId>net.snaq</groupId>
        <artifactId>dbpool</artifactId>
        <version>6.0</version>
    </dependency>
    <dependency>
        <groupId>org.fusesource.jansi</groupId>
        <artifactId>jansi</artifactId>
        <version>1.11</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>2.0</version>
    </dependency>
    <dependency>
        <groupId>commons-jxpath</groupId>
        <artifactId>commons-jxpath</artifactId>
        <version>1.3</version>
    </dependency>
    <dependency>
        <groupId>net.sourceforge.htmlunit</groupId>
        <artifactId>htmlunit</artifactId>
        <version>2.15</version>
    </dependency>
  </dependencies>
</project>

解决方案

I think the problem stems from packaging the dependencies into the jar. Doing a quick dive into the code, it looks like the plugin processor overwrites the plugin dat file for each set of plugins it handles. My guess is that during the packaging process, your custom plugins are processed and written to the dat file, and then overwritten when your log4j dependency is processed for inclusion in the package. There may be better solutions, but off the top of my head I would suggest that you do one of the following:

  1. Do not package the dependencies into your jar. Just package your project and then include the dependencies on your classpath when you execute. Even if you want to package everything in a single portable jar, doing this would allow you to at least confirm if your plugins are being overwritten, or if there is something else wrong.

  2. Create a separate project for your custom plugins, package it separately from your main project, and then include the resulting jar as a dependency. As with option 1, make sure that you do not include the log4j jars in this package. Once you have created your custom plugin jar, you can package it along with the other dependencies in your main jar and it should work fine since your custom plugin jar will have its own plugin dat file.

Good luck!

这篇关于Log4j2自定义插件 - 与Maven Assembly插件注释处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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