Repository navigation
Add errorprone-slf4j checks and fix the SLF4J misuse they found - #3610
Conversation
Adds the errorprone-slf4j plugin (jp.skypencil.errorprone.slf4j) to the
error-prone profile's annotationProcessorPath. It registers itself via
META-INF/services, so no -Xplugin change is needed; its checks are then
configured with -Xep like any core check. It declares error_prone_check_api
compileOnly, so it adds no transitive dependency, and is verified working
against error-prone 2.50.0 despite being built against an older API.
Two checks are OFF as findings the project rejects rather than a backlog:
Slf4jLoggerShouldBeNonStatic fires on all ~200 `private static final Logger
LOG` fields, whose staticness is deliberate, and Slf4jSignOnlyFormat only
flagged the horizontal-rule separator lines the SystemInfoTest mains print.
Slf4jDoNotLogMessageOfExceptionExplicitly is demoted to WARN: its 74-site
backlog is one the project intends to drain, but the fixes change log output
on every platform and belong in their own change. WARN keeps it visible
without blocking; it escalates to ERROR once drained. The remaining checks are
ERROR, three of them (LoggerShouldBePrivate, LoggerShouldBeFinal,
IllegalPassedClass) as pure regression guards since their backlog was zero.
Fixes for the ERROR-severity findings:
Slf4jPlaceholderMismatch (13 sites, one constant): ParseUtil's DEFAULT_LOG_MSG
had a trailing "{}" for the exception, but SLF4J unconditionally trims a
trailing Throwable out of the substitution arguments, so it was attached as the
event cause (with stack trace) while the second placeholder was emitted
literally. Verified against slf4j-api 1.7.36 and 2.0.18. Dropping the
placeholder leaves throwable handling unchanged and removes the stray "{}".
Slf4jFormatShouldBeConst (12 sites):
- MacInstalledApps concatenated e.getMessage() into the message *and* passed
the exception, duplicating the text alongside the stack trace.
- SmcUtil wrapped the whole message in String.format for one hex field; the
constant is now the format string and only the hex is formatted.
- WmiQueryHandler and its FFM twin built the format in a local. Hoisted to a
constant and expanded the Object[] into varargs, because with the format
resolvable Slf4jPlaceholderMismatch cannot count elements of an Object[]
variable and the array form just trades one finding for another. Making the
arguments explicit also exposed four DoNotLogMessageOfExceptionExplicitly
sites the check could not see through the array - including the JNA twin's,
which had been invisible while the FFM twin's were reported.
- LogUtil.logAtLevel forwards a caller-supplied message by design, so it is
suppressed rather than fixed.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (7)
📜 Recent review details⏰ Context from checks skipped due to timeout. (12)
🧰 Additional context used📓 Path-based instructions (4)**/*.{java,properties}📄 CodeRabbit inference engine (AGENTS.md)
Files:
oshi-common/**/*.java📄 CodeRabbit inference engine (AGENTS.md)
Files:
**/*.java📄 CodeRabbit inference engine (AGENTS.md)
Files:
oshi-core-ffm/**/*.java📄 CodeRabbit inference engine (AGENTS.md)
Files:
🧠 Learnings (5)📚 Learning: 2026-04-12T19:22:09.716ZApplied to files:
📚 Learning: 2026-07-06T19:25:59.225ZApplied to files:
📚 Learning: 2026-07-30T00:35:22.638ZApplied to files:
📚 Learning: 2026-08-03T00:46:14.953ZApplied to files:
📚 Learning: 2026-08-02T23:56:23.866ZApplied to files:
🔇 Additional comments (7)
📝 WalkthroughWalkthroughThe change standardizes SLF4J logging across macOS and Windows code. It adds shared message formats, preserves existing log levels, corrects exception argument handling, and enables Error Prone SLF4J checks in Maven. ChangesSLF4J logging standardization
Estimated code review effort: 2 (Simple) | ~15 minutes Merge Risk: ⚪ Minimal · up to This change adds build-time SLF4J checks and corrects the reported logging issues without changing shipped APIs or runtime behavior; no actionable merge-blocking risk remains after normal checks and review. Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #3610 +/- ##
============================================
- Coverage 79.87% 79.80% -0.08%
- Complexity 3870 3993 +123
============================================
Files 696 696
Lines 24220 24220
Branches 4057 4057
============================================
- Hits 19345 19328 -17
- Misses 2712 2725 +13
- Partials 2163 2167 +4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
…RROR (#3611) The errorprone-slf4j check Slf4jDoNotLogMessageOfExceptionExplicitly was adopted at WARN in #3610 with a 74-site backlog left to drain. This drains it and moves the check to the plugin's default ERROR severity. Every one of the 74 log statements substituted the exception's message into the log text instead of passing the exception itself. Passing the throwable is better on three counts: the stack trace and cause chain reach the logs rather than a single message line; getMessage() returns null for exceptions constructed without one, which logged the literal text "null"; and SLF4J defers all rendering when the level is disabled, whereas getMessage() is evaluated at the call site regardless. Where the message ended in a placeholder that only existed to hold the exception text, the placeholder is dropped along with any separator left dangling in front of it. Three shared format constants (COM_EXCEPTION_MSG in oshi-common, shared by both Sensors twins, and COM_EXCEPTION_FMT in each WmiQueryHandler twin) lost their trailing placeholder instead, covering eleven call sites between them. AbstractNetworkIF built its InstantiationException from the SocketException's message and discarded the exception, so the stack trace getNetworks() now logs would have stopped there. It attaches the cause via initCause, since InstantiationException has no (String, Throwable) constructor. ExceptionUtil.logAtLevel appends both t.getMessage() and t to an Object[] before dispatching, which these checks cannot see through, so its callers are untouched here and left for a follow-up.
Adds the errorprone-slf4j plugin (
jp.skypencil.errorprone.slf4j, Apache-2.0) to theerror-proneprofile, and fixes everything it found except one deferred category.It registers itself through
META-INF/services, so it only needs to be on theannotationProcessorPath; its checks are then configured with-Xeplike any core check. It declareserror_prone_check_apiascompileOnly, so it contributes no transitive dependency, and it is build-time only — nothing ships.Check configuration
Slf4jPlaceholderMismatchSlf4jFormatShouldBeConstSlf4jLoggerShouldBePrivateSlf4jLoggerShouldBeFinalSlf4jIllegalPassedClassSlf4jDoNotLogMessageOfExceptionExplicitlySlf4jLoggerShouldBeNonStaticprivate static final Logger LOGfieldsSlf4jSignOnlyFormatSystemInfoTestmains printThe three escalated-to-ERROR guards follow the same per-check pattern as #3606. Note the severity token Error Prone accepts is
WARN, notWARNING.Slf4jPlaceholderMismatch— 13 sites, one constantParseUtil.DEFAULT_LOG_MSGwas\"{} didn't parse. Returning default. {}\", with the exception passed as the second argument. SLF4J trims a trailingThrowableout of the substitution arguments unconditionally, regardless of placeholder count, so the exception was attached as the event's cause (stack trace and all) while the second placeholder was emitted literally:Verified directly against
slf4j-api1.7.36 and 2.0.18. Dropping the placeholder leaves throwable handling exactly as it was and removes the stray{}from all 13 call sites, which are otherwise untouched.Slf4jFormatShouldBeConst— 12 sitesMacInstalledApps(×2) concatenatede.getMessage()into the message and passed the exception, duplicating the text alongside the stack trace.SmcUtilwrapped the entire message inString.formatfor a single%08xfield. The constant is now the format string and only the hex value is formatted.WmiQueryHandlerand its FFM twin built the format in a local. Hoisted to a constant and expanded theObject[]into varargs — with the format resolvable,Slf4jPlaceholderMismatchcannot count elements of anObject[]variable, so keeping the array just trades one finding for another. Making the arguments explicit also exposed fourDoNotLogMessageOfExceptionExplicitlysites the check could not see through the array, including the JNA twin's, which had been invisible while the FFM twin's were reported — hence 74 deferred sites rather than 70.LogUtil.logAtLevelforwards a caller-supplied message by design, so it carries@SuppressWarningsrather than a fix.Deferred to a follow-up
Slf4jDoNotLogMessageOfExceptionExplicitly, 74 sites across 44 files, sits at WARN so the backlog stays visible without blocking. Passing the throwable instead ofe.getMessage()keeps the stack trace and cause chain, is null-safe (a barenew IOException()currently logs the literalnull), and is cheaper when the level is disabled, sincegetMessage()is evaluated at the call site regardless. But it changes log output on every platform and the JNA/FFM twins have to move together, so it belongs in its own change. That check escalates to ERROR once drained.Verification
./mvnw clean spotless:apply sortpom:sort checkstyle:check install forbiddenapis:check javadoc:javadoc— clean, 1559 tests successful, 0 failed (macOS/aarch64/JDK 26)../mvnw clean install -DskipTests -Perror-prone— green, with only the deferred check's WARNING-severity findings and no other Error Prone output.No
CHANGELOG.mdentry: this is build tooling plus log-message hygiene, with no API or behavior change.Summary by CodeRabbit
Bug Fixes
Chores