• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why do we need mulitple loggers? - Log4j

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
drifter
Posts: 1364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic