Why is the new instance using the registrar from the old instances?

I am generating 2 instances this way:

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

      

The start()

gameManager method looks like this:

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

      

When I instantiate the game, I create loger: log = Logger.getLogger("TestLog");

( log

is the public field of the class it belongs to game

).

In game.start()

I start many processes and give them a link to the corresponding one log

. So I expect manager1 and manager2 to write to different files. But manager2 writes to its file and to manager1's log file. Why might this happen?

+2


a source to share


3 answers


You use the same log name "TestLog" both times, so you get the same instance Logger

both times. This is how this class works. See here if you are using the built-in class java.util.logging.Logger

. Other log packages probably have similar behavior.



+2


a source


Assuming you are using Log4J or java.util.logging (which is based on Log4J) there will only be one registered per class / name. No matter how many times you call a method getLogger

with the same argument, it will return exactly the same object to you.

Therefore, when you call getLogger("TestLog")

on each of your instances, you will get a reference to the same object Logger

. Hence, it will only log in one place, the one configured for the "TestLog" logger.



If you want the output from these two instances to go to different locations, you will need to call them getLogger

with different arguments so that they can receive different loggers (and of course, set up the logging system so that the two loggers have different outputs).

0


a source


On most sides of the logging framework, the name you put in the getLogger method makes the logger unique, not the class instance. So when you call Logger.getLogger("TestLog");

, you get the same logger in every case.

0


a source







All Articles