1 25 26 package org.objectweb.jonas.common; 27 28 import java.util.HashMap ; 29 import java.util.Map ; 30 import java.util.Properties ; 31 import java.util.TreeSet ; 32 33 import javax.ejb.EJBException ; 34 import javax.management.Notification ; 35 import javax.management.NotificationBroadcaster ; 36 import javax.management.NotificationEmitter ; 37 import javax.management.NotificationFilter ; 38 import javax.management.NotificationListener ; 39 40 import org.objectweb.jonas.jms.JmsServiceImplMBean; 41 import org.objectweb.jonas.management.ReconfigDispatcher; 42 import org.objectweb.jonas.management.ReconfiguredProp; 43 44 import org.objectweb.util.monolog.Monolog; 45 import org.objectweb.util.monolog.api.Handler; 46 import org.objectweb.util.monolog.api.Logger; 47 import org.objectweb.util.monolog.api.Level; 48 import org.objectweb.util.monolog.api.LoggerFactory; 49 import org.objectweb.util.monolog.api.MonologFactory; 50 51 public class LogManagement extends ReconfigDispatcher implements LogManagementMBean { 52 53 long sequenceNumber = 0; 55 56 public static final String SERVICE_NAME = "log"; 58 59 62 private static LogManagement unique = null; 63 64 public static LogManagement getInstance() { 65 if (unique == null) { 66 unique = new LogManagement(); 67 } 68 return unique; 69 } 70 71 74 public String [] getTopics() { 75 Logger[] logs = Log.getLoggerFactory().getLoggers(); 76 TreeSet tset = new TreeSet (); 78 for (int i = 0; i < logs.length; i++) { 79 tset.add(logs[i].getName()); 80 } 81 return (String []) tset.toArray(new String [0]); 82 } 83 84 87 public String getTopicLevel(String topic) { 88 String ret = null; 89 Logger topicLogger = Log.getLoggerFactory().getLogger(topic); 90 Level lev = topicLogger.getCurrentLevel(); 91 return lev.getName(); 92 } 93 94 97 public void setTopicLevel(String topic, String level) { 98 Logger topicLogger = Log.getLoggerFactory().getLogger(topic); 99 Level lev = Log.getLevelFactory().getLevel(level); 100 if (lev != null) { 102 topicLogger.setLevel(lev); 103 } else { 104 throw new EJBException ("Unknown level " + level); 105 } 106 String propName = "logger." + topic + ".level"; 108 sendReconfigNotification(++sequenceNumber, SERVICE_NAME, new ReconfiguredProp(propName, level)); 111 } 112 113 116 public Properties getProperties() { 117 Properties props = Log.getProperties(); 118 if (props == null) { 119 Log.getLoggerFactory(); 120 props = Log.getProperties(); 121 } 122 return props; 123 } 124 125 public void saveConfig() { 126 sendSaveNotification(++sequenceNumber, SERVICE_NAME); 128 } 129 130 131 135 public String [] getHandlerNames() { 136 LoggerFactory lf = Log.getLoggerFactory(); 137 138 if (lf instanceof MonologFactory) { 139 MonologFactory mf = (MonologFactory) lf; 140 Handler[] hs = mf.getHandlers(); 141 String [] hns = new String [hs.length]; 142 for (int i = 0; i < hs.length; i++) { 143 hns[i] = hs[i].getName(); 144 } 145 return hns; 146 } 147 return null; 148 } 149 150 154 public Map getHandlerAttributes(String handlername) { 155 LoggerFactory lf = Log.getLoggerFactory(); 156 157 if (lf instanceof MonologFactory) { 158 MonologFactory mf = (MonologFactory) lf; 159 160 Handler h = mf.getHandler(handlername); 161 String [] ans = h.getAttributeNames(); 162 Map m = new HashMap (ans.length); 163 for (int i = 0; i < ans.length; i++) { 164 m.put(ans[i], h.getAttribute(ans[i])); 165 } 166 return m; 167 } 168 return null; 169 } 170 171 178 public void addNotificationListener(NotificationListener arg0, NotificationFilter arg1, Object arg2) 179 throws IllegalArgumentException { 180 Handler handler = Monolog.getMonologFactory().getHandler("jmxHandler"); 181 if (handler != null && (handler instanceof NotificationEmitter )) { 182 ((NotificationEmitter ) handler).addNotificationListener(arg0, arg1, arg2); 183 } 184 super.addNotificationListener(arg0, arg1, arg2); 185 } 186 187 } 188 | Popular Tags |