Using same log4j logger in standalone java application

Posted by ktaylorjohn on Stack Overflow See other posts from Stack Overflow or by ktaylorjohn
Published on 2010-06-14T14:32:06Z Indexed on 2010/06/14 14:42 UTC
Read the original article Hit count: 183

Filed under:
|

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?

© Stack Overflow or respective owner

Related posts about java

Related posts about log4j