下面列出了怎么用java.util.logging.MemoryHandler的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public void run() {
// MemoryHandler
check(new MemoryHandler(),
Level.ALL, null, null, SimpleFormatter.class,
ConfiguredHandler.class, 1000, Level.SEVERE);
check(new MemoryHandler(new SpecifiedHandler(), 100, Level.WARNING),
Level.ALL, null, null, SimpleFormatter.class,
SpecifiedHandler.class, 100, Level.WARNING);
// StreamHandler
check(new StreamHandler(),
Level.INFO, null, null, SimpleFormatter.class,
null);
check(new StreamHandler(System.out, new SpecifiedFormatter()),
Level.INFO, null, null, SpecifiedFormatter.class,
System.out);
// ConsoleHandler
check(new ConsoleHandler(),
Level.INFO, null, null, SimpleFormatter.class,
System.err);
// SocketHandler (use the ServerSocket's port)
try {
check(new SocketHandler("localhost", serverSocket.getLocalPort()),
Level.ALL, null, null, XMLFormatter.class);
} catch (IOException e) {
throw new RuntimeException("Can't connect to localhost:" + serverSocket.getLocalPort(), e);
}
}
@Override
public void run() {
// MemoryHandler
check(new MemoryHandler(),
Level.FINE, null, ConfiguredFilter.class, ConfiguredFormatter.class,
ConfiguredHandler.class, 123, Level.FINE);
check(new MemoryHandler(new SpecifiedHandler(), 100, Level.WARNING),
Level.FINE, null, ConfiguredFilter.class, ConfiguredFormatter.class,
SpecifiedHandler.class, 100, Level.WARNING);
// StreamHandler
check(new StreamHandler(),
Level.FINE, "ASCII", ConfiguredFilter.class, ConfiguredFormatter.class,
null);
check(new StreamHandler(System.out, new SpecifiedFormatter()),
Level.FINE, "ASCII", ConfiguredFilter.class, SpecifiedFormatter.class,
System.out);
// ConsoleHandler
check(new ConsoleHandler(),
Level.FINE, "ASCII", ConfiguredFilter.class, ConfiguredFormatter.class,
System.err);
// SocketHandler (use the ServerSocket's port)
try {
check(new SocketHandler("localhost", serverSocket.getLocalPort()),
Level.FINE, "ASCII", ConfiguredFilter.class, ConfiguredFormatter.class);
} catch (Exception e) {
throw new RuntimeException("Can't connect to localhost:" + serverSocket.getLocalPort(), e);
}
}
void check(MemoryHandler handler,
Level expectedLevel,
String expectedEncoding,
Class<? extends Filter> expectedFilterType,
Class<? extends Formatter> expectedFormatterType,
Class<? extends Handler> expextedTargetType,
int expextedSize,
Level expectedPushLevel) {
checkType(handler, "target", getTarget(handler), expextedTargetType);
checkEquals(handler, "size", getSize(handler), expextedSize);
checkEquals(handler, "pushLevel", handler.getPushLevel(), expectedPushLevel);
check(handler, expectedLevel, expectedEncoding, expectedFilterType, expectedFormatterType);
}
Handler getTarget(MemoryHandler memoryHandler) {
try {
return (Handler) memoryHandlerTarget.get(memoryHandler);
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
}
}
int getSize(MemoryHandler memoryHandler) {
try {
return (int) memoryHandlerSize.get(memoryHandler);
} catch (IllegalAccessException e) {
throw new IllegalAccessError(e.getMessage());
}
}
@Override
public boolean enableFileOut(String filepattern, boolean check, int bufferSize, int fileSize, int fileCount,
boolean isAppend, Formatter format) {
if (check == true) {
if (this.fileHandler == null) {
initFileHandler(filepattern, fileSize, fileCount, isAppend);
}
if (this.fileHandler != null) {
this.fileHandler.setLevel(this.level);
this.fileHandler.setFormatter(format);
}
/**
* NOTE: we use async log buffer
*/
if (this.memHandler == null&& this.fileHandler != null) {
this.memHandler = new MemoryHandler(this.fileHandler, bufferSize, this.level);
this.log.addHandler(this.memHandler);
isEnableFileOutSus = true;
}
}
else {
if (this.memHandler != null) {
log.removeHandler(this.memHandler);
isEnableFileOutSus = false;
}
}
return isEnableFileOutSus;
}
@Override protected void setUp() throws Exception {
super.setUp();
manager.reset();
initProps();
manager.readConfiguration(propertiesToInputStream(props));
handler = new MemoryHandler();
}
@Setup(Level.Trial)
public void up() {
memoryHandler = new MemoryHandler(new NoOpJULHandler(), 262144, java.util.logging.Level.SEVERE);
logger = Logger.getLogger(getClass().getName());
logger.setUseParentHandlers(false);
logger.addHandler(memoryHandler);
logger.setLevel(java.util.logging.Level.ALL);
}
@Setup(Level.Trial)
public void up() {
memoryHandler = new MemoryHandler(new NoOpJULHandler(), 262144, java.util.logging.Level.SEVERE);
logger = java.util.logging.Logger.getLogger(getClass().getName());
logger.setUseParentHandlers(false);
logger.addHandler(memoryHandler);
logger.setLevel(java.util.logging.Level.ALL);
}
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");
}
}
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");
}
}
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");
}
}
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");
}
}
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");
}
}
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");
}
}
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");
}
}
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");
}
}
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");
}
}
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");
}
}
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");
}
}
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");
}
}
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");
}
}