Skip to content

Commit 38165d2

Browse files
authored
Gradle: Reproducer for apache/logging-log4j2#3779 (#351)
2 parents 1e9ac0a + b00a4b5 commit 38165d2

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

log4j-samples-gradle-metadata/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
plugins {
1818
id("application")
19+
id "io.spring.dependency-management" version "1.1.7"
1920
}
2021

2122
def log4jVersion = providers.environmentVariable("LOG4J_VERSION").getOrElse("2.25.0")

settings.gradle

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,42 @@ pluginManagement {
2121
mavenCentral()
2222
}
2323
}
24+
25+
/*
26+
* Fail the build if any WARN/ERROR log events occur.
27+
* Some projects forbid *any* build-time logging at these levels;
28+
* this ensures Log4j upgrades don't get blocked by harmless noise.
29+
*
30+
* See: https://github.com/apache/logging-log4j2/issues/3779
31+
*/
32+
import org.gradle.internal.logging.events.LogEvent
33+
import org.gradle.internal.logging.LoggingManagerInternal
34+
def events = []
35+
def listener = { e ->
36+
if (e instanceof LogEvent && e.logLevel.ordinal() >= LogLevel.WARN.ordinal()) {
37+
events << e
38+
}
39+
}
40+
41+
gradle.settingsEvaluated {
42+
def loggingManager = gradle.services.get(LoggingManagerInternal)
43+
loggingManager.addOutputEventListener(listener)
44+
/*
45+
* Note: This relies on Gradle’s internal logging APIs.
46+
* It may break in Gradle 9.x or later, but that’s expected —
47+
* using internal hooks is unsupported until Gradle provides a stable alternative.
48+
*/
49+
gradle.buildFinished {
50+
loggingManager.removeOutputEventListener(listener)
51+
if (!events.isEmpty()) {
52+
println "\n* Build failed due to ${events.size()} WARN/ERROR log event(s):"
53+
events.each { println "[${it.logLevel}] ${it.message}" }
54+
throw new GradleException("Build aborted: disallowed WARN/ERROR log events detected.")
55+
}
56+
}
57+
}
58+
59+
2460
dependencyResolutionManagement {
2561
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
2662
repositories {

0 commit comments

Comments
 (0)