@Test @SuppressWarnings("unchecked") public void getLoggerAppenders() { List<String> list = (List<String>) jmxClientTemplate.invoke( Log4jMBean.LOG4J_MBEAN_NAME, "getLoggerAppenders", new Class[] {String.class}, new Object[] {"org.springside"}); assertEquals(2, list.size()); assertEquals("Console(parent)", list.get(0)); assertEquals("RollingFile(parent)", list.get(1)); list = (List<String>) jmxClientTemplate.invoke( Log4jMBean.LOG4J_MBEAN_NAME, "getLoggerAppenders", new Class[] {String.class}, new Object[] {"DBLogExample"}); assertEquals(2, list.size()); assertEquals("Console", list.get(0)); assertEquals("DBLog", list.get(1)); }
@Test public void accessLoggerLevel() { String loggerName = "foo"; String oldLevel = "WARN"; String newLevel = "ERROR"; // 判断原级别 assertEquals( oldLevel, jmxClientTemplate.invoke( Log4jMBean.LOG4J_MBEAN_NAME, "getLoggerLevel", new Class[] {String.class}, new Object[] {loggerName})); // 设定新级别 jmxClientTemplate.invoke( Log4jMBean.LOG4J_MBEAN_NAME, "setLoggerLevel", new Class[] {String.class, String.class}, new Object[] {loggerName, newLevel}); assertEquals(newLevel, Logger.getLogger(loggerName).getEffectiveLevel().toString()); // 恢复原级别 jmxClientTemplate.invoke( Log4jMBean.LOG4J_MBEAN_NAME, "setLoggerLevel", new Class[] {String.class, String.class}, new Object[] {loggerName, oldLevel}); }
@Test public void accessRootLoggerLevel() { String oldLevel = "WARN"; String newLevel = "ERROR"; // 判断原级别 assertEquals( oldLevel, jmxClientTemplate.getAttribute(Log4jMBean.LOG4J_MBEAN_NAME, "RootLoggerLevel")); // 设定新级别 jmxClientTemplate.setAttribute(Log4jMBean.LOG4J_MBEAN_NAME, "RootLoggerLevel", newLevel); assertEquals(newLevel, Logger.getRootLogger().getLevel().toString()); // 恢复原级别 jmxClientTemplate.setAttribute(Log4jMBean.LOG4J_MBEAN_NAME, "RootLoggerLevel", oldLevel); }
@After public void tearDown() throws Exception { jmxClientTemplate.close(); }