1

I use shadowJar I use log4j2 and I need to log everything in json format. But once I add 'org.apache.logging.log4j:log4j-layout-template-json:2.17.1' into build.gradle file I'm getting this errors:

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.

if I remove log4j-layout-template-json dependency usual logging become working, but I need logging in json format using JsonTemplateLayout.

here is my build.gradle file:

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()
}

I also use gradle shadowJar plugin to build fat jar. Because I'm need to run my jar file this way: java -cp example-0.0.1-all.jar org.example.Main

Guru_1010
  • 574
  • 7
  • 22

1 Answers1

2

Answering my question this is what worked for me:

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

shadowJar{
    transform(Log4j2PluginsCacheFileTransformer)
}
Guru_1010
  • 574
  • 7
  • 22
  • I second this, took me forever to figure out that this was my actual issue. This is particularly relevant if you are building an aws lambda as log4j2 will not work correctly without this. For a similar issue with a bit more of a write up for both gradle and maven check out https://stackoverflow.com/questions/75838785/console-contains-an-invalid-element-or-attribute-jsontemplatelayout-even-after/76681723#76681723. As well as this documentation https://github.com/aws/aws-lambda-java-libs/blob/main/aws-lambda-java-log4j2/README.md – Rhineb Jul 13 '23 at 16:56