logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Keith Hatton" <khat...@axiomsystems.com>
Subject RE: static/inherit problem
Date Fri, 04 Apr 2003 09:00:36 GMT
Doh! Of course, you are right about the private static declaration.

Why is non-static wrong "conceptually"? It seems that what you are asking for, is something
that changes per-instance, because the instances may be of different subclasses. That's exactly
what a non-static variable is for.

In fact, making it static is downright wrong, because all instances of A (regardless of which
subclass) see the same static variable. If your logger reference must be static then you must
put it individually into every subclass.

In the case where it's an instance variable in the superclass, you aren't looking up a reference
to the logger every time you log, only once per object creation. And of course you aren't
creating multiple logger instances, only one per class. This is only a potential performance
problem if you are logging within objects that are frequently created.

Keith


-----Original Message-----
From: wleana [mailto:wleana@yahoo.com]
Sent: 03 April 2003 23:04
To: log4j-user@jakarta.apache.org
Subject: Re: static/inherit problem


It doesn't make the difference...:-(
Of course, make it non-static will work technically, but conceptually 
it is not correct, and performance-wise not right either...
This should be a very common problem, how does everyone handling this?


--- In Log4J@yahoogroups.com, "Keith Hatton" <khatton@a...> wrote:
> Perhaps something like this:
> 
> class A {
> private static MyLog mLog;
> protected myId;
> A(){
>   this("A");
> }
> 
> protected A(String id) {
>   myId = id;
>   init myLog with myId...
> }
> 
> public void doSth(String aMsg){
>    mLog.debug(aMsg);
> }
> } 
> 
> class B extends A{
> B(){
>   super("B");
>  }
> }
> 
> 
> Keith
> 
> 
> -----Original Message-----
> From: wleana [mailto:wleana@y...]
> Sent: 03 April 2003 10:05
> To: log4j-user@j...
> Subject: static/inherit problem
> 
> 
> Hi, I have a design issue related to the fact that the Logger needs 
> to be static. And suppose I have A and its subclass B.
>  
> I have a wrapper MyLog that wraps around Logger and a myId field of 
> each class, in which, the log message always print out myId of its 
> caller.
> 
> class A {
> private static MyLog mLog;
> protected myId;
> A(){
>   myId = "A";
>   init myLog with myId...
> }
> 
> public void doSth(String aMsg){
>    mLog.debug(aMsg);
> }
> } 
> 
> class B extends A{
> B(){
>  myId = "B";
>  super()
>  }
> }
> 
> When I do this:
> B b=new B();
> b.doSth("blah");
> 
> The log will actually show the caller's myId is "A", instead of "B".
> (you see I even make the myId field non-static because of the same 
> issue, but actually I would like it to be static conceptually)
> 
> I have tried different ways, it only works if mLog is not static, 
the 
> within doSth from class A, it will actually dynamically use object 
of 
> type B's mLog...But we want the Logger class (or its wrapper class) 
> to belong to class not to an object...how should we solve this? 
What 
> should be the right design of this? Please share your thoughts. 
> 
> Thanks a lot.
> -wleana
> 
> 
> --------------------------------------------------------------------
-
> To unsubscribe, e-mail: log4j-user-unsubscribe@j...
> For additional commands, e-mail: log4j-user-help@j...
> 
> 
> --------------------------------------------------------------------
-
> To unsubscribe, e-mail: log4j-user-unsubscribe@j...

> For additional commands, e-mail: log4j-user-help@j...


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


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


Mime
View raw message