Using same log4j logger in standalone java application
- by ktaylorjohn
I have some code which is a standalone java application comprising of 30+ classes.
Most of these inherit from some other base classes.
Each and every class has this method to get and use a log4j logger
public static Logger getLogger() {
if (logger != null) return logger;
try {
PropertyUtil propUtil = PropertyUtil.getInstance("app-log.properties");
if (propUtil != null && propUtil.getProperties() != null)
PropertyConfigurator.configure(propUtil.getProperties ());
logger = Logger.getLogger(ExtractData.class);
return logger;
} catch (Exception exception) {
exception.printStackTrace();
}
}
A) My question is whether this should be refactored to some common logger which is initialized once and used across by all classes? Is that a better practice?
B) If yes, how can this be done ? How can I pass the logger around ?
C) This is actually being used in the code not as Logger.debug() but getLogger().debug().
What is the impact of this in terms of performance?