We have a lot of class code that has several templates, for example:
private static Logger logger = null; private static Logger getLogger() { if (logger == null) { logger = Logger.getLogger(MyClass.class); } return logger; }
The idea is that a class can log debugging information in Logger. The first code that should register something calls getLogger (), and this leads to the creation of a logger.
There are a few things that I don't like about this template. At first, singleton getLogger () is not synchronized and not synchronized, but the correct one places a burden on each subsequent call for no reason.
I really want to be able to condensate this just before this:
private static final Logger logger = Logger.getLogger(MyClass.class);
Then I can just refer to the registrar directly and not even worry about how to generate a singleton.
The problem I'm afraid of is that by doing this I cause the Logger to be created when the class loads, even if the registrar is never called. I have 10,000 + odd classes all calling getLogger (), so how many Logger instances do I really create here? If my log4j properties contain multiple additives, am I just referring to the same log again and again, or am I creating 10,000 of these things?
locka
source share