log4j - prints multiple times same output

log4j prints the same output multiple times - when using BasicConfigurator

What is the problem?

I'm using log4j, latest release 1.2.12. Fine, no problems. Only one little thing: duplicated logging output when invoking BasicConfigurator.configure() multiple times.

At first some code. We have two classes, A and B (this was how I spotted that problem).
Code-download

Class A:


package net.enarion.java.log4j.duplicatedError;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

/**
 * class A
 * @author Tobias Kluge, enarion.net
 * @url http://enarion.net/java/log4j/double-lines-with-same-content/
 */
public class A {
    static Logger logger = Logger.getLogger(A.class);
    public void say(int i) {
      BasicConfigurator.configure();
      logger.error("hello, world! - number " + i);
    }
}


class B:


package net.enarion.java.log4j.duplicatedError;

import org.apache.log4j.Logger;

/**
 * class B
 * @author Tobias Kluge, enarion.net
 * @url http://enarion.net/java/log4j/double-lines-with-same-content/
 */
public class B {
  static Logger logger = Logger.getLogger(B.class);
  public void say(A a, int i) {
    a.say(i);
  }

  public static void main(String[] args) {
      A a = new A();
      B b = new B();
      b.say(a, 1);
      b.say(a, 2);
      b.say(a, 3);
  }
}

The output

What should be the output?

0 [main] ERROR net.enarion.java.log4j.duplicatedError.A  - hello, world! - number 1
0 [main] ERROR net.enarion.java.log4j.duplicatedError.A  - hello, world! - number 2
0 [main] ERROR net.enarion.java.log4j.duplicatedError.A  - hello, world! - number 3

But what is the output? Can you guess it?

0 [main] ERROR net.enarion.java.log4j.duplicatedError.A  - hello, world! - number 1
0 [main] ERROR net.enarion.java.log4j.duplicatedError.A  - hello, world! - number 2
0 [main] ERROR net.enarion.java.log4j.duplicatedError.A  - hello, world! - number 2
0 [main] ERROR net.enarion.java.log4j.duplicatedError.A  - hello, world! - number 3
0 [main] ERROR net.enarion.java.log4j.duplicatedError.A  - hello, world! - number 3
0 [main] ERROR net.enarion.java.log4j.duplicatedError.A  - hello, world! - number 3

What's the solution?
Hmm, I think this is a bug. It works to comment out BasicConfigurator.configure(); in the function say of class A. That's it.

Bug report: http://issues.apache.org/bugzilla/show_bug.cgi?id=37768

Google Adsense