File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Hierarchical logging Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Hierarchical logging" Watch "Hierarchical logging" New topic
Author

Hierarchical logging

Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
I've read some articel about Java logging but I haven't found exact answer for a few questions. What is the suggested logger hierarchy? Should I use different logger for every logging class or for every package or ...?
My other question is about logger naming. I saw an article which used MyClass.class.getName() to name the logger. Is this a good approach. What about obfuscating?
Thanks in advance.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

How granular your logging is is up to you. I always have a logger-per-class, but that may not always be necessary.

I don't know what logger you're using, but I always use TheClass.class to name them.
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
I'm also thinking about having a logger-per-class. Won't it cause too much overhead with hundreds of logging classes?
I'm using Java logger. I've asked about the naming because I think obfuscating the code will prevent file based logger configuration whenever I use class names since the class names are obfuscated.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Too difficult a question for us beginners. Moving.9
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Miklos Szeles wrote:I'm also thinking about having a logger-per-class. Won't it cause too much overhead with hundreds of logging classes?
I'm using Java logger. I've asked about the naming because I think obfuscating the code will prevent file based logger configuration whenever I use class names since the class names are obfuscated.

Hundreds of logging classes? I'd guess a non-trivial Java app would have hundreds of thousands of objects. I'm certainly not going to worry about a few hundred loggers. In any case, in general loggers are defined as static, so there's only one for all instances of a class anyway.

If you use FooBar.class the obfuscator had better darn well know how to handle that--if it can't, you need a *way* better obfuscator.
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
First of all, thank you for your answers. I know that it is adviced to use one static logger for a class but the question still remains. What about when a few classes becomes hundreds of classes which becomes thousands off classes and so on? Won't they cause too much overhead?
As far as I know in java.util.logging.Logger has only two getLogger method. One of them requires a name and the other requires a name and a resourceBundleName. I saw that log4j has a getLogger method which requires a Class as parameter.
I understand that an obfuscator will handle the class name obfuscation correctly, but I was talking about configuration files. In a configuration file I have to use the name of the logger to set it's properties so an obfuscation will prevent the from file logging configuration(please correct me if I misunderstood something).
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

No, if you are naming specific classes in your logging configuration then you are doing it wrong. (Especially if, as you say, you expect to have thousands of classes writing logs!)

So review the tutorials and see how it should really be done.
Miklos Szeles
Ranch Hand

Joined: Oct 21, 2008
Posts: 142
I'm just starting to use Java logger so unfortunatelly I have no experience with it. But I would like to use it in the right way, this is why I ask my questions. In the LogManager documentaion I found:
The properties for loggers and Handlers will have names starting with the dot-separated name for the handler or logger.
So in my understanding it means that if I would like to set the log level for everything under my package called p1.p2 to INFO I have to write p1.p2.level = INFO in the configuration file. But in this line I have to use the name of the logger. But it won't work if I used p1.p2.MyClass.class.getName() as the logger name since the obfuscator made o1.o2.ObfuscatedClass from my class name.
Tanzy Akhtar
Ranch Hand

Joined: Jul 19, 2009
Posts: 110

Miklos wrote--
if I would like to set the log level for everything under my package called p1.p2 to INFO I have to write p1.p2.level = INFO in the configuration file.


No just use the following--



Thanks,
Tanzy.


Roll with punchers, there is always tomorrow.
Techie Blog -- http://jtanzy.blogspot.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Hierarchical logging
 
Similar Threads
Passed with 384/400 (B&S 2.3.2)
re: help appreciated
Good logging system for WebApp (Struts)
Question about Logging
Using Reflection to dynamically load logging classes