The "x" logger inherits the root appender instances (unless you set "additivity" for "x" to "false").  But then you are adding new appender instances to the "x" logger, so you have 4 instances; 2 per/appender.  Therefore, you should expect each message to be written twice to each appender.

Remove the references to the appenders on the "x" logger, and you will get the behavior your were hoping for.

Jake

On Thu, 8 Nov 2012 16:49:49 +0800 (SGT)
 tong123123 <tong123...@yahoo.com.hk> wrote:
My log4j.properties file is as follow
log4j.rootLogger=error, A1, A2
log4j.logger.x=info, A1, A2
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout

log4j.appender.A1.layout.ConversionPattern=%d %-5p (%13F:%L) %3x - %m%n

# Appender A2 writes to the file "test".
log4j.appender.A2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.A2.DatePattern='.'yyyy-MM-dd
log4j.appender.A2.File=./debugleveldemo_logs/debugleveldemo.log


# Appender A2 uses the PatternLayout.
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%d %-5p [%t] %-17c{2} (%13F:%L) %3x - %m%n

in my project, I have a class in package x.y and named TestLog4j.
the relevant code is as follow:
static Logger logger = Logger.getLogger(TestLog4j.class);

void method1(){
        Properties props = new Properties();
        try {
            // the file need placed in C:\workspace\workspace1\TestLog4j {root folder}
            props.load(new FileInputStream("log4j.properties"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        PropertyConfigurator.configure(props);
        logger.debug("debug");
        logger.info("info");
        logger.warn("warn");
        System.out.println("see system out");
    }

the output log message is duplicate as follow:
2012-11-08 16:37:10,478 INFO  (TestLog4j.java:30)     - info
2012-11-08 16:37:10,478 INFO  (TestLog4j.java:30)     - info
2012-11-08 16:37:10,490 WARN  (TestLog4j.java:31)     - warn
2012-11-08 16:37:10,490 WARN  (TestLog4j.java:31)     - warn
see system out

why each log message is displayed twice? for rootLogger, the level is error, so should not be triggered, and only log4j.logger.x is triggered, so I expected the debug message should be displayed only once.


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

Reply via email to