Skip to content

Commit 8ac9865

Browse files
ES-2476: Fix JSON format logging in Corda CLI (#6288)
JSON format logging in Corda CLI got broken after #6169 This change brings in the missing pieces of Gradle configuration.
1 parent ea5ed3b commit 8ac9865

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

tools/corda-cli/app/build.gradle

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
plugins {
22
id 'org.jetbrains.kotlin.jvm'
3-
id 'application'
43
}
54

65
group = 'net.corda.cli'
76

8-
mainClassName = 'net.corda.cli.application.AppKt'
9-
10-
application {
11-
mainClass = mainClassName
12-
}
13-
147
dependencies {
158
implementation libs.kotlin.stdlib
169
implementation libs.log4j.slf4j
10+
// This alone isn't enough to enable JSON logging, please see :tools:corda-cli:jar task for details
11+
runtimeOnly libs.log4j.layout.json
1712

1813
implementation "info.picocli:picocli:$picocliVersion"
1914

tools/corda-cli/build.gradle

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,19 +142,45 @@ if (project.hasProperty(S3_BUCKET_URI_PROPERTY)) {
142142
}
143143
}
144144

145+
// This is a workaround for https://issues.apache.org/jira/browse/LOG4J2-954,
146+
// which causes the Log4J2 plugins cache to be overwritten by the last one on the classpath, making it incompatible with a fat jar.
147+
// There's a similar logic in `corda.common-app`, but we don't want to use OSGi approach in Corda CLI.
148+
def mergeLog4j2Plugins = tasks.register('mergeLog4j2Plugins') {
149+
dependsOn configurations.runtimeClasspath
150+
inputs.files(configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) })
151+
def outputDir = layout.buildDirectory.dir('log4j')
152+
outputs.dir(outputDir)
153+
154+
doLast {
155+
def inputFiles = inputs.files.getFiles().findAll { it.name == 'Log4j2Plugins.dat' }.collect { it.toURI().toURL() }
156+
if (inputFiles) {
157+
def combinedCache = new org.apache.logging.log4j.core.config.plugins.processor.PluginCache()
158+
combinedCache.loadCacheFiles(Collections.enumeration(inputFiles))
159+
def outputFile = outputDir.get().file('META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat').asFile
160+
outputFile.parentFile.mkdirs()
161+
try (OutputStream out = new FileOutputStream(outputFile)) {
162+
combinedCache.writeCache(out)
163+
}
164+
}
165+
}
166+
}
167+
145168
jar {
146169
dependsOn configurations.runtimeClasspath
170+
dependsOn mergeLog4j2Plugins
171+
147172
from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } } {
148173
exclude "META-INF/*.SF"
149174
exclude "META-INF/*.DSA"
150175
exclude "META-INF/*.RSA"
151176
exclude "META-INF/*.EC"
152177
exclude "META-INF/INDEX.LIST"
153178
exclude "META-INF/versions/*/module-info.class"
179+
// Exclude the Log4J2 plugins caches as the merged version is included from mergeLog4J2Plugins
154180
exclude "META-INF/org/apache/logging/log4j/core/config/plugins/Log4j2Plugins.dat"
155181
exclude "module-info.class"
156182
}
157-
183+
from(mergeLog4j2Plugins)
158184

159185
finalizedBy 'cliInstallArchive'
160186
if (project.hasProperty(S3_BUCKET_URI_PROPERTY)) {
@@ -210,10 +236,3 @@ publishing {
210236
}
211237
}
212238
}
213-
214-
tasks.named("installDist") {
215-
dependsOn jar
216-
def homePath = System.properties['user.home']
217-
from jar
218-
into "$homePath/.corda/cli/"
219-
}

0 commit comments

Comments
 (0)