Re: [jgroups-dev] Logging and Inheritance
Brought to you by:
belaban
From: Amin A. <abb...@ya...> - 2010-10-08 20:30:19
|
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 <be...@ya...> wrote: > From: Bela Ban <be...@ya...> > Subject: Re: [jgroups-dev] Logging and Inheritance > To: "Amin Abbaspour" <abb...@ya...> > Cc: jav...@li... > 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 > > |