You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Ionel GARDAIS <ig...@yahoo.fr> on 2004/01/19 20:54:36 UTC

Using logger declared in an abstract superclass

Hi,

I have the following problem : say I have two classes
:

abstract class A { Logger log =
Logger.getLogger(this.getClass(); }

class B extends A { log.debug("foo"); }

class C extends A { log.debug("bar"); }

I declared the RootLogger as a file logger to a file
named "main.log"

When I look at the main.log file, I have the log from
both B and C, with both information about the class
which made the log.

Now, if I declare another logger, especialy for the B
class, I can see B's log messages in both the root
logger and its dedicated logger.

C's log appears only in the main logger (as expected)


Problem is : for the B class, why does the root logger
gets the message too ?

Is it a side-effect because I declare the logger in an
abstract superclass and I declared the custom logger
upon the B class ?

Is there a way to tell "if I have a dedicated logger
for a class, use it but do not log to the root logger
?"


thanks,
ionel

_________________________________________________________________
Do You Yahoo!? -- Une adresse @yahoo.fr gratuite et en français !
Yahoo! Mail : http://fr.mail.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Using logger declared in an abstract superclass

Posted by Christian Hufgard <ch...@gmx.de>.
Hi Ionel,

this is not a fault, this is the default and wanted behaviour of
log4j. A short extract from the (short) manual:

>The addAppender method adds an appender to a given logger. Each
>enabled logging request for a given logger will be forwarded to all
>the appenders in that logger as well as the appenders higher in the
>hierarchy. In other words, appenders are inherited additively from the
>logger hierarchy. For example, if a console appender is added to the
>root logger, then all enabled logging requests will at least print on
>the console. If in addition a file appender is added to a logger, say
>C, then enabled logging requests for C and C's children will print on
>a file and on the console. It is possible to override this default
>behavior so that appender accumulation is no longer additive by
>setting the additivity flag to false.

I had exactly the same problems last week, but after re-reading the
manual I found the above passage *g*
So if you want class B only to log to the dedicated logger you have to
add a line like this:
log4j.appender.classB.additivity = false


Cheers,

Christian





Monday, January 19, 2004, 8:54:36 PM,:

IG> Hi,

IG> I have the following problem : say I have two classes
IG> :

IG> abstract class A { Logger log =
IG> Logger.getLogger(this.getClass(); }

IG> class B extends A { log.debug("foo"); }

IG> class C extends A { log.debug("bar"); }

IG> I declared the RootLogger as a file logger to a file
IG> named "main.log"

IG> When I look at the main.log file, I have the log from
IG> both B and C, with both information about the class
IG> which made the log.

IG> Now, if I declare another logger, especialy for the B
IG> class, I can see B's log messages in both the root
IG> logger and its dedicated logger.

IG> C's log appears only in the main logger (as expected)


IG> Problem is : for the B class, why does the root logger
IG> gets the message too ?

IG> Is it a side-effect because I declare the logger in an
IG> abstract superclass and I declared the custom logger
IG> upon the B class ?

IG> Is there a way to tell "if I have a dedicated logger
IG> for a class, use it but do not log to the root logger
IG> ?"


IG> thanks,
IG> ionel

IG> _________________________________________________________________
IG> Do You Yahoo!? -- Une adresse @yahoo.fr gratuite et en français !
IG> Yahoo! Mail : http://fr.mail.yahoo.com

IG> ---------------------------------------------------------------------
IG> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
IG> For additional commands, e-mail: log4j-user-help@logging.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org