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 tony kerz <to...@sbcglobal.net> on 2006/06/01 07:19:48 UTC

why would the following result in duplicate log messages?

log4j 1.3alpha8

----------------------------------------
TestLog.java:
----------------------------------------

package com.kerz.test;

import org.apache.log4j.Logger;

public class TestLog
{
  private static Logger s_log = Logger.getLogger(TestLog.class);

  public static void main(String[] args)
  {
    s_log.debug("log4j!");
  }
}

----------------------------------------
log4j.xml:
----------------------------------------

<?xml version="1.0" encoding="UTF-8" ?>
 
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>

  <appender
    name="STDOUT"
    class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
      <param
        name="ConversionPattern"
        value="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
    </layout>
  </appender>
 
  <logger name="com.kerz.test.TestLog">
    <level value="debug" />
    <appender-ref ref="STDOUT" />
  </logger>

  <root>
    <level value="error" />
    <appender-ref ref="STDOUT" />
  </root>
 
</log4j:configuration>

----------------------------------------
console:
----------------------------------------

2006-06-01 01:04:36,937 DEBUG [main] test.TestLog (TestLog.java:23) - log4j!
2006-06-01 01:04:36,937 DEBUG [main] test.TestLog (TestLog.java:23) - log4j!

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


Re: why would the following result in duplicate log messages?

Posted by tony k <to...@sbcglobal.net>.
cool, that works perfectly and makes sense now that you explained it.

thanks a lot paul!
--
View this message in context: http://www.nabble.com/why-would-the-following-result-in-duplicate-log-messages--t1714833.html#a4677054
Sent from the Log4j - Users forum at Nabble.com.


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


Re: why would the following result in duplicate log messages?

Posted by Paul Smith <ps...@aconex.com>.
Because you have the appender attached to both the TestLog logger,  
and the root logger.  By default, any log events going to a logger go  
to any appenders attached specifically to that logger, and then  
traverses up the logger hierarchy, looking for any appender attached  
to it up to and including the root logger.

This is known as logger additivity, and something that an be turned  
off at an individual logger, so during traversal if it hits a logger  
with additivity=false, it stops going up.

If you remove the appender attached to the TestLog logger, you'll get  
one line output, because the log gets output when it hits the root  
logger (and it's attached appender).

cheers,

Paul Smith
On 01/06/2006, at 3:19 PM, tony kerz wrote:

> log4j 1.3alpha8
>
> ----------------------------------------
> TestLog.java:
> ----------------------------------------
>
> package com.kerz.test;
>
> import org.apache.log4j.Logger;
>
> public class TestLog
> {
>  private static Logger s_log = Logger.getLogger(TestLog.class);
>
>  public static void main(String[] args)
>  {
>    s_log.debug("log4j!");
>  }
> }
>
> ----------------------------------------
> log4j.xml:
> ----------------------------------------
>
> <?xml version="1.0" encoding="UTF-8" ?>
> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
>
> <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
>
>  <appender
>    name="STDOUT"
>    class="org.apache.log4j.ConsoleAppender">
>    <layout class="org.apache.log4j.PatternLayout">
>      <param
>        name="ConversionPattern"
>        value="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />
>    </layout>
>  </appender>
>  <logger name="com.kerz.test.TestLog">
>    <level value="debug" />
>    <appender-ref ref="STDOUT" />
>  </logger>
>
>  <root>
>    <level value="error" />
>    <appender-ref ref="STDOUT" />
>  </root>
> </log4j:configuration>
>
> ----------------------------------------
> console:
> ----------------------------------------
>
> 2006-06-01 01:04:36,937 DEBUG [main] test.TestLog (TestLog.java:23)  
> - log4j!
> 2006-06-01 01:04:36,937 DEBUG [main] test.TestLog (TestLog.java:23)  
> - log4j!
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>