1 8 9 package mx4j.log; 10 11 import java.security.AccessController ; 12 import java.security.PrivilegedAction ; 13 import java.util.HashMap ; 14 import java.util.Map ; 15 import javax.management.RuntimeOperationsException ; 16 17 import mx4j.MX4JSystemKeys; 18 19 34 public class Log 35 { 36 private static Logger m_prototype; 37 private static Map m_prototypeMap = new HashMap (); 38 private static Map m_loggerCache = new HashMap (); 39 private static int m_defaultPriority; 40 41 static 42 { 43 String priority = (String )AccessController.doPrivileged(new PrivilegedAction () 45 { 46 public Object run() 47 { 48 return System.getProperty(MX4JSystemKeys.MX4J_LOG_PRIORITY); 49 } 50 }); 51 if ("trace".equalsIgnoreCase(priority)) 52 { 53 m_defaultPriority = Logger.TRACE; 54 } 55 else if ("debug".equalsIgnoreCase(priority)) 56 { 57 m_defaultPriority = Logger.DEBUG; 58 } 59 else if ("info".equalsIgnoreCase(priority)) 60 { 61 m_defaultPriority = Logger.INFO; 62 } 63 else if ("warn".equalsIgnoreCase(priority)) 64 { 65 m_defaultPriority = Logger.WARN; 66 } 67 else if ("error".equalsIgnoreCase(priority)) 68 { 69 m_defaultPriority = Logger.ERROR; 70 } 71 else if ("fatal".equalsIgnoreCase(priority)) 72 { 73 m_defaultPriority = Logger.FATAL; 74 } 75 else 76 { 77 m_defaultPriority = Logger.INFO; 78 } 79 80 String prototype = (String )AccessController.doPrivileged(new PrivilegedAction () 81 { 82 public Object run() 83 { 84 return System.getProperty(MX4JSystemKeys.MX4J_LOG_PROTOTYPE); 85 } 86 }); 87 if (prototype != null && prototype.trim().length() > 0) 88 { 89 try 90 { 91 ClassLoader cl = Thread.currentThread().getContextClassLoader(); 92 Class cls = cl.loadClass(prototype); 93 redirectTo((Logger)cls.newInstance()); 94 } 95 catch (Exception x) 96 { 97 x.printStackTrace(); 98 } 101 } 102 } 103 104 private Log() 105 { 106 } 107 108 113 public static void setDefaultPriority(int priority) 114 { 115 switch (priority) 116 { 117 case Logger.TRACE: 118 m_defaultPriority = Logger.TRACE; 119 break; 120 case Logger.DEBUG: 121 m_defaultPriority = Logger.DEBUG; 122 break; 123 case Logger.INFO: 124 m_defaultPriority = Logger.INFO; 125 break; 126 case Logger.WARN: 127 m_defaultPriority = Logger.WARN; 128 break; 129 case Logger.ERROR: 130 m_defaultPriority = Logger.ERROR; 131 break; 132 case Logger.FATAL: 133 m_defaultPriority = Logger.FATAL; 134 break; 135 default: 136 m_defaultPriority = Logger.WARN; 137 break; 138 } 139 } 140 141 146 public static int getDefaultPriority() 147 { 148 return m_defaultPriority; 149 } 150 151 157 public static Logger getLogger(String category) 158 { 159 if (category == null) 160 { 161 throw new RuntimeOperationsException (new IllegalArgumentException ("Category cannot be null")); 162 } 163 164 synchronized (m_loggerCache) 165 { 166 Logger logger = (Logger)m_loggerCache.get(category); 167 if (logger == null) 168 { 169 Logger prototype = null; 171 synchronized (m_prototypeMap) 172 { 173 prototype = (Logger)m_prototypeMap.get(category); 174 } 175 if (prototype == null) 176 { 177 if (m_prototype != null) 179 { 180 logger = createLogger(m_prototype, category); 181 } 182 else 183 { 184 logger = createLogger(null, category); 185 } 186 } 187 else 188 { 189 logger = createLogger(prototype, category); 190 } 191 192 m_loggerCache.put(category, logger); 194 } 195 return logger; 196 } 197 } 198 199 private static Logger createLogger(Logger prototype, String category) 200 { 201 Logger logger = null; 202 try 203 { 204 logger = prototype == null ? new Logger() : (Logger)prototype.getClass().newInstance(); 205 } 206 catch (Exception x) 207 { 208 x.printStackTrace(); 209 logger = new Logger(); 210 } 211 logger.setCategory(category); 212 logger.setPriority(m_defaultPriority); 213 return logger; 214 } 215 216 222 public static void redirectTo(Logger prototype) 223 { 224 m_prototype = prototype; 225 226 synchronized (m_loggerCache) 228 { 229 m_loggerCache.clear(); 230 } 231 } 232 233 242 public static void redirectTo(Logger prototype, String category) 243 { 244 if (category == null) 245 { 246 throw new RuntimeOperationsException (new IllegalArgumentException ("Category cannot be null")); 247 } 248 249 if (prototype == null) 250 { 251 synchronized (m_prototypeMap) 253 { 254 m_prototypeMap.remove(category); 255 } 256 257 synchronized (m_loggerCache) 259 { 260 m_loggerCache.remove(category); 261 } 262 } 263 else 264 { 265 synchronized (m_prototypeMap) 267 { 268 m_prototypeMap.put(category, prototype); 269 } 270 271 synchronized (m_loggerCache) 273 { 274 m_loggerCache.remove(category); 275 } 276 } 277 } 278 } 279 | Popular Tags |