wood burning stoves 2.0*
The moose likes Java in General and the fly likes Apache Log4j with static context Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Apache Log4j with static context" Watch "Apache Log4j with static context" New topic

Apache Log4j with static context

Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134


The below code creates two instances of Logger, couldnt figure out why.


Logger with static modifier, should get initialized once when class is loaded, but it gets two instance instead of one. Removing the static modifier solves my problem.What is the relevance that it creates two with a static modifier.

Thanks & Regards
Sidharth Pallai
Martin Vajsar

Joined: Aug 22, 2010
Posts: 3606

There are two static variables of type of Logger in your code, _logger and log. Both seem to be initialized in the declaration. If you make one of them non-static, the corresponding instance does not get created, since the Test class is not instantiated.
Sidharth Pallai
Ranch Hand

Joined: Apr 21, 2008
Posts: 134

It took me much time to identify the space after the string "com.aaa.TelnetMgr" in the the array declared in the interface, for which it was creating two instances for the same logger. Sorry to take your valuable time folks.
It is sorta covered in the JavaRanch Style Guide.
subject: Apache Log4j with static context
Similar Threads
Inner classes and inheritance
Junit Help
Can Logger be protected?
why protected modifier is not allowed?