Skip to content

Commit b00a4b5

Browse files
committed
fix: fail build on WARN/ERROR log events via internal logging hook
Use Gradle 8.x’s internal logging API to abort the build if any log event of severity WARN or higher is emitted. This helps prevent harmless warnings from blocking Log4j adoption in strict environments. ⚠️ This approach relies on unsupported APIs and may break with Gradle 9.x, but it is currently the most effective option.
1 parent d40f62d commit b00a4b5

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

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)