Here is an example of a simple class, which can be used at the very beginning of any Java application to setup Log4j configuration. Configuration may be located wherever we want and it doesn't need to be named as default "log4j.properties".
public abstract class LoggerSetup {
private static Logger logger;
// Period in [ms] between checking for logger properties change
private static final long CHECK_PERIOD = 20000;
/**
* Set logger file properties
* @param filename - path to log4j properties file
*/
public static void setLogger(String filename) {
File f = new File(filename);
if (f.exists()) {
PropertyConfigurator.configureAndWatch(filename, 20000);
}
logger = Logger.getLogger(LoggerSetup.class);
logger.debug("Setup logger");
}
}
As we can see, every 20 seconds, Log4j check whether properties file has changed. If so, it will reload logger setup. It means that during application execution you may change logger levels, appenders, formats etc. without the need
for application restart.
No comments:
Post a Comment