Skip to content

Consolidate SLF4J level dispatch into a new LogUtil - #3527

Merged
dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:logutil-level-dispatch
Jul 26, 2026
Merged

dbwiddis merged 1 commit into
oshi:masterfrom
dbwiddis:logutil-level-dispatch

Conversation

@dbwiddis

@dbwiddis dbwiddis commented Jul 26, 2026 •

Copy link
Copy Markdown
Member

PerfDataUtil.handleError and ExceptionUtil.logAtLevel each carried their own five-way LogLevel switch. This hoists the dispatch into a new oshi.util.LogUtil and has both delegate to it.

LogUtil

Two methods, the level-parameterized halves of classic SLF4J logging:

ExceptionUtil.logAtLevel keeps its exact signature and becomes a one-line delegate, so oshi-core-ffm and any downstream caller are unaffected.

PerfDataUtil.handleError

31 lines to 7. isEnabled lets it keep skipping FormatUtil.formatError when the level is off, without an inline switch to do the checking.

It was also double-formatting: FormatUtil.formatError returns the output of String.format, and the result was passed through String.format again with no arguments. Harmless, since the hex output contains no %, but pointless. PerfCounterQueryHandler had the same copy-paste and is fixed too — its isWarnEnabled guards are left alone, since both sites log at a level known at the call site and can check it directly.

forbidden-apis

Widened from org.slf4j.event.Level to org.slf4j.event.**. Checking the jars, slf4j-api 1.7.5 has no org/slf4j/event package at all, while 1.7.25 has six entries — the package arrives as a unit, so every type in it breaks the same hosts that motivated #3524 and #3525.

Coverage

The level switch now lives in oshi-common, where a cross-platform test covers all ten branches. Previously the only fully covered copy was in PerfDataUtil, reachable only on Windows CI. Local JaCoCo, before vs after:

oshi-common  ExceptionUtil  LINE 77/77  BRAN 5/5   ->  LINE 68/68  (no branches)
             LogUtil                    (new)      ->  LINE 17/17  BRAN 10/10

oshi-core    PerfDataUtil             54 line, 41 br  ->  43 line, 26 br
             PerfCounterQueryHandler  56 line, 26 br  ->  53 line, 22 br

The oshi-core classes are Windows-only and read 0-covered on a Mac either way; the point is the shrinking denominator.

LogUtilTest uses a java.lang.reflect.Proxy recorder rather than SLF4J's own SubstituteLogger, because SubstituteLoggingEvent.getLevel() returns the banned org.slf4j.event.Level. The proxy asserts the contract directly: which Logger method each level dispatches to, and that arguments arrive unformatted for deferred substitution.

No user-observable behavior change — the emitted message text is byte-identical.

Summary by CodeRabbit

  • Bug Fixes
    • Improved logging compatibility across supported SLF4J versions.
    • Ensured log messages preserve formatting arguments and exception details.
    • Improved handling of enabled and disabled log levels.
    • Maintained clear warnings for invalid performance counter values.

PerfDataUtil.handleError and ExceptionUtil.logAtLevel each carried their
own five-way LogLevel switch. Hoist the dispatch into oshi.util.LogUtil
and have both delegate. ExceptionUtil.logAtLevel keeps its signature, so
oshi-core-ffm and any downstream caller are unaffected.

LogUtil.isEnabled restores the capability of Logger#isEnabledForLevel
that #3432 had to drop for slf4j 1.x compatibility. That lets
handleError keep skipping FormatUtil.formatError when the level is off,
without reintroducing an inline switch to do it.

handleError also double-formatted an already-formatted string:
FormatUtil.formatError returns the output of String.format, and the
result was passed through String.format again with no arguments.
PerfCounterQueryHandler had the same copy-paste, fixed there as well;
its isWarnEnabled guards are left alone, since both log at a level known
at the call site and can check it directly.

Widen the forbidden-apis ban from org.slf4j.event.Level to the whole
org.slf4j.event package. slf4j-api 1.7.5 has no such package at all, so
every type in it breaks the same hosts that motivated #3524 and #3525.

The level switch now lives in oshi-common, where a cross-platform test
covers all ten branches. Previously the only fully covered copy was in
PerfDataUtil, reachable only on Windows CI.
@coderabbitai

coderabbitai Bot commented Jul 26, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: bf91f987-0f8a-4fa4-8a5e-57c2307d8673

📥 Commits

Reviewing files that changed from the base of the PR and between a99741d and bdf420c.

📒 Files selected for processing (9)
  • CHANGELOG.md
  • config/forbidden-apis.txt
  • oshi-common/src/main/java/oshi/util/ExceptionUtil.java
  • oshi-common/src/main/java/oshi/util/LogUtil.java
  • oshi-common/src/test/java/oshi/util/ExceptionUtilTest.java
  • oshi-common/src/test/java/oshi/util/LogUtilTest.java
  • oshi-core/src/main/java/oshi/util/platform/windows/PerfCounterQueryHandler.java
  • oshi-core/src/main/java/oshi/util/platform/windows/PerfDataUtil.java
  • oshi-core/src/test/java/oshi/util/platform/windows/PerfDataUtilTest.java
💤 Files with no reviewable changes (1)
  • oshi-core/src/test/java/oshi/util/platform/windows/PerfDataUtilTest.java
📜 Recent review details
⏰ Context from checks skipped due to timeout. (6)
  • GitHub Check: Codacy Static Code Analysis
  • GitHub Check: Test JDK 25, windows-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, ubuntu-latest (+ JNA==FFM)
  • GitHub Check: Test JDK 25, macos-latest (+ JNA==FFM)
  • GitHub Check: Lint (Spotless, Checkstyle, ForbiddenAPIs, Javadoc)
  • GitHub Check: Analyze (java)
🧰 Additional context used
🧠 Learnings (2)
📚 Learning: 2026-04-12T19:22:09.716Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3149
File: oshi-common/src/main/java/oshi/hardware/common/platform/mac/MacCentralProcessor.java:159-163
Timestamp: 2026-04-12T19:22:09.716Z
Learning: When reviewing OSHI hardware platform code that uses or implements `CentralProcessor`/`PhysicalProcessor.getEfficiency()`, remember the API’s counterintuitive semantics: a *higher* efficiency-class value indicates *greater performance* (lower efficiency). P-cores/“performance cores” should therefore have higher values (e.g., 1) than E-cores/“efficiency cores” (e.g., 0), as documented in the `CentralProcessor` Javadoc. Do not flag code as incorrect solely because ARM_P_CORES or Intel P-cores are assigned higher efficiency values.

Applied to files:

  • oshi-common/src/main/java/oshi/util/LogUtil.java
  • oshi-common/src/main/java/oshi/util/ExceptionUtil.java
📚 Learning: 2026-07-06T19:25:59.225Z
Learnt from: dbwiddis
Repo: oshi/oshi PR: 3432
File: oshi-common/src/main/java/oshi/util/ExceptionUtil.java:26-56
Timestamp: 2026-07-06T19:25:59.225Z
Learning: In this repo’s SLF4J 1.x-compatible logging code, do not flag cases where a logger call uses SLF4J 1.6+ implicit-cause handling—e.g., `log.error("...{}", value, t)` (or similar with a single `{}` placeholder) where `t` is passed as the final/trailing argument. SLF4J will attach `t` as the log event’s cause with the full stack trace; this is equivalent to SLF4J 2.x’s fluent `atLevel(...).setCause(t).log(...)` and is intentionally used to preserve SLF4J 1.x runtime compatibility.

Applied to files:

  • oshi-common/src/main/java/oshi/util/LogUtil.java
  • oshi-common/src/main/java/oshi/util/ExceptionUtil.java
  • oshi-core/src/main/java/oshi/util/platform/windows/PerfCounterQueryHandler.java
  • oshi-core/src/main/java/oshi/util/platform/windows/PerfDataUtil.java
🔇 Additional comments (8)
oshi-common/src/main/java/oshi/util/LogUtil.java (1)

11-85: LGTM!

oshi-common/src/test/java/oshi/util/LogUtilTest.java (1)

31-148: LGTM!

config/forbidden-apis.txt (1)

23-25: LGTM!

CHANGELOG.md (1)

6-6: LGTM!

oshi-common/src/main/java/oshi/util/ExceptionUtil.java (1)

26-38: LGTM!

oshi-common/src/test/java/oshi/util/ExceptionUtilTest.java (1)

8-8: LGTM!

Also applies to: 397-405

oshi-core/src/main/java/oshi/util/platform/windows/PerfDataUtil.java (1)

27-27: LGTM!

Also applies to: 160-165

oshi-core/src/main/java/oshi/util/platform/windows/PerfCounterQueryHandler.java (1)

137-140: LGTM!


📝 Walkthrough

Walkthrough

The PR adds oshi.util.LogUtil for SLF4J 1.x-compatible level checks and dispatch, migrates ExceptionUtil and Windows performance logging to it, updates compatibility rules, and adds focused tests.

Changes

Logging consolidation

Layer / File(s) Summary
Add LogUtil dispatch utilities
oshi-common/src/main/java/oshi/util/LogUtil.java, oshi-common/src/test/java/oshi/util/LogUtilTest.java, config/forbidden-apis.txt, CHANGELOG.md
Adds enabled-level checks and deferred-argument logging dispatch for every LogLevel, with proxy-based tests and updated SLF4J API restrictions.
Migrate shared logging callers
oshi-common/src/main/java/oshi/util/ExceptionUtil.java, oshi-common/src/test/java/oshi/util/ExceptionUtilTest.java, oshi-core/src/main/java/oshi/util/platform/windows/*, oshi-core/src/test/java/oshi/util/platform/windows/PerfDataUtilTest.java
Routes exception and performance-counter logging through LogUtil, preserves throwable and formatted error arguments, and removes obsolete level-iteration coverage.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant ExceptionUtil
  participant PerfDataUtil
  participant LogUtil
  participant SLF4JLogger
  ExceptionUtil->>LogUtil: logAtLevel(level, message, throwable message, throwable)
  PerfDataUtil->>LogUtil: isEnabled(level)
  LogUtil->>SLF4JLogger: call matching isXEnabled()
  PerfDataUtil->>LogUtil: logAtLevel(level, formatted error, arguments)
  LogUtil->>SLF4JLogger: call matching error/warn/info/trace/debug method
Loading

Possibly related PRs

  • oshi/oshi#3432: Refactors the same SLF4J level-dispatch helpers in ExceptionUtil and PerfDataUtil.
  • oshi/oshi#3525: Introduces the oshi.util.LogLevel abstraction used by the migrated dispatch paths.

Suggested reviewers: wolfs

Poem

I’m a rabbit with logs in my burrow tonight,
Routing each level to the method just right.
WARN hops forward, ERROR thumps the ground,
Deferred little arguments safely abound.
SLF4J paths now neatly align—
Squeak, test, and merge: the dispatch is fine!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly summarizes the main change: centralizing SLF4J level dispatch in a new LogUtil.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Jul 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.23810% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 78.46%. Comparing base (a99741d) to head (bdf420c).

Files with missing lines Patch % Lines
...util/platform/windows/PerfCounterQueryHandler.java 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #3527      +/-   ##
============================================
+ Coverage     78.43%   78.46%   +0.02%     
+ Complexity     3611     3606       -5     
============================================
  Files           697      698       +1     
  Lines         24108    24106       -2     
  Branches       3994     3989       -5     
============================================
+ Hits          18909    18914       +5     
+ Misses         2997     2995       -2     
+ Partials       2202     2197       -5     
Flag Coverage Δ
linux 35.09% <85.71%> (+0.02%) ⬆️
macos 36.04% <85.71%> (+0.01%) ⬆️
windows 43.32% <95.23%> (+0.06%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@dbwiddis
dbwiddis merged commit 09ba9f1 into oshi:master Jul 26, 2026
16 of 17 checks passed
@dbwiddis
dbwiddis deleted the logutil-level-dispatch branch July 26, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant