[Logo]
Forums Register Login
Why do we need mulitple loggers? - Log4j
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.
Take a look at the Logger Hierarchy section of the Short introduction to log4j

You gain flexibility by using some sort of hierarchical naming convention.
When you end up with 1300 classes do you really want all of them logging at the same time?
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?
You didn't understand my response I guess.
Sorry, I missed your message.

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) ?
Performance is not an issue, it's the absolute deluge of logging that you will get. You will not be able to control which loggers are turned on, and at what level is they all use the same logger.

The hierarchical nature of the loggers is its best feature and you;d be remiss not to take advantage of it.
Wink, wink, nudge, nudge, say no more ... https://richsoil.com/cards



All times above are in ranch (not your local) time.
The current ranch time is
Nov 19, 2017 13:13:20.