Re: [jgroups-dev] Logging and Inheritance

Amin Abbaspour <[email protected]>
Newsgroups gmane.comp.java.javagroups.devel
Message-ID <[email protected]>
Hi,

IMHO this is a known issue and that's why it's sometimes recommended to define loggers as 'private static'.

Yes, I noticed this in our runtime and I can send the exact log as is when I get back to work but here is an example:

Suppose these two classes where B extends A:

~~~ A.java ~~~
import org.apache.log4j.Logger;
public class A {
    protected final Logger log = Logger.getLogger(this.getClass());
    protected void a() {
        log.info("I am in parent");
    }
}
~~~ B.java ~~~
public class B extends A {
    public void b() {
        a();
        log.info("I am in child");
    }
    public static void main(String[] args) {
        new B().b();
    }
}
~~~ log4j.properties ~~~
log4j.rootLogger=DEBUG,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=[%p] %c:%L - %m%n
~~~

So if you run it you'll with log4j-1.2.14 you'll get:
---
[INFO] B:5 - I am in parent
[INFO] B:4 - I am in child
---

But if you make logger as static in A and define a new one in B you'll get:
---
[INFO] A:5 - I am in parent
[INFO] B:6 - I am in child
---

As Galder said, first (current) method brings better call tracking but results in incorrect line numbers using Log4j. It differs from team to team but for me, second method's output is more clear.

Regards,
Amin

--- On Fri, 10/8/10, Bela Ban <[email protected]> wrote:

> From: Bela Ban <[email protected]>
> Subject: Re: [jgroups-dev] Logging and Inheritance
> To: "Amin Abbaspour" <[email protected]>
> Cc: [email protected]
> Date: Friday, October 8, 2010, 11:53 AM
> Can't reproduce this, do you have
> some sample code which throws 
> exceptions ? I injected a few exceptions, and the line
> numbers were 
> always correct
> 
> Amin Abbaspour wrote:
> > Hi all,
> >
> > I believe it's better to define logger in each class
> rather than using inheritance, i.e.:
> >
> > private static final Log log =
> LogFactory.getLog(MyClassName.class);
> >
> > It's like this in right now (see Protocol.java):
> >
> > protected final Log
> log=LogFactory.getLog(this.getClass());
> >
> > This adds a small bit of code but prevent incorrect
> line numbers in log. 
> >
> > With current model one may see a message in UDP:896
> while logger class is UDP and line number belongs to
> TP.java.
> >
> > Of course this means that every log call in TP results
> in logging via TP logger not the inherited one, which is
> bearable IMHO. 
> >
> >
> >   
> 
> -- 
> Bela Ban
> Lead JGroups / JBoss Clustering team
> JBoss - a division of Red Hat
> 
> 


      

------------------------------------------------------------------------------
Beautiful is writing same markup. Internet Explorer 9 supports
standards for HTML5, CSS3, SVG 1.1,  ECMAScript5, and DOM L2 & L3.
Spend less time writing and  rewriting code and more time creating great
experiences on the web. Be a part of the beta today.
http://p.sf.net/sfu/beautyoftheweb
_______________________________________________
Javagroups-development mailing list
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.