Re: [jgroups-dev] Logging and Inheritance

Bela Ban <[email protected]>
Newsgroups gmane.comp.java.javagroups.devel
Message-ID <[email protected]>
While the line numbers given are for the wrong class in such a case, I'm 
not going to change the way loggers are created because

    * I want to have 1 logger for a protocol, not 2 or more
    * I don't want to break the OO principle of inheritance
    * Showing line numbers is bad, and should *never* be used in
      production as it is very slow to grab the line numbers (throwing a
      random Throwable, catching it and parsing the first line in the
      stack). This may not even work if we use javac without debug options



Amin Abbaspour wrote:
> 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
>>
>>
>
>
>
>

-- 
Bela Ban
Lead JGroups / Clustering Team
JBoss

------------------------------------------------------------------------------
Download new Adobe(R) Flash(R) Builder(TM) 4
The new Adobe(R) Flex(R) 4 and Flash(R) Builder(TM) 4 (formerly 
Flex(R) Builder(TM)) enable the development of rich applications that run
across multiple browsers and platforms. Download your free trials today!
http://p.sf.net/sfu/adobe-dev2dev
_______________________________________________
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.