java.util.logging.MemoryHandler Java Examples

The following examples show how to use java.util.logging.MemoryHandler. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MemoryHandlerJULBenchmark.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #2
Source File: MemoryHandlerJULLocationBenchmark.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #3
Source File: OldMemoryHandlerTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
@Override protected void setUp() throws Exception {
    super.setUp();
    manager.reset();
    initProps();
    manager.readConfiguration(propertiesToInputStream(props));
    handler = new MemoryHandler();
}
 
Example #4
Source File: HandlersConfigTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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);
    }
}
 
Example #5
Source File: HandlersConfigTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@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);
    }
}
 
Example #6
Source File: HandlersConfigTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #7
Source File: HandlersConfigTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
Handler getTarget(MemoryHandler memoryHandler) {
    try {
        return (Handler) memoryHandlerTarget.get(memoryHandler);
    } catch (IllegalAccessException e) {
        throw new IllegalAccessError(e.getMessage());
    }
}
 
Example #8
Source File: HandlersConfigTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
int getSize(MemoryHandler memoryHandler) {
    try {
        return (int) memoryHandlerSize.get(memoryHandler);
    } catch (IllegalAccessException e) {
        throw new IllegalAccessError(e.getMessage());
    }
}
 
Example #9
Source File: PLogger.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #10
Source File: MemoryHandlerTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}
 
Example #11
Source File: MemoryHandlerTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}
 
Example #12
Source File: MemoryHandlerTest.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}
 
Example #13
Source File: MemoryHandlerTest.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}
 
Example #14
Source File: MemoryHandlerTest.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}
 
Example #15
Source File: MemoryHandlerTest.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}
 
Example #16
Source File: MemoryHandlerTest.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}
 
Example #17
Source File: MemoryHandlerTest.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}
 
Example #18
Source File: MemoryHandlerTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}
 
Example #19
Source File: MemoryHandlerTest.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}
 
Example #20
Source File: MemoryHandlerTest.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}
 
Example #21
Source File: MemoryHandlerTest.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}
 
Example #22
Source File: MemoryHandlerTest.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
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");
    }
}