Pregunta

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?

¿Fue útil?

Solución

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"));

Otros consejos

  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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top