Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hierarchical logging

 
Miklos Szeles
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49789
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for us beginners. Moving.9
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic