Java Log4J2配置失败:带有Spring AMQP/Spring Rabbit的Log4J2 RabbitMQ追加器

Java Log4J2配置失败:带有Spring AMQP/Spring Rabbit的Log4J2 RabbitMQ追加器,java,rabbitmq,log4j2,spring-amqp,spring-rabbit,Java,Rabbitmq,Log4j2,Spring Amqp,Spring Rabbit,我正试图在一个已经运行并记录日志的java应用程序中,将Log4J2 Appender集成到RabbitMQ中 该应用程序是作为gradle项目构建的。在集成spring rabbit之前,build.gradle文件如下所示: group 'Name' version '1.18.7' apply plugin: 'java' apply plugin: 'com.github.johnrengelman.shadow' sourceCompatibility = 1.8 reposit

我正试图在一个已经运行并记录日志的java应用程序中,将Log4J2 Appender集成到RabbitMQ中

该应用程序是作为gradle项目构建的。在集成spring rabbit之前,
build.gradle
文件如下所示:

group 'Name'
version '1.18.7'

apply plugin: 'java'
apply plugin: 'com.github.johnrengelman.shadow'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    [...]
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.10.0'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.10.0'
    compile group: 'com.lmax', name: 'disruptor', version: '3.3.7'
    [...]
}

jar {
    manifest {
        attributes 'Main-Class': 'the.main.Clazz'
    }
}


buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath group: 'com.github.jengelman.gradle.plugins', name: 'shadow', version: '2.0.2'
    }
}
位于
src/resources
文件夹中的log4j2.xml文件包含以下内容(在rabbitMQ之前):

…日志开始表现得很奇怪。从IDE开始,一切都很好。将以下附加程序添加到
log4j2.xml
可以正常工作:

    <RabbitMQ name="RABBIT_MQ"
              host="my.host.name" port="5672" user="logger" password="logger" virtualHost="loggerhost"
              exchange="logs" exchangeType="fanout" declareExchange="false"
              applicationId="app-xyz" routingKeyPattern="%X{applicationId}.%c.%p"
              contentType="text/plain" contentEncoding="UTF-8" generateId="true" deliveryMode="NON_PERSISTENT"
              charset="UTF-8"
              senderPoolSize="3" maxSenderRetries="5">
        <PatternLayout>
            <Pattern>%d [%-6p] %C{1}.%M(%F:%L) - %m%n%throwable</Pattern>
        </PatternLayout>
    </RabbitMQ>
当我使用
-Dlog4j2.debug
选项启动应用程序JAR时,我看到了很多消息,有些消息似乎说无法加载配置(仍然在同一位置)。这里有一段摘录:

DEBUG StatusLogger Using configurationFactory org.apache.logging.log4j.core.config.ConfigurationFactory$Factory@2a33fae0
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
我甚至使用了
-Dlog4j.configurationFile
选项,直接指向文件系统中的XML,但结果保持不变


同样,从IDE而不是通过
java-jar…
启动应用程序也可以。在我看来,
springrabbitmq
依赖项带来了一些额外的log4j内容,干扰了我的配置。我完全是在阴暗的水域钓鱼。

所以,经过大量的研究,我可以用以下回答我的问题

仅仅添加springrabbitmq
org.springframework.amqp:springrabbit
依赖项,让Log4J2配置失败是不够的。它是与另一个依赖项的组合,在我的示例中没有列出:
com.fasterxml.jackson.core:jackson-databind

总而言之。这不起作用:

compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.3'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.10.0'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.10.0'
compile group: 'com.lmax', name: 'disruptor', version: '3.3.7'
compile group: 'org.springframework.amqp', name: 'spring-rabbit', version: '2.0.2.RELEASE'
但这是可行的(没有弹簧):

这是可行的(没有Jackson):

所以Jackson和Spring的组合打破了Log4J2。有趣的是,如果没有Jackson,我的程序也能正常工作,即使Jackson被大量使用。让我们来看看,
gradledependecies
有什么要说的:

compile - Dependencies for source set 'main' (deprecated, use 'implementation ' instead).
+--- org.apache.logging.log4j:log4j-api:2.10.0
+--- org.apache.logging.log4j:log4j-core:2.10.0
|    \--- org.apache.logging.log4j:log4j-api:2.10.0
+--- com.lmax:disruptor:3.3.7
\--- org.springframework.amqp:spring-rabbit:2.0.2.RELEASE
     +--- org.springframework.amqp:spring-amqp:2.0.2.RELEASE
     |    \--- org.springframework:spring-core:5.0.3.RELEASE
     |         \--- org.springframework:spring-jcl:5.0.3.RELEASE
     +--- com.rabbitmq:amqp-client:5.1.2
     |    \--- org.slf4j:slf4j-api:1.7.25 -> 1.8.0-alpha2
     +--- com.rabbitmq:http-client:1.3.1.RELEASE
     |    +--- org.apache.httpcomponents:httpclient:4.5.3
     |    |    +--- org.apache.httpcomponents:httpcore:4.4.6
     |    |    +--- commons-logging:commons-logging:1.2
     |    |    \--- commons-codec:commons-codec:1.9 -> 1.11
     |    \--- com.fasterxml.jackson.core:jackson-databind:2.9.2
     |         +--- com.fasterxml.jackson.core:jackson-annotations:2.9.0
     |         \--- com.fasterxml.jackson.core:jackson-core:2.9.2
     +--- org.springframework:spring-context:5.0.3.RELEASE
     |    +--- org.springframework:spring-aop:5.0.3.RELEASE
     |    |    +--- org.springframework:spring-beans:5.0.3.RELEASE
     |    |    |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     |    |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    +--- org.springframework:spring-core:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-expression:5.0.3.RELEASE
     |         \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     +--- org.springframework:spring-messaging:5.0.3.RELEASE
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     +--- org.springframework:spring-tx:5.0.3.RELEASE
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     +--- org.springframework:spring-web:5.0.3.RELEASE
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     \--- org.springframework.retry:spring-retry:1.2.1.RELEASE
          \--- org.springframework:spring-core:4.3.9.RELEASE -> 5.0.3.RELEASE (*)
哦,等等,春天的AMQP里还有一个杰克逊。好吧,这就解决了一个谜,为什么我在移除依赖项后仍然可以使用Jackson。但这对我来说是非常不兼容的,并且依赖于SpringAMQP包为我提供一个核心依赖。所以我最后做的是,这似乎是可行的:

compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.3'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.10.0'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.10.0'
compile group: 'com.lmax', name: 'disruptor', version: '3.3.7' version: '2.12.0'
compile (group: 'org.springframework.amqp', name: 'spring-rabbit', version: '2.0.2.RELEASE') {
    exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind'
}
依赖关系树现在看起来如下所示:

compile - Dependencies for source set 'main' (deprecated, use 'implementation ' instead).
+--- com.fasterxml.jackson.core:jackson-databind:2.9.3
|    +--- com.fasterxml.jackson.core:jackson-annotations:2.9.0
|    \--- com.fasterxml.jackson.core:jackson-core:2.9.3
+--- org.apache.logging.log4j:log4j-api:2.10.0
+--- org.apache.logging.log4j:log4j-core:2.10.0
|    \--- org.apache.logging.log4j:log4j-api:2.10.0
+--- com.lmax:disruptor:3.3.7
\--- org.springframework.amqp:spring-rabbit:2.0.2.RELEASE
     +--- org.springframework.amqp:spring-amqp:2.0.2.RELEASE
     |    \--- org.springframework:spring-core:5.0.3.RELEASE
     |         \--- org.springframework:spring-jcl:5.0.3.RELEASE
     +--- com.rabbitmq:amqp-client:5.1.2
     |    \--- org.slf4j:slf4j-api:1.7.25 -> 1.8.0-alpha2
     +--- com.rabbitmq:http-client:1.3.1.RELEASE
     |    \--- org.apache.httpcomponents:httpclient:4.5.3
     |         +--- org.apache.httpcomponents:httpcore:4.4.6
     |         +--- commons-logging:commons-logging:1.2
     |         \--- commons-codec:commons-codec:1.9 -> 1.11
     +--- org.springframework:spring-context:5.0.3.RELEASE
     |    +--- org.springframework:spring-aop:5.0.3.RELEASE
     |    |    +--- org.springframework:spring-beans:5.0.3.RELEASE
     |    |    |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     |    |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    +--- org.springframework:spring-core:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-expression:5.0.3.RELEASE
     |         \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     +--- org.springframework:spring-messaging:5.0.3.RELEASE
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     +--- org.springframework:spring-tx:5.0.3.RELEASE
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     +--- org.springframework:spring-web:5.0.3.RELEASE
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     \--- org.springframework.retry:spring-retry:1.2.1.RELEASE
          \--- org.springframework:spring-core:4.3.9.RELEASE -> 5.0.3.RELEASE (*)
我必须承认,我不完全理解为什么两个Jackson依赖项的组合会降低Log4J2配置,但现在它可以使用这种策略:

  • 显式声明Jackson依赖项(使应用程序使用Jackson时透明)
  • 从Spring依赖项中排除Jackson
我真正不明白的是:为什么所有的组合都能在IDE中工作,但后来在运行JAR时失败了?

这是一个已知的关于log4j的问题,三年前就有报道。当您使用shadowjar插件时,它会出现。参见shadowjar问题

您可以阅读这两个标签以获得更好的理解,但如果您正在寻找点击式解决方案,只需使用gradle插件:

plugins {
  id "com.github.johnrengelman.shadow" version "2.0.2"
  id "de.sebastianboegl.shadow.transformer.log4j" version "2.2.0"
}

。。。或者回到log4j 2.0.2。

您能在GitHub上与我们分享我们这边要玩的项目吗?以及一些如何复制的说明。ThanksTry使用
-verbose
JVM参数并比较log4j东西的类加载-spring rabbit依赖项是
可选的
,因此它不应该引入任何log4j JAR。请尝试从
RabbitMQ
appender配置中删除
PatternLayout
元素。
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.10.0'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.10.0'
compile group: 'com.lmax', name: 'disruptor', version: '3.3.7'
compile group: 'org.springframework.amqp', name: 'spring-rabbit', version: '2.0.2.RELEASE'
compile - Dependencies for source set 'main' (deprecated, use 'implementation ' instead).
+--- org.apache.logging.log4j:log4j-api:2.10.0
+--- org.apache.logging.log4j:log4j-core:2.10.0
|    \--- org.apache.logging.log4j:log4j-api:2.10.0
+--- com.lmax:disruptor:3.3.7
\--- org.springframework.amqp:spring-rabbit:2.0.2.RELEASE
     +--- org.springframework.amqp:spring-amqp:2.0.2.RELEASE
     |    \--- org.springframework:spring-core:5.0.3.RELEASE
     |         \--- org.springframework:spring-jcl:5.0.3.RELEASE
     +--- com.rabbitmq:amqp-client:5.1.2
     |    \--- org.slf4j:slf4j-api:1.7.25 -> 1.8.0-alpha2
     +--- com.rabbitmq:http-client:1.3.1.RELEASE
     |    +--- org.apache.httpcomponents:httpclient:4.5.3
     |    |    +--- org.apache.httpcomponents:httpcore:4.4.6
     |    |    +--- commons-logging:commons-logging:1.2
     |    |    \--- commons-codec:commons-codec:1.9 -> 1.11
     |    \--- com.fasterxml.jackson.core:jackson-databind:2.9.2
     |         +--- com.fasterxml.jackson.core:jackson-annotations:2.9.0
     |         \--- com.fasterxml.jackson.core:jackson-core:2.9.2
     +--- org.springframework:spring-context:5.0.3.RELEASE
     |    +--- org.springframework:spring-aop:5.0.3.RELEASE
     |    |    +--- org.springframework:spring-beans:5.0.3.RELEASE
     |    |    |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     |    |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    +--- org.springframework:spring-core:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-expression:5.0.3.RELEASE
     |         \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     +--- org.springframework:spring-messaging:5.0.3.RELEASE
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     +--- org.springframework:spring-tx:5.0.3.RELEASE
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     +--- org.springframework:spring-web:5.0.3.RELEASE
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     \--- org.springframework.retry:spring-retry:1.2.1.RELEASE
          \--- org.springframework:spring-core:4.3.9.RELEASE -> 5.0.3.RELEASE (*)
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.3'
compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.10.0'
compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.10.0'
compile group: 'com.lmax', name: 'disruptor', version: '3.3.7' version: '2.12.0'
compile (group: 'org.springframework.amqp', name: 'spring-rabbit', version: '2.0.2.RELEASE') {
    exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind'
}
compile - Dependencies for source set 'main' (deprecated, use 'implementation ' instead).
+--- com.fasterxml.jackson.core:jackson-databind:2.9.3
|    +--- com.fasterxml.jackson.core:jackson-annotations:2.9.0
|    \--- com.fasterxml.jackson.core:jackson-core:2.9.3
+--- org.apache.logging.log4j:log4j-api:2.10.0
+--- org.apache.logging.log4j:log4j-core:2.10.0
|    \--- org.apache.logging.log4j:log4j-api:2.10.0
+--- com.lmax:disruptor:3.3.7
\--- org.springframework.amqp:spring-rabbit:2.0.2.RELEASE
     +--- org.springframework.amqp:spring-amqp:2.0.2.RELEASE
     |    \--- org.springframework:spring-core:5.0.3.RELEASE
     |         \--- org.springframework:spring-jcl:5.0.3.RELEASE
     +--- com.rabbitmq:amqp-client:5.1.2
     |    \--- org.slf4j:slf4j-api:1.7.25 -> 1.8.0-alpha2
     +--- com.rabbitmq:http-client:1.3.1.RELEASE
     |    \--- org.apache.httpcomponents:httpclient:4.5.3
     |         +--- org.apache.httpcomponents:httpcore:4.4.6
     |         +--- commons-logging:commons-logging:1.2
     |         \--- commons-codec:commons-codec:1.9 -> 1.11
     +--- org.springframework:spring-context:5.0.3.RELEASE
     |    +--- org.springframework:spring-aop:5.0.3.RELEASE
     |    |    +--- org.springframework:spring-beans:5.0.3.RELEASE
     |    |    |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     |    |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    +--- org.springframework:spring-core:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-expression:5.0.3.RELEASE
     |         \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     +--- org.springframework:spring-messaging:5.0.3.RELEASE
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     +--- org.springframework:spring-tx:5.0.3.RELEASE
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     +--- org.springframework:spring-web:5.0.3.RELEASE
     |    +--- org.springframework:spring-beans:5.0.3.RELEASE (*)
     |    \--- org.springframework:spring-core:5.0.3.RELEASE (*)
     \--- org.springframework.retry:spring-retry:1.2.1.RELEASE
          \--- org.springframework:spring-core:4.3.9.RELEASE -> 5.0.3.RELEASE (*)
plugins {
  id "com.github.johnrengelman.shadow" version "2.0.2"
  id "de.sebastianboegl.shadow.transformer.log4j" version "2.2.0"
}