public void testMemoryHandlerDefaultValue() throws SecurityException, IOException { props.clear(); props.put("java.util.logging.MemoryHandler.target", baseClassName + "$MockHandler"); manager.readConfiguration(EnvironmentHelper .PropertiesToInputStream(props)); handler = new MemoryHandler(); assertNull(handler.getFilter()); assertTrue(handler.getFormatter() instanceof SimpleFormatter); assertNull(handler.getEncoding()); assertNotNull(handler.getErrorManager()); assertEquals(handler.getLevel(), Level.ALL); assertEquals(handler.getPushLevel(), Level.SEVERE); }
protected static void dumpMemoryTrace47(java.util.logging.Logger logger) { MemoryHandler mHand = null; if (logger != null) { Handler[] handlers = logger.getHandlers(); for (int i = 0; i < handlers.length; i++) { if (handlers[i] instanceof java.util.logging.MemoryHandler) { synchronized (handlers[i]) { mHand = ((java.util.logging.MemoryHandler) handlers[i]); mHand.push(); return; } // synchronized (handler). } } // for handlers... dumpMemoryTrace47(logger.getParent()); } }
protected static void dumpMemoryTrace47(java.util.logging.Logger logger) { MemoryHandler mHand = null; if (logger!= null) { Handler[] handlers = logger.getHandlers(); for (int i=0; i<handlers.length; i++) { if (handlers[i] instanceof java.util.logging.MemoryHandler) { synchronized (handlers[i]) { mHand = ((java.util.logging.MemoryHandler)handlers[i]); mHand.push(); return; } // synchronized (handler). } } // for handlers... dumpMemoryTrace47(logger.getParent()); } }
@Override public void 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.memHandler = new MemoryHandler(this.fileHandler, bufferSize, this.level); this.log.addHandler(this.memHandler); } } else { if (this.memHandler != null) { log.removeHandler(this.memHandler); } } }
@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()); } }
protected void setUp() throws Exception { super.setUp(); manager.reset(); initProps(); manager.readConfiguration(EnvironmentHelper .PropertiesToInputStream(props)); handler = new MemoryHandler(); errSubstituteStream = new NullOutputStream(); System.setErr(new PrintStream(errSubstituteStream)); }
@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"); } }