A class that declares more than one logger splits its own log output across several loggers, so the logging configuration that applies to the class governs only part of what it logs.
A class is expected to log through a single logger. That logger defines one category for the whole class, and the logging configuration attaches a level and appenders to it, so the output of the class is filtered and routed as one unit.
Declaring a second logger field breaks that. The reader now has to work out which logger each call site uses before being able to tell where a
message goes, and the two loggers do not necessarily resolve to the same category. When they do not, the level configured for the class governs only
part of its output: raising it to DEBUG during an incident produces a partial trace, with no indication that anything is missing. When
they do resolve to the same category, the second field is pure duplication and the class ends up with two names for one thing.
More than one logger in a class is almost always accidental: a field left behind by a refactoring that split or merged classes, a declaration copied along with the code that used it, or two logging facades that ended up in the same file.
This rule raises an issue when a class declares more than one logger field, for SLF4J, Log4j, Log4j 2, Commons Logging and
java.util.logging.
This rule targets production code only. A test class often declares one logger per class under test, so it can capture the output of each of them. Those loggers belong to the classes being exercised, and keeping them does not split the logging of a production class.
A nested or inner class is a class of its own, so a logger it declares is not counted against the logger of the enclosing class. Only the fields declared directly in the same class body are counted together.
A non-static logger field declared next to a static one still raises an issue, because the class still ends up with two loggers, regardless of the modifiers. Rule {rule:java:S1312} covers the modifiers a logger field should have.
A logger whose name is derived from another logger of the same class, as in LoggerFactory.getLogger(LOG.getName() + ".config"), does
not count. That call creates a child category, which inherits the level and the appenders configured for the parent, so the output of the class is
still governed as one unit and the problem described above does not arise. Reusing the name as it is, as in
LoggerFactory.getLogger(LOG.getName()), builds the same category rather than a child one and is still reported.
Keep one logger and remove the other field, then point the calls that used the removed logger at the remaining one.
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
Marker instead: create the marker once with MarkerFactory.getMarker("AUDIT"), pass it as the first argument of the logging
call, and route it in the logging configuration with a marker filter. A marker keeps the distinction and the routing without giving the class a second
logger.
With Log4j 2, MarkerManager.getMarker plays the same role. With java.util.logging, which has no marker concept, keep the
single logger and either use a dedicated level or a Filter on the handler.
public class PaymentService {
private static final Logger LOG = LoggerFactory.getLogger(PaymentService.class);
private static final Logger LOGGER = LoggerFactory.getLogger(PaymentService.class); // Noncompliant
public void capture(Payment payment) {
LOG.debug("Capturing payment {}", payment.id());
gateway.capture(payment);
LOGGER.info("Payment {} captured for {}", payment.id(), payment.amount());
}
}
public class PaymentService {
private static final Logger LOG = LoggerFactory.getLogger(PaymentService.class);
public void capture(Payment payment) {
LOG.debug("Capturing payment {}", payment.id());
gateway.capture(payment);
LOG.info("Payment {} captured for {}", payment.id(), payment.amount());
}
}
The class now has a single logger, so everything it logs is filtered and routed by the configuration that targets its category, and a call site no longer says anything about where its message goes beyond the level it uses.
The refactoring preserves behavior as long as every call moved to the remaining logger keeps its level and its arguments. When the removed logger used a different category, check the logging configuration before deleting the field, otherwise messages that used to be written to a dedicated appender silently move to the main log. If that separation has to be kept, use a marker as described above rather than a second logger.
java.util.logging.LoggerThose two rules constrain the form and the name of a logger field. This rule is about their number: a class should declare only one.