Hi,
What is probably foiling you is appender additivity.
Loggers are organized in a tree, the levels of the tree are separated by
dots in the logger name. The method you're using to get a logger,
Logger.getLogger(this.getClass()), is actually just a convenience. The
basic way of getting a logger is by name, e.g. getLogger("MyLogger") or
getLogger("myTree.branch1.MyFirstLogger"). Since using loggers with the
same name as the class from which your logging is a very common way of
organizing one's loggers (but not the only conceivable one) the API also
provides getLogger(Class) as a method, so you can simply call
Logger.getLogger(this.getClass()) instead of having to call
Logger.getLogger(this.getClass().getName()).
At the bottom of the logger tree is the root logger, which is what you
obtain by calling Logger.getRootLogger(). I hope it is now clear what
the difference is between these two function calls.
If you log to a certain logger, you enter a log message into the tree at
the point of the logger you're logging to. The event will flow down the
tree all the way to the root, and will be picked up by appenders that
are attached to the tree along the way, to write the event to a log
file, send it to a remote network location, or whatever else the
particular appender you're using does. This flowing of the event down
the tree is called "appender additivity" (and it can be turned of for a
specific logger, which stops events arriving at that logger from flowing
further down the tree).
What probably happened to you is that you attached appenders at two
different points in the tree below (or at) the logger you're logging to.
For example, if you are logging to logger
com.yourdomain.yourpackage.MyClass, and you attached an appender to both
that logger and also to the root logger, you will get your log message
twice; once through the appender attached to the logger associated to
your class, and once through the appender attached to the root logger.
If you happended to attach the *same* appender at both levels, you will
get an entry in your log file (if it's a file appender) twice, because
the event will arrive at the appender twice (once through your own
logger, once through the root logger).
The solution in that particular case would be to remove the appender
from the higher tree level and leave it only attached at the root
logger; you may think you need an appender attached to every logger you
log to. You don't, because of this concept of appender additivity.
I recommend you read the short introduction to log4j at
http://logging.apache.org/log4j/docs/manual.html
It explains all this and more and is quite essential knowledge to
understand how to configure your log4j setup.
Best regards,
Eelke Blok
-----Original Message-----
From: Mansour [mailto:[EMAIL PROTECTED]
Sent: dinsdag 12 september 2006 9:02
To: Log4J Users List
Subject: Logger.getRootLogger() or Logger.getLogger(this.getClass())
Hi there:
I am still new to logging with java. In my project I am using log4j in
each class to trace the excution. However, at some points I'n getting
the logs twice.
I 'll explain what I did. I am using somehting like this in all the
classes :
Logger logger=Logger.getLogger(this.getClass());
BasicConfigurator.configure();
logger.setLevel(Level.ALL);
//some code ......
logger.info("myMessage");
//more code ....
when, a method is executed, it logs the message at least 2 times !!!
what's is going on?? by the way, I dont understand the herirachy in the
log4j. I mean what's the difference between Logger.getRootLogger() and
Logger.getLogger(this.getClass()) ??
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
PRIVACY: The information transmitted is intended only for the person or
entity to which it is addressed and may contain confidential and/or
priviliged material. Any review, retransmission, dissemination or other
use of this information by persons or entities other than the intended
recipient is prohibited. If you received this in error, please contact
the sender and delete the material from any computer. Thank you.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]