1

PMD Rule LoggerIsNotStaticFinal:

In most cases, the Logger reference can be declared as static and final.

I seem to remember some arguments for using local variables for logging, but cannot recall it.

One example seems to be for an instance variable, but requires that it be a final, initialized in the constructor.

Another case I can think of (make up?) is when one might want to use different loggers for the same piece of code, perhaps dependent on some other variables.

Any other cases where a local variable of type logger may be more appropriate?

1
  • From my own experience, I never encountered a case where I needed a local logger.
    – Spotted
    Nov 3, 2015 at 8:29

1 Answer 1

3

I am not aware of any situation where a local logger makes sense over a static final logger using any of the built-in or industry-standard logging frameworks in Java.

  • Loggers are thread-safe, so threads can share loggers. Having "my own" logger does not have any benefit here.

  • Loggers are typically defined for one of the following categories:

    • For a fully-qualified class name. Having a local logger makes no sense, since two local loggers on different threads will resolve to the same object anyway.

    • For an arbitrary logger category. For example, perhaps all hardware events log to a dedicated hardware logger regardless of the code that responds to the event. As with the previous point, local copies of this logger will all resolve to the same object anyway.

  • If there are going to be multiple references to the same logger, it makes sense to have a single reference instead. There is a slight memory advantage that is not worth obsessing over, but the intent of this is the logger for this class is better represented by having it at the class level. Furthermore, retrieving the logger instance can be done once which might impact performance-critical code. It can also clutter code to have extraneous, unnecessary statements to retrieve a logger that can simply be retrieved at the top of the class.

Given how Java loggers work, there really is no advantage to a local logger.

What is more efficient from an execution and code clutter standpoint?

Static:

public class MyClass {
  private static final Logger LOG = Logger.getLogger(MyClass.class);

  public void doSomething1() {
    LOG.info("Yadda, yadda, yadda");
  }
}

Local:

public class MyClass {
  public void doSomething1() {
    Logger LOG = Logger.getLogger(MyClass.class);
    LOG.info("Yadda, yadda, yadda");
  }

  // OR...

  public void doSomething2() {
    Logger.getLogger(MyClass.class).info("Yadda, yadda, yadda");
  }
}

The local statements are more verbose. Logging this way takes more space, which distracts the reader's eye from the code that actually does stuff.

There is no reason for acquiring local loggers.

3
  • Thanks. I am well aware of the advantages of private static final loggers that you describe. I am more interested in whether any use-cases exist for the opposite case, which if I read your reply right you are saying you are not aware of. Perhaps you are also contending that such a use-case does not exist, but I do not find your arguments to be compelling or a formal proof. Nov 3, 2015 at 6:14
  • @MiserableVariable This answer is not a rigorous proof: my points are that any use case or concern that might be relevant would lead me to believe that the opposite (static final) is a better option. The lack of compelling reasons for a local logger is my point.
    – user22815
    Nov 3, 2015 at 14:48
  • I agree with you largely, which is why you have my vote. Nov 3, 2015 at 21:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.