views:

41

answers:

3

I generate 2 instances in this way:

    gameManager manager1 = new CTManager(owner,players1,"en");
    manager1.start();
    gameManager manager2 = new CTManager(owner,players2,"en");
    manager2.start();

The start() method of the gameManager looks like that:

void start() {
    game.start();
}

When I create the game instance I create a loger: log = Logger.getLogger("TestLog"); (log is a public field of the class in which the game belongs).

In the game.start() I run many processes and give them a reference to the corresponding log. So, I expect that manager1 and manager2 will write to different files. But manager2 writes to its own file and to the log file of the manager1. Why can it happen?

A: 

Assuming you're using Log4J or java.util.logging (which is based on Log4J), there will only be one logged per class/name. No matter how many times you call the getLogger method with the same argument, it will return you exactly the same object.

So when you call getLogger("TestLog") in each of your instances, you will get a reference to the exact same Logger object. Consequently it is only going to log to one place, the one configured for the "TestLog" logger.

If you want to have the output from these two instances go to different places, you will need to have them call getLogger with different arguments so that they get distinct loggers (and of course configure the logging system such that these two loggers have different outputs).

Andrzej Doyle
+2  A: 

You're using the same log name, "TestLog", both times, so you're getting the same instance of Logger both times. That's how that class works. See here, if you're using the built-in java.util.logging.Logger class. Other logging packages likely have similar behavior.

Syntactic
A: 

From most any logging framework's perspective, the name you put in the getLogger method is what makes the logger unique, not the class instance. So when you call Logger.getLogger("TestLog"); you get the same logger in each case.

Yishai