下面列出了怎么用org.apache.log4j.helpers.FileWatchdog的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testConfigDirLog4jConfig() throws IOException {
RuntimeInfo runtimeInfo = Mockito.mock(RuntimeInfo.class);
File configDir = new File("target", UUID.randomUUID().toString());
Assert.assertTrue(configDir.mkdirs());
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("log4j.properties");
OutputStream os = new FileOutputStream(new File(configDir, "log4j.properties"));
IOUtils.copy(is, os);
is.close();
os.close();
Mockito.when(runtimeInfo.getConfigDir()).thenReturn(configDir.getAbsolutePath());
Mockito.when(runtimeInfo.getLog4jPropertiesFileName()).thenReturn("log4j.properties");
new LogConfigurator(runtimeInfo).configure();
Mockito.verify(runtimeInfo, Mockito.times(1)).getConfigDir();
boolean foundFileWatcher = false;
for (Thread thread : Thread.getAllStackTraces().keySet()) {
foundFileWatcher |= (thread instanceof FileWatchdog);
}
Assert.assertTrue(foundFileWatcher);
Mockito.verify(runtimeInfo, Mockito.times(1)).setAttribute(Mockito.eq(RuntimeInfo.LOG4J_CONFIGURATION_URL_ATTR),
Mockito.any());
}
/**
* Check that changing log4j config file causes the logger configuration to be updated.
* String-accepting constructor is used.
*/
@Test
public void testConfigChangeStringConstructorDefaultDelay() throws Exception {
checkConfigUpdate(new Log4JLoggerSupplier() {
@Override public Log4JLogger get(File cfgFile) throws Exception {
return new Log4JLogger(cfgFile.getPath());
}
}, FileWatchdog.DEFAULT_DELAY + 5000); // use larger delay to avoid relying on exact timing
}
@After
public void cleanUp() throws Exception {
System.getProperties().remove("log4j.configuration");
System.getProperties().remove("log4j.defaultInitOverride");
for (Thread thread : Thread.getAllStackTraces().keySet()) {
if (thread instanceof FileWatchdog) {
Field interrupted = ((Class)thread.getClass().getGenericSuperclass()).getDeclaredField("interrupted");
interrupted.setAccessible(true);
interrupted.set(thread, true);
thread.interrupt();
}
}
}
@Test
public void testClasspathLog4jConfig() {
RuntimeInfo runtimeInfo = Mockito.mock(RuntimeInfo.class);
File configDir = new File("target", UUID.randomUUID().toString());
Assert.assertTrue(configDir.mkdirs());
Mockito.when(runtimeInfo.getConfigDir()).thenReturn(configDir.getAbsolutePath());
Mockito.when(runtimeInfo.getLog4jPropertiesFileName()).thenReturn("log4j.properties");
new LogConfigurator(runtimeInfo).configure();
Mockito.verify(runtimeInfo, Mockito.times(1)).getConfigDir();
for (Thread thread : Thread.getAllStackTraces().keySet()) {
Assert.assertFalse(thread instanceof FileWatchdog);
}
Mockito.verify(runtimeInfo, Mockito.times(1)).setAttribute(Mockito.eq(RuntimeInfo.LOG4J_CONFIGURATION_URL_ATTR),
Mockito.any());
}
/**
* Creates new logger with given configuration {@code path}.
* Calling this constructor is equivalent to calling {@code Log4JLogger(path, FileWatchdog.DEFAULT_DELAY}.
*
* @param path Path to log4j configuration XML file.
* @throws IgniteCheckedException Thrown in case logger can't be created.
*/
public Log4JLogger(final String path) throws IgniteCheckedException {
this(path, FileWatchdog.DEFAULT_DELAY);
}
/**
* Creates new logger with given configuration {@code cfgFile}.
* Calling this constructor is equivalent to calling {@code Log4JLogger(cfgFile, FileWatchdog.DEFAULT_DELAY}.
*
* @param cfgFile Log4j configuration XML file.
* @throws IgniteCheckedException Thrown in case logger can't be created.
*/
public Log4JLogger(File cfgFile) throws IgniteCheckedException {
this(cfgFile, FileWatchdog.DEFAULT_DELAY);
}
/**
* Creates new logger with given configuration {@code cfgUrl}.
* Calling this constructor is equivalent to calling {@code Log4JLogger(cfgUrl, FileWatchdog.DEFAULT_DELAY}.
*
* @param cfgUrl URL for Log4j configuration XML file.
* @throws IgniteCheckedException Thrown in case logger can't be created.
*/
public Log4JLogger(final URL cfgUrl) throws IgniteCheckedException {
this(cfgUrl, FileWatchdog.DEFAULT_DELAY);
}
/**
Like {@link #configureAndWatch(String, long)} except that the
default delay as defined by {@link FileWatchdog#DEFAULT_DELAY} is
used.
@param configFilename A file in key=value format.
*/
static
public
void configureAndWatch(String configFilename) {
configureAndWatch(configFilename, FileWatchdog.DEFAULT_DELAY);
}
/**
Like {@link #configureAndWatch(String, long)} except that the
default delay as defined by {@link FileWatchdog#DEFAULT_DELAY} is
used.
@param configFilename A log4j configuration file in XML format.
*/
static
public
void configureAndWatch(String configFilename) {
configureAndWatch(configFilename, FileWatchdog.DEFAULT_DELAY);
}