• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why do we need mulitple loggers? - Log4j

 
Viji San
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carol Enderlin
drifter
Ranch Hand
Posts: 1364
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65217
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you end up with 1300 classes do you really want all of them logging at the same time?
 
Viji San
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65217
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't understand my response I guess.
 
Viji San
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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) ?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65217
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic