While implementing logging (log4j) in an application, we instantiate the logger in each class as like below
In RequestHelper.java, private static Logger log = Logger.getLogger(RequestHelper.class);
In ProcessRequest.java, private static Logger log = Logger.getLogger(ProcessRequest.class);
In ProcessResponse.java, private static Logger log = Logger.getLogger(ProcessResponse.class);
In RequestManager.java, private static Logger log = Logger.getLogger(RequestManager.class);
In log4j, all these loggers are arranged in a hierarchy, RootLogger will always be root of the hierarchy, also, it is managed by log4j Repository/Manger classes. The newly created loggers will inherit log Level, Appender information from RootLogger if nothing is configured in specific to the logger. If that's the case, I could retrieve RootLogger in all my classes to log the message instead of instantiating new logger (typically, passing class name as an argument) in each class.. right?. Can anyone please advise.
Agree. There are flexibilities. If I need to have the same Log LEVEL, Appender which I have it for RootLogger , can I ignore creating Logger in each Class and just invoke RootLogger wherever Logger is required? Would there be drawbacks by going with this approach?
Do you mean, when ONLY RootLogger is used and all classes (1300) try to get RootLogger for logging at the same time, there will be a huge performance hit due to Java Object lock on RootLogger for each class request in multi threading model (typically, web app) ?