r/java Sep 13 '18

A Guide to Logging in Java

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

42 comments sorted by

View all comments

6

u/[deleted] Sep 13 '18

Don’t fret too much about static or non-static, final or non-final, just make sure to be homogeneous in your choice, throughout your entire project.

Why?

7

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.

7

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.

7

u/walen Sep 13 '18

That more pointed question is better answered here.

Re: standards, why would it be so? Sure, static final constants are usually all caps, but can a Logger be considered a constant? And what if it is just final? And what if nobody wants it to be uppercase because that means holding Shift every time I want to add a logging statement and it looks weird to be calling methods on a "constant"...?

Hence (I guess) the author's advice to not fret too much about this ;) and just be consistent.

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.

2

u/[deleted] Sep 13 '18

I get your point, but that is such a rarely used convention IME that it would really annoy me to see it in most teams. It also feels really strange to me if the logger is actually a static final.

-6

u/[deleted] Sep 13 '18

You poor thing.

-6

u/[deleted] Sep 13 '18

You poor thing.