Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of logger in Utility classes of java

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?

like image 812
Amit Mutreja Avatar asked Sep 13 '12 10:09

Amit Mutreja


2 Answers

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"));
like image 124
RuntimeException Avatar answered Sep 23 '22 05:09

RuntimeException


  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.
like image 32
Santosh Avatar answered Sep 24 '22 05:09

Santosh