Mansour wrote:
> public class MainClass{
> Logger mainLogger=Logger.getLogger(this.getClass());
> BasicConfigurator.configure();
> mainLogger.setLevel(Level.ALL);
> //.....
> //more code
> new AnotherClass();
> }

> class AnotherClass{
> //code
> Logger subLogger=Logger.getLogger(this.getClass());
> BasicConfigurator.configure();
> sub.setLevel(Level.ALL);
> //more code
> }

> in this situation,  subLogger is a child of mainLogger. Am I right?? 

First, you really have to stop calling BasicConfigurator.configure() in
multiple places in your code. Call it once, at application startup. I am
90% certain this is causing your multiple log messages that you asked
about in your first message.

Then, on to your above question. I'm afraid you're not right. The logger
name determines the logger's place in the logger tree, and nothing else
(not the sequence in which getLogger calls are made, nor the place where
they are called in your code - unless you're using the 'this' variable,
of course, because its value *does* depend on the place where you're
using it).  

Before I go on, I again want to emphasize that the fact you can call
Logger.getLogger(this.getClass()) is just a convenience. I haven't
looked at the code, but AFAIK, (and for all we simple API callers need
to know) the implementation of this method is simply something like
this:

        static Logger getLogger(Class class) {
                return getLogger(class.getName());
        }

You get loggers by their name, they really have nothing to do with the
classes that use them unless you happen to choose to create a logger
with the same name as your class (which is exactly what you're doing
when you call Logger.getLogger(this.getClass()).

Let's say both these classes in the example are in package
com.yourdomain.yourapplication, i.e. their full names would be:
- com.yourdomain.yourapplication.MyMain
- com.yourdomain.yourapplication.AnotherClass

Calling Logger.getLogger(this.getClass()) inside them creates loggers
with the full names of the classes. 
So: 
Your variable mainLogger will hold a reference to the logger named
"com.yourdomain.yourapplication.MyMain"
Your variable subLogger will hold a refence to the logger named
"com.yourdomain.yourapplication.AnotherClass"

Now, forget all about class hierarchies, all you know now are logger
hierarchies ;)
The place in the logger tree is determined by the name, levels are
separated by dots. This means both your loggers are children to the
com.yourdomain.yourapplication logger, and are sibblings to one another.


I hope this clears it up for you. Remember, it's the logger's name that
determines its place in the logger hierarchy, and absolutely nothing
else.

Best regards,

Eelke Blok


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to