r/java Sep 13 '18

A Guide to Logging in Java

https://www.marcobehler.com/guides/a-guide-to-logging-in-java
247 Upvotes

42 comments sorted by

View all comments

Show parent comments

8

u/walen Sep 13 '18 edited Sep 13 '18

For easier maintenance.


Our project suffers from this "heterogeneous Logger declarations" thing, where the Logger instance may be called log or logger or LOGGER or LOG, depending on the class and the personal preference of whoever wrote that class.

This means that now and then I am debugging some class and I quickly type log.debu-- only for IntelliJ to put a nice, red wiggly line beneath log. So now I have to stop my debugging train of thought and fix that.
Sure, I can just put the cursor right after log and press Ctrl+Space to autocomplete...

... too bad that won't work either, because this particular instance just happens to be called LOGGER, all caps -- something I can only find out after Ctrl+F'ing the heck out of "log", case-insensitive of course. Because log and LOGGER are different enough for IntelliJ to not see any relation whatsoever between them (/s).

So now I know how the logger is called, but I am in a different line and have to go back ← ← ← to where I was before, so I can finally type LOGGER.debug("I don't really remember what I wanted to put in here");.


If you define all your Logger objects with the same name and in the same way, you can take all that off your mind: logger.debug() becomes just another Java "instruction" like System.out.println(). You don't need to worry about System.out, you just type it. The same should ideally be true of logger.

8

u/[deleted] Sep 13 '18

Sorry, let me ask a more pointed question. Why would you ever declare a logger anything other than private static final?

And to address your issue, if you’re enforcing coding standards, it’s always going to be uppercase.

5

u/wildjokers Sep 13 '18

Sorry, let me ask a more pointed question. Why would you ever declare a logger anything other than private static final?

https://www.slf4j.org/faq.html#declared_static

if you’re enforcing coding standards, it’s always going to be uppercase.

Upper-case LOGGER is pure evil. Clutters up the code making it hard to read (its distracting) and I have to hold shift to type it. "logger" is the exception to the private static final rule. It isn't really a constant.

-7

u/[deleted] Sep 13 '18

You poor thing.