下面列出了怎么用org.apache.log4j.spi.Configurator的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Read the configuration file <code>configFilename</code> if it
* exists. Moreover, a thread will be created that will periodically
* check if <code>configFilename</code> has been created or
* modified. The period is determined by the <code>delay</code>
* argument. If a change or file creation is detected, then
* <code>configFilename</code> is read to configure log4j.
*
* @param configFilename A log4j configuration file in XML format.
* @param delay The delay in milliseconds to wait between each check.
*/
public static void configureAndWatch(final String configFilename, final long delay) {
try {
File file = new File(configFilename);
InputStream is = new FileInputStream(file);
ConfigurationSource source = new ConfigurationSource(is, file);
int seconds = (int) TimeUnit.MILLISECONDS.toSeconds(delay);
XmlConfigurationFactory factory = new XmlConfigurationFactory(source, seconds);
factory.doConfigure();
org.apache.logging.log4j.core.config.Configurator.reconfigure(factory.getConfiguration());
} catch (IOException ioe) {
LOGGER.error("Unable to process configuration file {} due to {}", configFilename, ioe.getMessage());
}
}
/**
* A static version of doConfigure(URL).
*/
public static void configure(final URL url) throws FactoryConfigurationError {
try {
InputStream is = url.openStream();
ConfigurationSource source = new ConfigurationSource(is, url);
XmlConfigurationFactory factory = new XmlConfigurationFactory(source, 0);
factory.doConfigure();
org.apache.logging.log4j.core.config.Configurator.reconfigure(factory.getConfiguration());
} catch (IOException ioe) {
LOGGER.error("Unable to process configuration {} due to {}", url.toString(), ioe.getMessage());
}
}