Abhay Agarwal wrote:For example - if I am using Log4J implementation in web application,
First approach - I can use logger.debug, logger.warn etc statements in all classes. these logger statements are directly calling LOg4J framework to log messages in log file. So, log statements are kind of scattered across whole code.
Second approach - I can create a centralized Logger class [ and define debug, warn, info etc methods in it] and use this centralized Logger instance across whole code to log debug, warn, info etc statements. This centralized Logger class internally uses Log4J methods to log messages in log file.
Which approach is better ?Is it advisable or better pattern to use centralized logging code rather than using scattered log code ?
Unless your new Logger class also accepts the name of the class that is doing the logging then this is actually worse as all your log calls will look like they are coming from your Logger class, and not the actual class doing the logging.
So this:
becomes this, if you want to know where the logging occurred:
Not only do you still have the logging statement (as Jesper points out), but it's now longer, and you now need some way of getting that name into the log, probably by holding a map of class name to loggers...
Put simply, Log4J is already centralised logging .