1 18 19 package org.objectweb.util.monolog.wrapper.log4jMini; 20 21 import org.apache.log4j.Appender; 22 import org.apache.log4j.Category; 23 import org.apache.log4j.Priority; 24 import org.objectweb.util.monolog.api.Handler; 25 import org.objectweb.util.monolog.api.Level; 26 import org.objectweb.util.monolog.api.TopicalLogger; 27 import org.objectweb.util.monolog.api.BasicLevel; 28 import org.objectweb.util.monolog.wrapper.log4jMini.LevelImpl; 29 30 import java.util.Enumeration ; 31 import java.util.Vector ; 32 import java.util.Hashtable ; 33 34 public class MonologCategory implements TopicalLogger { 35 36 protected Hashtable handlers; 37 protected boolean enable; 38 protected Category category; 39 protected OwPriority interPriority = null; 40 41 public MonologCategory(Category category) { 42 this.category = category; 43 enable = true; 44 } 45 46 48 51 public void setIntLevel(int level) { 52 if (level == BasicLevel.INHERIT) { 53 category.setPriority(null); 54 return; 55 } 56 switch (level) { 57 case 10000: 58 category.setPriority(org.apache.log4j.Priority.DEBUG); 59 break; 60 case 20000: 61 category.setPriority(org.apache.log4j.Priority.INFO); 62 break; 63 case 30000: 64 category.setPriority(org.apache.log4j.Priority.WARN); 65 break; 66 case 40000: 67 category.setPriority(org.apache.log4j.Priority.ERROR); 68 break; 69 case 50000: 70 category.setPriority(org.apache.log4j.Priority.FATAL); 71 break; 72 default: 73 if (interPriority == null) 74 interPriority = new OwPriority(level, String.valueOf(level)); 75 else 76 interPriority.level = level; 77 category.setPriority(interPriority); 78 break; 79 } 80 } 81 82 public void setLevel(Level l) { 83 if (l.getIntValue() == BasicLevel.INHERIT) { 84 category.setPriority(null); 85 return; 86 } 87 switch (l.getIntValue()) { 88 case 10000: 89 category.setPriority(org.apache.log4j.Priority.DEBUG); 90 break; 91 case 20000: 92 category.setPriority(org.apache.log4j.Priority.INFO); 93 break; 94 case 30000: 95 category.setPriority(org.apache.log4j.Priority.WARN); 96 break; 97 case 40000: 98 category.setPriority(org.apache.log4j.Priority.ERROR); 99 break; 100 case 50000: 101 category.setPriority(org.apache.log4j.Priority.FATAL); 102 break; 103 default: 104 if (interPriority == null) { 105 interPriority = new OwPriority(l.getIntValue(), l.getName()); 106 } 107 else { 108 interPriority.level = l.getIntValue(); 109 } 110 category.setPriority(interPriority); 111 break; 112 } 113 } 114 115 118 public int getCurrentIntLevel() { 119 Priority p = category.getPriority(); 120 return (p==null ? BasicLevel.INHERIT : p.toInt()); 121 } 122 123 public Level getCurrentLevel() { 124 Priority p = category.getPriority(); 125 return (p==null 126 ? BasicLevel.LEVEL_INHERIT 127 : LevelImpl.getLevel(p.toInt()) ); 128 } 129 130 133 public boolean isLoggable(int level) { 134 return level >= category.getChainedPriority().toInt(); 135 } 136 137 public boolean isLoggable(Level l) { 138 return isLoggable(l.getIntValue()); 139 } 140 141 144 public boolean isOn() { 145 return this.enable; 146 } 147 148 152 public void log(int level, Object o) { 153 if (this.enable && this.isLoggable(level)) { 154 callLog(level, o.toString(), null); 155 } 156 } 157 158 public void log(Level l, Object o) { 159 log(l.getIntValue(), o.toString()); 160 } 161 162 165 public void log(int level, Object o, Throwable t) { 166 if (this.enable && this.isLoggable(level)) 167 callLog(level, o, t); 168 } 169 170 public void log(Level l, Object o, Throwable t) { 171 log(l.getIntValue(), o, t); 172 } 173 174 178 public void log(int level, Object o, Object location, Object method) { 179 if (this.enable && this.isLoggable(level)) 180 log(level, o, null, location, method); 181 } 182 183 public void log(Level l, Object o, Object location, Object method) { 184 if (this.enable && this.isLoggable(l)) 185 log(l.getIntValue(), o, null, location, method); 186 } 187 188 192 public void log(int level, Object o, Throwable t, Object location, 193 Object method) { 194 if (this.enable && this.isLoggable(level)) 195 callLog(level, 196 location.toString() + "." + method.toString() 197 + ": " + o.toString(), 198 t); 199 } 200 201 public void log(Level l, Object o, Throwable t, Object location, 202 Object method) { 203 if (this.enable && this.isLoggable(l)) 204 log(l.getIntValue(), o, t, location, method); 205 } 206 207 210 public void turnOn() { 211 this.enable = true; 212 } 213 214 217 public void turnOff() { 218 this.enable = false; 219 } 220 221 public Handler[] getHandler() { 223 if (handlers==null) { 224 return new Handler[0]; 225 } 226 Handler[] result = new Handler[handlers.size()]; 227 int i = 0; 228 for (Enumeration e= handlers.elements(); e.hasMoreElements() ; i++) { 229 result[i] = (Handler)(e.nextElement()); 230 } 231 return result; 232 } 233 234 public String getName() { 235 return category.getName(); 236 } 237 238 public Handler getHandler(String hn) { 239 if (handlers==null) { 240 return null; 241 } 242 return (Handler) handlers.get(hn); 243 } 244 245 public void setName(String name) { 246 } 247 248 public void removeAllHandlers() throws Exception { 249 if (handlers!=null) { 250 handlers.clear(); 251 } 252 category.removeAllAppenders(); 253 } 254 255 public String getType() { 256 return "logger"; 257 } 258 259 public void setAdditivity(boolean a) { 260 category.setAdditivity(a); 261 } 262 263 public String [] getAttributeNames() { 264 return new String [0]; 265 } 266 267 public boolean getAdditivity() { 268 return category.getAdditivity(); 269 } 270 271 public Object getAttribute(String name) { 272 return null; 273 } 274 275 public String [] getTopic() { 276 String [] res = new String [1]; 277 res[0] = category.getName(); 278 return res; 279 } 280 281 public Object setAttribute(String name, Object value) { 282 return null; 283 } 284 285 288 public void addHandler(Handler h) throws Exception { 289 if (h instanceof Appender) { 290 if (handlers==null) { 291 handlers = new Hashtable (); 292 } 293 category.addAppender((Appender) h); 294 handlers.put(h.getName(), h); 295 } 296 else 297 throw new Exception ( 298 "The type of the handler does not match with this wrapper"); 299 } 300 301 304 public void addTopic(String topic) throws Exception { 305 } 306 307 310 public Enumeration getTopics() { 311 Vector v = new Vector (1); 312 v.addElement(category.getName()); 313 return v.elements(); 314 } 315 316 319 public void removeHandler(Handler h) throws Exception { 320 if (h instanceof Appender) { 321 if (handlers!=null) { 322 handlers.remove(h.getName()); 323 } 324 this.category.removeAppender((Appender) h); 325 } else 326 throw new Exception ( 327 "The type of the handler does not match with this wrapper"); 328 } 329 330 333 public void removeTopic(String topic) throws Exception { 334 throw new Exception ( 335 "The multiple name is not supported in this version"); 336 } 337 338 private void callLog(int level, Object o, Throwable t) { 339 if (level > Priority.ERROR_INT) 340 this.category.fatal(o, t); 341 else if (level > Priority.WARN_INT) 342 this.category.error(o, t); 343 else if (level > Priority.INFO_INT) 344 this.category.warn(o, t); 345 else if (level > Priority.DEBUG_INT) 346 this.category.info(o, t); 347 else 348 this.category.debug(o, t); 349 } 350 351 public class OwPriority extends org.apache.log4j.Priority { 354 355 protected int level = 10000; 356 357 public OwPriority(int l) { 358 super(l, "INTER"); 359 level = l; 360 } 361 public OwPriority(int l, String n) { 362 super(l, n); 363 level = l; 364 } 365 366 public boolean isGreaterOrEqual(Priority pr) { 367 return level >= pr.toInt(); 368 } 369 } 370 } 371 | Popular Tags |