当 shadowJar 构建 jar 文件时,log4j JsonTemplateLayout 不起作用

log4j JsonTemplateLayout doesn't work when jar file built by shadowJar

我用的是shadowJar 我使用 log4j2,我需要以 json 格式记录所有内容。但是一旦我将 'org.apache.logging.log4j:log4j-layout-template-json:2.17.1' 添加到 build.gradle 文件中,我就会收到此错误:

ERROR StatusLogger Unrecognized format specifier [d]
ERROR StatusLogger Unrecognized conversion specifier [d] starting at position 16 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [thread]
ERROR StatusLogger Unrecognized conversion specifier [thread] starting at position 25 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [level]
ERROR StatusLogger Unrecognized conversion specifier [level] starting at position 35 in conversion pattern.
ERROR StatusLogger Unrecognized format specifier [logger]
ERROR StatusLogger Unrecognized conversion specifier [logger] starting at position 47 in conversion pattern.

如果我删除 log4j-layout-template-json 依赖项,通常的日志记录将开始工作,但我需要使用 JsonTemplateLayout.

以 json 格式登录

这是我的 build.gradle 文件:

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


shadowJar {
    zip64 = true

}

dependencies {

    compileOnly 'org.projectlombok:lombok:1.18.22'
    annotationProcessor 'org.projectlombok:lombok:1.18.22'
    implementation 'org.apache.logging.log4j:log4j-api:2.17.1'
    implementation 'org.apache.logging.log4j:log4j-core:2.17.1'
    implementation 'org.apache.logging.log4j:log4j-layout-template-json:2.17.1'
    implementation 'org.apache.logging.log4j:log4j-slf4j-impl:2.17.1'
    testImplementation 'org.testng:testng:7.4.0'
}


tasks.named('test') {
    // Use TestNG for unit tests.
    useTestNG()
}

我也使用gradle shadowJar 插件构建fat jar。因为我需要这样 运行 我的 jar 文件:java -cp example-0.0.1-all.jar org.example.Main

回答我的问题 这对我有用:

import com.github.jengelman.gradle.plugins.shadow.transformers.Log4j2PluginsCacheFileTransformer

shadowJar{
    transform(Log4j2PluginsCacheFileTransformer)
}