Question

i am just wondering 'bout use of Logger API in Utility classes while writing java code. we can always use logger where we will be using our utility class to log a proper message BUT what i want to ask is--

Is it a good practice to use Logger in utility classes?

Was it helpful?

Solution

I assume you are writing your own *Util code.

Personally I avoid using Logger in utility classes, because of the "noise" in the log files. If your utility class is well tested, you may remove the log statements.

Just remember to only log, and not perform any business logic in the log statement (whether utility class or not).

E.g. I saw below in one of my projects, and this is not good practice.

log.info("Added = " + list.add("someString"));

OTHER TIPS

  1. The whole objective of logging in the code is that you should be able debug or look for any abnormal application behavior just by looking into the logs.
  2. So if the utility class you referred hosts an important logic/algorithm which you think logger here will help you debug any issue in future then you should log it.
  3. But if your util class contains concise utility functions where hardly any complex logic is involved then logging should be avoided there.

It is a great Idea to use java.util.logging.Logger in all utility classes. The most Java logging frameworks are able to collect Logs from this logger and you won't need to bundle any libraries, because it comes along with the jre.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top