Repository navigation
SONARJAVA-7163 Implement rule S9436 - Classes should not declare more than one logger - #6336
Conversation
742aa80 to
77bae00
Compare
|
/pvf |
77bae00 to
0dc44c3
Compare
This comment has been minimized.
This comment has been minimized.
|
📊 Performance Validation complete |
|
⚖️ Ruling update ready for review: #6338 |
0dc44c3 to
0a1db24
Compare
0a1db24 to
d613bbe
Compare
d613bbe to
06b1454
Compare
|
Both fixed. Derived exemption now needs a getName() concatenation, and final uninitialized loggers count. Tests added. |
Code Review ✅ Approved 4 closed / 4 findings🟡 Medium risk · Adding S9436 changes analyzer findings for Java classes with multiple logger fields; imperfect detection could misreport issues or miss logger configurations. Adds rule S9436 to flag classes that declare more than one logger field, with a secondary location on the first logger. Supported facades are SLF4J, Log4j 2, Log4j 1.x, ✅ 4 closed✅ Quality: Rule description leaves Commons Logging out of the supported facades
✅ Quality: Multi-declarator test case leaves the secondary location unchecked
✅ Edge Case: Derived-logger exemption skips any initializer that mentions a logger
✅ Edge Case: Uninitialized-field exemption also skips final loggers set in a block
Verification Repos 4 inspectedFunctional validation No results Rules No rules evaluated Cross-repo 4 repositories inspected 0 open impacts
Auto-approval Not enabledOptionsAuto-apply is off → Gitar will not commit updates to this branch. Comment with these commands to change the behavior for this request:
Was this helpful? React with 👍 / 👎 | Gitar |
|
romainbrenguier
left a comment
There was a problem hiding this comment.
Maybe one test-case to check, otherwise looks good. We can check with mega-FPS on peach if it makes sense.
| class DerivedChildLogger { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(DerivedChildLogger.class); // Compliant | ||
| private static final Logger LOG_CONFIG = LoggerFactory.getLogger(LOG.getName() + ".config"); // Compliant, a child category inherits the parent configuration |
There was a problem hiding this comment.
What about LoggerFactory.getLogger(DerivedChildLogger.class.getName() + ".config")?
| <p>Keep one logger and remove the other field, then point the calls that used the removed logger at the remaining one.</p> | ||
| <p>The second logger is sometimes deliberate: a separate stream is wanted for audit or metrics messages, so that they can be routed to their own file | ||
| and kept at their own level. In that case, do not simply merge the calls, as that would lose the distinction. Tag the messages with an SLF4J | ||
| <code>Marker</code> instead: create the marker once with <code>MarkerFactory.getMarker("AUDIT")</code>, pass it as the first argument of the logging |
There was a problem hiding this comment.
It would be nice to have a compliant/non-compliant example with the Marker case. (to be done on the RSPEC PR)




Implements rule S9436, which raises when a class declares more than one logger field.
A second logger field means part of the class logs under a category nobody configured, so those messages are filtered out or routed to the wrong appender, and the level set for the class governs only half of its output.
The check collects the logger-typed fields declared directly in a class body and reports every one after the first, with a secondary location on the first. Supported facades: SLF4J, Log4j 2, Log4j 1.x,
java.util.loggingand Commons Logging.Nested, inner and anonymous classes each count separately, local variables are ignored, and the rule is disabled without a semantic model.
RSPEC: https://github.com/SonarSource/rspec/pull/8338
Jira: https://sonarsource.atlassian.net/browse/SONARJAVA-7163