wood burning stoves 2.0*
The moose likes Other Open Source Projects and the fly likes Why do we need mulitple loggers? - Log4j Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Products » Other Open Source Projects
Bookmark "Why do we need mulitple loggers? - Log4j" Watch "Why do we need mulitple loggers? - Log4j" New topic
Author

Why do we need mulitple loggers? - Log4j

Viji San
Ranch Hand

Joined: Aug 12, 2010
Posts: 35
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

Joined: Oct 10, 2000
Posts: 1364
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

Joined: Jan 10, 2002
Posts: 60822
    
  65

When you end up with 1300 classes do you really want all of them logging at the same time?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Viji San
Ranch Hand

Joined: Aug 12, 2010
Posts: 35
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

Joined: Jan 10, 2002
Posts: 60822
    
  65

You didn't understand my response I guess.
Viji San
Ranch Hand

Joined: Aug 12, 2010
Posts: 35
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

Joined: Jan 10, 2002
Posts: 60822
    
  65

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Why do we need mulitple loggers? - Log4j
 
Similar Threads
log4j category
JBoss & Log4j
Log4j crap instructions
junit asserts for log messages
Logging - obvious?