本文整理汇总了Java中java.util.logging.MemoryHandler.publish方法的典型用法代码示例。如果您正苦于以下问题:Java MemoryHandler.publish方法的具体用法?Java MemoryHandler.publish怎么用?Java MemoryHandler.publish使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.logging.MemoryHandler
的用法示例。
在下文中一共展示了MemoryHandler.publish方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.util.logging.MemoryHandler; //导入方法依赖的package包/类
public static void main(String... args) throws IOException {
// load logging.propertes for the test
String tstSrc = System.getProperty("test.src", ".");
File fname = new File(tstSrc, LM_PROP_FNAME);
String prop = fname.getCanonicalPath();
System.setProperty(CFG_FILE_PROP, prop);
LogManager.getLogManager();
// create a logger
logger = Logger.getLogger(MemoryHandlerTest.class.getName());
// don't have parent handlers get log messages
logger.setUseParentHandlers(false);
//
// Test 1,2: create a CustomMemoryHandler which in the config has
// specified a target of CustomTargetHandler. (1) Make sure that it
// is created and (2) that the target handler is loaded.
//
CustomMemoryHandler cmh = new CustomMemoryHandler();
try {
logger.addHandler(cmh);
} catch (RuntimeException rte) {
throw new RuntimeException(
"Test Failed: did not load java.util.logging.ConsoleHandler as expected",
rte);
}
// if we get here and our config has been processed properly, then we
// should have loaded our target handler
if (CustomTargetHandler.numLoaded !=1) {
throw new RuntimeException(
"Test failed: did not load CustomTargetHandler as expected");
}
//
// Test 3: try to add a handler with no target. This should fail with
// an exception
CustomMemoryHandlerNoTarget cmhnt = null;
try {
cmhnt = new CustomMemoryHandlerNoTarget();
} catch (RuntimeException re) {
// expected -- no target specified
System.out.println("Info: " + re.getMessage() + " as expected.");
}
if (cmhnt != null) {
throw new RuntimeException(
"Test Failed: erroneously loaded CustomMemoryHandlerNoTarget");
}
// Test 4: log a message and check that the target handler is actually used
logger.log(Level.WARNING, "Unused");
if (CustomTargetHandler.numPublished != 1) {
throw new RuntimeException("Test failed: CustomTargetHandler was not used");
}
// Test 5: make sure that SimpleTargetHandler hasn't been erroneously called
if (SimpleTargetHandler.numPublished != 0) {
throw new RuntimeException("Test failed: SimpleTargetHandler has been used");
}
// Test 6: try using SimpleTargetHanlder via standard MemoryHandler
// (which has target set to SimpleTargetHandler)
MemoryHandler mh = new MemoryHandler();
mh.publish(new LogRecord(Level.INFO, "Unused msg to MemoryHandler"));
// see if it made it to the SimpleTargetHandler
if (SimpleTargetHandler.numPublished != 1) {
throw new RuntimeException("Test failed: SimpleTargetHandler was not used");
}
}